Storybook is firing DOMContentLoaded twice on hard refresh
See original GitHub issueDescribe the bug Also seems to be happening on hot reload too.
To Reproduce
window.addEventListener('DOMContentLoaded', () => {
console.log('fired')
});
Then refresh the page using F5.
This is due to storybook simulating page reload for hot-reload changes here:
export function simulateDOMContentLoaded() {
var DOMContentLoadedEvent = document.createEvent('Event');
DOMContentLoadedEvent.initEvent('DOMContentLoaded', true, true);
document.dispatchEvent(DOMContentLoadedEvent);
}
However it should not fire the above function on a hard refresh of the page because the browser fires it automatically.
Here’s current workaround:
preview.js
let domContentAlreadyLoaded = false;
document.addEventListener('DOMContentLoaded', (e) => {
if (domContentAlreadyLoaded) {
e.stopPropagation();
}
domContentAlreadyLoaded = true;
});
Hot reload doesn’t work but it’s better than bugs.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:6
Top Results From Across the Web
DOMContentLoaded will be fired twice on my page
I try to search online, and find two results: DOMContentLoaded event firing twice for a single page load, which said AVG add-on causes...
Read more >DOMContentLoaded event running twice, not sure how to ...
Something you can try to do is set your DOMContentLoaded to only fire once. You can do this with the once option. window.addEventListener(" ......
Read more >Ludicrously Fast Page Loads - A Guide for Full-Stack Devs
Your website is slow, but the backend is fast. How do you diagnose performance issues on the frontend of your site? We'll discuss...
Read more >HTML Standard
... 4.10.5.1.19 Image Button state ( type=image ); 4.10.5.1.20 Reset Button state ( type=reset ); 4.10.5.1.21 Button state ( type=button ).
Read more >Deep dive into the murky waters of script loading - web.dev
In the above example, the browser will download both scripts in parallel and execute them just before DOMContentLoaded fires, ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Another, perhaps cleaner, temporarily fix might be to pass
{ once: true }
as option ofaddEventListener()
. See MDN for more info about this parameter.So it will be ⬇️
Hi everyone, as i encountered the same problem with the DOMContentLoaded event being fired twice, i tried your suggested workarounds. unfortunately they didn’t worked form and I had to find another workaround to fit my needs.
In my solution I basically just check from which srcElement the DOMContentLoaded is fired an store the URL in a variable. If the event is fired again, I check if it is the same URL to decide if the event should be ignored.
With this implemented, my JS init script is only loaded 1 time. It works with “hard reload” and switching storys. Hope this helps