Skip to content

Commit

Permalink
Use <script type="module"> to defer inline scripts
Browse files Browse the repository at this point in the history
Since `<script defer>` is not available for inline scripts, this change ensures inline scripts are also deferred until after `window.GOVUKPrototypeKit` is available
  • Loading branch information
colinrotherham committed Sep 18, 2023
1 parent a710825 commit 7291342
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const pluginBazViewMarkup = `
{% endblock %}
{% block pageScripts %}
<script>
<script type="module">
window.GOVUKPrototypeKit.documentReady(() => {
new window.BAZ.Modules.PluginBaz('.test-baz')
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const pluginFooBarCombinedViewMarkup = `
{% endblock %}
{% block pageScripts %}
<script>
<script type="module">
window.GOVUKPrototypeKit.documentReady(() => {
new window.BAR.Modules.PluginBar('.test-bar')
new window.FOO.Modules.PluginFoo('.test-foo')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const pluginFooBarSeparatedViewMarkup = `
{% endblock %}
{% block pageScripts %}
<script>
<script type="module">
window.GOVUKPrototypeKit.documentReady(() => {
new window.FOO.Modules.PluginFoo('.test-foo')
new window.BAR.Modules.PluginBar('.test-bar')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const pluginFooViewMarkup = `
{% endblock %}
{% block pageScripts %}
<script>
<script type="module">
window.GOVUKPrototypeKit.documentReady(() => {
new window.FOO.Modules.PluginFoo('.test-foo')
})
Expand Down
2 changes: 1 addition & 1 deletion lib/nunjucks/views/error-handling/server-error.njk
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@


{% block pageScripts %}
<script>
<script type="module">
;(() => {
const toggleErrorStack = (event) => {
const button = event.target
Expand Down
25 changes: 12 additions & 13 deletions lib/nunjucks/views/manage-prototype/plugin-install-or-uninstall.njk
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,17 @@
{% endblock %}

{% block pageScripts %}
<script>
if (window.Promise) {
// Dynamically load and execute the script
const managePluginsScript = document.createElement('script')
managePluginsScript.addEventListener('load', () => {
window.GOVUKPrototypeKit.documentReady(window.GOVUKPrototypeKit.internal.managePlugins.init)
})
managePluginsScript.setAttribute('src', '/plugin-assets/govuk-prototype-kit/lib/assets/javascripts/manage-prototype/manage-plugins.js')
document.body.appendChild(managePluginsScript)
} else {
// Display the page as if javascript was not enabled
document.body.classList.remove('js-enabled')
}
<script type="module">
// Dynamically load and execute the script
const managePluginsScript = document.createElement('script')
managePluginsScript.addEventListener('load', () => {
window.GOVUKPrototypeKit.documentReady(window.GOVUKPrototypeKit.internal.managePlugins.init)
})
managePluginsScript.setAttribute('src', '/plugin-assets/govuk-prototype-kit/lib/assets/javascripts/manage-prototype/manage-plugins.js')
document.body.appendChild(managePluginsScript)
</script>
<script nomodule>
// Display the page as if javascript was not enabled
document.body.classList.remove('js-enabled')
</script>
{% endblock %}

0 comments on commit 7291342

Please sign in to comment.