question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Storybook is firing DOMContentLoaded twice on hard refresh

See original GitHub issue

Describe 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:open
  • Created 2 years ago
  • Reactions:3
  • Comments:6

github_iconTop GitHub Comments

8reactions
LucaPipolocommented, Oct 1, 2021

Another, perhaps cleaner, temporarily fix might be to pass { once: true } as option of addEventListener(). See MDN for more info about this parameter.

So it will be ⬇️

document.addEventListener('DOMContentLoaded', () => {
  // Your code...
}, { once: true });
1reaction
vascoeduardocommented, Jul 4, 2022

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.

let eventSrcUrl = null;

document.addEventListener('DOMContentLoaded', function (event) {
    console.log('DOM fully loaded and parsed') 
    if(eventSrcUrl != event.srcElement.URL) {
        eventSrcUrl = event.srcElement.URL
        console.log('Init JS features' )
        // init();
    } else {
        console.log("second DOMContentLoaded Event was blocked!");
    }   
})

With this implemented, my JS init script is only loaded 1 time. It works with “hard reload” and switching storys. Hope this helps

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found