-
Notifications
You must be signed in to change notification settings - Fork 338
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
Investigate components being initialised more than once #1127
Comments
We need to make sure that we allow for use cases where people inject new HTML into the DOM after the page has loaded and then running the initialise functions to enhance the new components. Custom Elements seem to account for this, and the polyfills have code that could help us |
I've put together an example of how you could store instances on the classes for each component. We'd likely also want to have consistent destroy methods too, which I've demonstrated. |
My approach I've shared already puts the instance information on the components themselves, but we could also consider an approach similar to web component custom elements. Where we have a single registry (could be an array), that we update and check:
It's important to consider how downstream components might also copy this approach. I think this approach only works well when you can be sure that initialising a component once will then work for any new components added to the page, for example loading markup in a modal. Since this is not the case with GOV.UK Frontend, it may not be the best way to do things, |
Seen this again from a Prototype Kit user. Their tabs component was skipping two tabs at once since they accidentally included their |
As discussed in the JavaScript effort/value session, this is happening because we don't have any guards against initialising a component more than once. This doesn't feel right, but it's also a bit of an unusual case which doesn't come up very often. |
Having this functionality may come in useful with our current work relating to localisation and programatic APIs. For example, a service developer may only want to pass configuration options to a specific instance of a component, but use the default options for all other components. In attempting to do so, they may write something like this: new window.GOVUKFrontend.Accordion($element, { config }).init()
window.GOVUKFrontend.initAll() However, unless the developer takes care to remove the The simplest way to do this is probably by recording which elements have already been initialised and checking against that record when components are initialised again. This could be done by adding a |
In a call this week about upcoming changes to the Prototype Kit, it was asked whether it was possible to run Frontend's |
happened again on support today |
Another instance of this happening on support today, this time in an app using Turbolinks. Turbolinks caches the DOM state of a page when navigating away, restoring that state if the page is navigated back to (in a similar, but not the same, way as bfcache). This was resulting in DOM manipulations made through our JavaScript happening multiple times if the page was navigated away from and back to again. |
Another instance of this happening on support today, at: https://github.com/DFE-Digital/npq-prototype/blob/main/app/views/registration-status/accordion.html |
was this fixed in recent work? |
Issue should be fixed by #5272, but we'll wait till it's released to close this issue properly. |
GOV.UK Frontend v5.7.0 now prevents components from being initialised twice on the same element, so this can now be closed. |
We had a case of a user initialising the components twice by accident. This doesn't warn or error, but creates a broken state (the accordion for example opens then closes on click, and creates two 'Open all' buttons.
The text was updated successfully, but these errors were encountered: