My NextJS website utilizes a custom JavaScript provided by an ad provider for running ads. The script is typically structured like this:
<script type="text/javascript" src="//cdn.adsite.com/static/js/ad.js" ></script>
In addition to the script, I place div tags on the webpage to serve as ad slots:
<div id="ad-tag-6098" ></div>
<div id="ad-tag-6099" ></div>
<div id="ad-tag-6076" ></div>
These div tags correspond to different ad slots on the page and are filled with ads using the JavaScript.
While this setup functions correctly on initial page loads or reloads, it fails to display ads when navigating internally through the router.
I am seeking guidance on how to properly reload the ad.js script to ensure that the ad slots display content accurately and refresh appropriately even during navigation using the NextJS router.
UPDATE: I have discovered a more complex issue related to the functioning of these ad slots. Simply reloading the JavaScript is insufficient. Due to the nature of these slots, transitioning between pages causes them to be stored in memory, leading to errors as the script recognizes them as already displayed.
The desired functionality should involve:
- Rendering the ad slot component, defining it, and displaying the ad,
- Removing all defined slots from memory upon route changes,
- Upon visiting a new page, allowing ad components to define and display fresh slots.