File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11// ref: https://github.com/btk/nextjs-google-adsense/blob/master/src/components/GoogleAdSense.tsx
22// ref: https://medium.com/frontendweb/how-to-add-google-adsense-in-your-nextjs-89e439f74de3
33
4+ import { usePathname } from "next/navigation" ;
45import type { ScriptProps } from "next/script" ;
56import Script from "next/script" ;
67// biome-ignore lint/correctness/noUnusedImports: React refers to a UMD global, but the current file is a module.
@@ -21,6 +22,7 @@ export const GoogleAdSense = ({
2122 debug = false ,
2223 ...props
2324} : GoogleAdSenseProps ) : JSX . Element | null => {
25+ const pathname = usePathname ( ) ;
2426 const _publisherId =
2527 process . env . NEXT_PUBLIC_ADSENSE_PUBLISHER_ID ?? publisherId ;
2628
@@ -33,6 +35,8 @@ export const GoogleAdSense = ({
3335
3436 return (
3537 < Script
38+ // Rerender the script when pathname changes to ensure load when navigating pages
39+ key = { `adsbygoogle-js-${ _publisherId } -${ pathname } ` }
3640 async = { true }
3741 src = { `https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-${ _publisherId } ${
3842 debug ? "google_console=1" : ""
You can’t perform that action at this time.
0 commit comments