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

Plugin scripts with "type": "module" run too late #2353

Closed
colinrotherham opened this issue Oct 3, 2023 · 2 comments · Fixed by #2359
Closed

Plugin scripts with "type": "module" run too late #2353

colinrotherham opened this issue Oct 3, 2023 · 2 comments · Fixed by #2359
Assignees
Labels
🐛 Bug Something isn't working the way it should (including incorrect wording in documentation)

Comments

@colinrotherham
Copy link
Contributor

colinrotherham commented Oct 3, 2023

Description of the issue

Prototypes using window.GOVUKFrontend will break when alphagov/govuk-frontend#4264 is released

Why? Plugin scripts configured with "type": "module" execute after prototype JavaScript has already run

Steps to reproduce the issue

  1. Install the GOV.UK Frontend v5 preview (via GitHub commit)

    npm install --save "alphagov/govuk-frontend#5b2bfde4f"
  2. Edit prototype app/assets/javascripts/application.js and add:

    console.log(window.GOVUKFrontend)
  3. Open the prototype in a browser

Actual vs expected behaviour

Actual: GOV.UK Frontend global window.GOVUKFrontend is undefined
Expected: Prototype scripts are also deferred, ensuring window.GOVUKFrontend is available

Prototypes correctly using window.GOVUKPrototypeKit.documentReady() are unaffected

@colinrotherham colinrotherham added the 🐛 Bug Something isn't working the way it should (including incorrect wording in documentation) label Oct 3, 2023
@colinrotherham
Copy link
Contributor Author

colinrotherham commented Oct 3, 2023

Not strictly a bug since since impact depends on:

  1. How many prototypes access window.GOVUKFrontend directly?
  2. How many plugins (or future plugins) access window.GOVUKFrontend directly?
  3. How widely is window.GOVUKPrototypeKit.documentReady() used to initialise safely?

@colinrotherham
Copy link
Contributor Author

colinrotherham commented Oct 9, 2023

The simplest fix was to add type="module" to application.js in:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 Bug Something isn't working the way it should (including incorrect wording in documentation)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant