Skip to content
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

Preview v5: Update to use <script type="module"> #2958

Merged
merged 3 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions lib/metalsmith.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,9 @@ module.exports = metalsmith
done()
})

// build the entrypoint for application specific JavaScript
// build the entrypoints for application specific JavaScript
.use(rollup('javascripts/application.mjs'))

// build GOV.UK Frontend JavaScript
.use(rollup('javascripts/govuk-frontend.mjs'))

// build the entrypoint for example specific JavaScript
.use(rollup('javascripts/example.mjs'))
.use(rollup('javascripts/application-example.mjs'))

// add hash to files in production
.use((files, metalsmith, done) => {
Expand Down
13 changes: 13 additions & 0 deletions src/javascripts/application-example.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { initAll } from 'govuk-frontend'

import ExamplePage from './components/example-page.mjs'

// Initialise GOV.UK Frontend
initAll({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example <iframe> pages now use application-example.mjs

We adjust initAll() from GOV.UK Frontend to turn off auto focus

// Auto focusing the error summary and notification banner is not useful
// when used in examples, and causes the viewport to scroll
errorSummary: { disableAutoFocus: true },
notificationBanner: { disableAutoFocus: true }
})

new ExamplePage(document).init()
5 changes: 5 additions & 0 deletions src/javascripts/application.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { initAll } from 'govuk-frontend'

import Analytics from './components/analytics.mjs'
import BackToTop from './components/back-to-top.mjs'
import CookieBanner from './components/cookie-banner.mjs'
Expand All @@ -11,6 +13,9 @@ import OptionsTable from './components/options-table.mjs'
import Search from './components/search.mjs'
import AppTabs from './components/tabs.mjs'

// Initialise GOV.UK Frontend
initAll()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For application.mjs we can use the default initAll() from GOV.UK Frontend


// Initialise cookie banner
var $cookieBanner = document.querySelector('[data-module="govuk-cookie-banner"]')
new CookieBanner($cookieBanner).init()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
function ExamplePage ($module) {
this.$module = $module
}

ExamplePage.prototype.init = function () {
var $module = this.$module
if (!$module) {
Expand All @@ -9,6 +10,7 @@ ExamplePage.prototype.init = function () {
var $form = $module.querySelector('form[action="/form-handler"]')
this.preventFormSubmission($form)
}

ExamplePage.prototype.preventFormSubmission = function ($form) {
// we should only have one form per example
if (!$form) {
Expand All @@ -19,4 +21,4 @@ ExamplePage.prototype.preventFormSubmission = function ($form) {
})
}

new ExamplePage(document).init()
export default ExamplePage
12 changes: 0 additions & 12 deletions src/javascripts/govuk-frontend.mjs

This file was deleted.

4 changes: 2 additions & 2 deletions src/styles/page-template/block-areas/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ stylesheets:
<span class="app-annotate-block__label">block: bodyEnd</span>
{{ super() }}
{# Since we’re not extending the Design System layout we need to add this manually #}
<script src="{{ getFingerprint('javascripts/govuk-frontend.js') }}"></script>
<script src="{{ getFingerprint('javascripts/vendor/iframeResizer.contentWindow.min.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/application-example.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/vendor/iframeResizer.contentWindow.min.js') }}"></script>
</div>
{%- endblock %}
6 changes: 5 additions & 1 deletion src/styles/page-template/custom/code.njk
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,9 @@ ignoreInSitemap: true
{% endblock %}

{% block bodyEnd %}
<script src="/govuk-frontend/all.js"></script>
<script type="module" src="/javascripts/govuk-frontend.min.js"></script>
<script type="module">
import { initAll } from '/javascripts/govuk-frontend.min.js'
initAll()
</script>
{% endblock %}
4 changes: 2 additions & 2 deletions src/styles/page-template/custom/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,6 @@ ignoreInSitemap: true
{% endblock %}

{% block bodyEnd %}
<script src="{{ getFingerprint('javascripts/govuk-frontend.js') }}"></script>
<script src="{{ getFingerprint('javascripts/vendor/iframeResizer.contentWindow.min.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/application-example.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/vendor/iframeResizer.contentWindow.min.js') }}"></script>
{% endblock %}
7 changes: 5 additions & 2 deletions src/styles/page-template/default/code.njk
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ ignoreInSitemap: true

{% block bodyEnd %}
{# Run JavaScript at end of the <body>, to avoid blocking the initial render. #}
<script src="/govuk-frontend/all.js"></script>
<script>window.GOVUKFrontend.initAll()</script>
<script type="module" src="/javascripts/govuk-frontend.min.js"></script>
<script type="module">
import { initAll } from '/javascripts/govuk-frontend.min.js'
initAll()
</script>
{% endblock %}
4 changes: 2 additions & 2 deletions src/styles/page-template/default/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ layout: false
{% endblock %}

{% block bodyEnd %}
<script src="{{ getFingerprint('javascripts/govuk-frontend.js') }}"></script>
<script src="{{ getFingerprint('javascripts/vendor/iframeResizer.contentWindow.min.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/application-example.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/vendor/iframeResizer.contentWindow.min.js') }}"></script>
{% endblock %}
3 changes: 1 addition & 2 deletions views/layouts/_generic.njk
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,5 @@
{% block footer %}{% endblock %}

{% block bodyEnd %}
<script src="{{ getFingerprint('javascripts/govuk-frontend.js') }}"></script>
<script src="{{ getFingerprint('javascripts/application.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/application.js') }}"></script>
{% endblock %}
5 changes: 2 additions & 3 deletions views/layouts/layout-example-full-page-govuk.njk
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
{{ contents | safe }}
{% endblock %}
{% block bodyEnd %}
<script src="{{ getFingerprint('javascripts/govuk-frontend.js') }}"></script>
<script src="{{ getFingerprint('javascripts/example.js') }}"></script>
<script src="{{ getFingerprint('javascripts/vendor/iframeResizer.contentWindow.min.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/application-example.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/vendor/iframeResizer.contentWindow.min.js') }}"></script>
{% endblock %}
5 changes: 2 additions & 3 deletions views/layouts/layout-example-full-page.njk
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
{{ contents | safe }}
{% endblock %}
{% block bodyEnd %}
<script src="{{ getFingerprint('javascripts/govuk-frontend.js') }}"></script>
<script src="{{ getFingerprint('javascripts/example.js') }}"></script>
<script src="{{ getFingerprint('javascripts/vendor/iframeResizer.contentWindow.min.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/application-example.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/vendor/iframeResizer.contentWindow.min.js') }}"></script>
{% endblock %}
5 changes: 2 additions & 3 deletions views/layouts/layout-example.njk
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@
{% endblock %}

{% block bodyEnd %}
<script src="{{ getFingerprint('javascripts/govuk-frontend.js') }}"></script>
<script src="{{ getFingerprint('javascripts/example.js') }}"></script>
<script src="{{ getFingerprint('javascripts/vendor/iframeResizer.contentWindow.min.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/application-example.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/vendor/iframeResizer.contentWindow.min.js') }}"></script>
{% endblock %}

{# Example pages shouldn't have a footer, so blank the one provided by the template #}
Expand Down
36 changes: 21 additions & 15 deletions views/partials/_cookie-banner.njk
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,31 @@
to avoid a high cumulative layout shift (CLS) score https://web.dev/cls/ #}
<script>

/* If cookie policy changes and/or the user preferences object format needs to
* change, bump this version up afterwards. The user should then be shown the
* banner again to consent to the new policy.
*
* Note that because isValidCookieConsent checks that the version in the user's
* cookie is equal to or greater than this number, you should be careful to
* check backwards compatibility when changing the object format.
*
/**
* If cookie policy changes and/or the user preferences object format needs to
* change, bump this version up afterwards. The user should then be shown the
* banner again to consent to the new policy.
*
* Note that because isValidCookieConsent checks that the version in the user's
* cookie is equal to or greater than this number, you should be careful to
* check backwards compatibility when changing the object format.
*/
window.GDS_CONSENT_COOKIE_VERSION = 1;

(function () {
/** Check the cookie preferences object.
*
* If the consent object is not present, malformed, or incorrect version,
* returns false, otherwise returns true.
*
* This is also duplicated in cookie-functions.js - the two need to be kept in sync
*/
// Skip early setup when cookie banner component is not supported
if (!('noModule' in HTMLScriptElement.prototype)) {
return
}

/**
* Check the cookie preferences object.
*
* If the consent object is not present, malformed, or incorrect version,
* returns false, otherwise returns true.
*
* This is also duplicated in cookie-functions.js - the two need to be kept in sync
*/
function isValidConsentCookie (options) {
return (options && options.version >= window.GDS_CONSENT_COOKIE_VERSION)
}
Expand Down