Scripts on the layout page do not load correctly with internal routing

I have a NodeJS SPA where I have setup an internal routing to the Home Page when successfully logged out. The content pages have been setup with one layout page; so the routing is happening from one of the content pages to another content page, i.e. Home Page.

The routing works correctly by navigating to the Home page, but many a times the Home page does not display any content because dmx scripts on the layout page fail to load (screenshot attached). If I refresh the Home page, then it displays the content correctly.

The only way I have been able to resolve this issue is by disabling the internal routing. How can I resolve the issue of dmx scripts failing to load when internal routing is enabled? Any help to resolve this is highly appreciated.