-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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
next/script in _app.js creates duplicate script tags in document after moving between pages #26860
Comments
Your reproduction repo is just the |
@awareness481 sorry just pushed commit |
@awareness481 should have maybe worded the issue differently. It's not that I see the script running more than once per navigation but that it runs again when you navigate to another page. In this example I do |
Even I'm facing this issue. |
EDIT: See my message below. App.tsx export default function MyApp({ Component, pageProps }) {
return <>
<Script dangerouslySetInnerHTML={{
__html: (`
(function () {
var id = 'gtaginit';
if (window[id]) { return; }
window.dataLayer = window.dataLayer || [];
window.gtag = function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${GA_TRACKING_ID}');
window[id] = true;
})();
`)
}} />
<Script src={`https://www.googletagmanager.com/gtag/js?id=${GA_TRACKING_ID}`} />
<Component {...pageProps} />
</>
} |
Okay, thinking better there's no reason to load this kind of script this way. Using React Hooks this code can be much cleaner: // gtag.ts
const initialize = () => {
window.dataLayer = window.dataLayer || [];
window.gtag = function gtag() { window.dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', GA_TRACKING_ID);
};
// app.tsx
export default function MyApp({ Component, pageProps }) {
useEffect(initialize, []); // this code will run only once
return <>
<Script src={`https://www.googletagmanager.com/gtag/js?id=${GA_TRACKING_ID}`} />
<Component {...pageProps} />
</>
} |
I revered to using good old |
This is happening with next 11.1.0 for when I use |
Since those scripts are private, I cannot make that example here but I can see that the scripts get executed multiple times thus lead to the error. |
…onent (vercel#27218) Fixes inline scripts being duplicated when used with `next/script` component ## Bug - [x] fixes vercel#26860 - [x] Integration tests added ## Documentation / Examples Updated docs to indicate that `id` is needed for inline scripts
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
What version of Next.js are you using?
11.0.1
What version of Node.js are you using?
16.1.0
What browser are you using?
Chrome, Safari
What operating system are you using?
macOS
How are you deploying your application?
next export
Describe the Bug
When I add inline script tags to
_app.js
they are then added to the document and ran multiple times when navigating between pages.This is causing problems with GTM as it's throwing errors that it has already been registered.
Expected Behavior
There should only be one script tag in the document per
next/script
and it should run once instead of every time navigation occurs.To Reproduce
https://github.com/williamtetlow/nextjs-multiple-script-tags
next/script
are ran multiple timesThe text was updated successfully, but these errors were encountered: