-
Notifications
You must be signed in to change notification settings - Fork 237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add recommended lite page js approach #12592
base: latest
Are you sure you want to change the base?
Conversation
|
||
By applying this approach we can use modern JS and Typescript when maintaining the code rather than inlining transpiled code directly in our React components and maintianing it as a string. | ||
|
||
Additionally we are able to reliably test this code as we do any other code we maintain as shown in [this PR](https://github.com/bbc/simorgh/pull/12360/files#diff-a2444976147693573560a81ce6e248fa83e719d9474f021ba94707f0225560c4R2). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As above, this code has now moved on: imports
implementation - should we include these links instead?
Or should we include the links to the tests for the scripts (e.g.
- https://github.com/bbc/simorgh/blob/latest/src/server/utilities/liteATITracking/index.test.ts
- https://github.com/bbc/simorgh/blob/latest/src/server/utilities/liteATITracking/viewTracking/index.test.ts
- https://github.com/bbc/simorgh/blob/latest/src/server/utilities/liteATITracking/clickTracking/index.test.ts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If folks agree with my approach re: the PR above, I'd leave it as is, let's discuss on this PR 👍
Co-authored-by: Karina Thomas <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth calling out, if the inline JS that we create imports another file, I don't think the transpilation will work properly, so that needs to be bared in mind. There might be a way we can configure babel and webpack to include imported libraries directly inline maybe? But atm it seems like it can't, which was an issue found doing the opera mini script beacon
simorgh/src/app/components/ATIAnalytics/canonical/sendBeaconOperaMiniScript/index.ts
Line 1 in 4d31a92
import isOperaProxy from '#app/lib/utilities/isOperaProxy'; |
Should this: import isOperaProxy from '#app/lib/utilities/isOperaProxy';
const sendBeaconOperaMiniScript = (atiPageViewUrlString: string) => `
if (${isOperaProxy.toString()}() && !Boolean(window.hasOperaMiniScriptRan)) {
window.hasOperaMiniScriptRan = true;
var atiPageViewUrl = "${atiPageViewUrlString}";
atiPageViewUrl += document.referrer ? "&ref=" + document.referrer : '';
var xhr = new XMLHttpRequest();
xhr.open("GET", atiPageViewUrl, true);
xhr.withCredentials = true;
xhr.send();
}
`;
export default sendBeaconOperaMiniScript; Be changed to something like this: import isOperaProxy from '#app/lib/utilities/isOperaProxy';
const sendBeaconOperaMiniScript = (atiPageViewUrlString: string) =>
if (isOperaProxy() && !Boolean(window.hasOperaMiniScriptRan)) {
window.hasOperaMiniScriptRan = true;
var atiPageViewUrl = "${atiPageViewUrlString}";
atiPageViewUrl += document.referrer ? "&ref=" + document.referrer : '';
var xhr = new XMLHttpRequest();
xhr.open("GET", atiPageViewUrl, true);
xhr.withCredentials = true;
xhr.send();
}
;
export default sendBeaconOperaMiniScript; and the usage contain a tiny bit of inline js:
So we only use inline JS to pass parameters? This would apply the recommended pattern I think, can you check this @HarveyPeachey ? |
The main problem is isOperaProxy():
When transpiled, webpack attaches a reference to the imported module, it doesn't inline the imported code. Does that make sense? So the alternative is to take the code in isOperaProxy, and just paste it in manually. |
Hmm, I can't think of an alternative to disallowing importing of scripts into inline JS? I think this might be better than the alternative of maintaining transpiled code in a string? What do you think @Isabella-Mitchell @karinathomasbbc @amoore108 |
I might need to do a bit more digging / thinking about this, but could we add some sort of annotation to files which are designed to be in-lined, and run some sort of unit test to ensure that it doesn't import code from elsewhere? (Not sure if this is even viable / possible). |
There could be something here: https://eslint.org/docs/latest/rules/no-restricted-imports |
My question is more is disallowing imports sensible? I don't think we have to have tooling just some guidance we agree on in this case. I personally think it's better to have modern code with some moderate repetition rather than maintaining transpiled code as a string. |
Resolves JIRA: NA
Summary
Adds recommended lite page js approach to Coding Standards to ensure inline JS we add is maintainable
Developer Checklist
Testing
Ready-For-Test, Local
)Ready-For-Test, Test
)Ready-For-Test, Preview
)Ready-For-Test, Live
)Additional Testing Steps
Useful Links