-
Notifications
You must be signed in to change notification settings - Fork 333
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
Update how we load JavaScript in browser #2639
Comments
Changing the That association is currently created through Rollup's bundling process, and I couldn't find anything in the Rollup docs that would create an explicit Changing the Rollup configuration in return gulp.src(file)
.pipe(rollup({
// Used to set the `window` global and UMD/AMD export name
// Component JavaScript is given a unique name to aid individual imports, e.g GOVUKFrontend.Accordion
name: moduleName,
- // Legacy mode is required for IE8 support
- legacy: true,
- // UMD allows the published bundle to work in CommonJS and in the browser.
- format: 'umd'
+ format: 'es'
})) …and changing the initialisation code in HTML: - <script src="/public/all.js"></script>
- <script>window.GOVUKFrontend.initAll()</script>
+ <script type="module">
+ import { initAll } from '/public/all.js'
+ initAll()
+ </script> This seems to work as desired. JS is loaded in modern browsers that understand However this still presented some weirdness in IE: styles used when JS is available were still being applied to components like the Accordion, which in that case was making the accordion's content inaccessible. This is because the code that adds the Adding - <script{% if cspNonce %} nonce="{{ cspNonce }}"{% endif %}>document.body.className = ((document.body.className) ? document.body.className + ' js-enabled' : 'js-enabled');</script>
+ <script type="module" {%- if cspNonce %} nonce="{{ cspNonce }}"{% endif %}>document.body.className = ((document.body.className) ? document.body.className + ' js-enabled' : 'js-enabled');</script> This probably wouldn't be necessary if this code was present in the compiled JS; or if we made it so that components had individual 'enabled' states. (I imagine this JS is currently separated so that the class is applied as soon as possible and the browser doesn't first render a no-JS version of the CSS, before having to switch to yes-JS styles.) But that might be a discussion for another time. |
Was going to add #2639 but that's also done Can mark this as closed too 🎉 |
What
We're updating how browsers should load our JavaScript to ensure it only runs in browsers that are actually capable of executing it, letting older browsers show our components in their non-JavaScript state.
This involves:
govuk-frontend
providing code that can be parsed in any browser supporting<script type="module">
<script type="module">
to avoid scripts being downloaded in older browsersWhy
We have decided to reduce support for IE11 in GOV.UK Frontend and drop support completely for IE8-10. We shared these proposed changes with the frontend community in GDS and across government, and have only received positive feedback.
As part of this proposal, we said we would stop shipping JavaScript to IE browsers. These browsers will fall back to a no-JavaScript experience.
Who needs to work on this
Developers; Tech Writer
Who needs to review this
Developers
Done when
Update JavaScript loading
The text was updated successfully, but these errors were encountered: