Skip to content

Commit 61a984b

Browse files
committed
fix: add pathname based refresh to GoogleAdSense
1 parent 3e29f0a commit 61a984b

1 file changed

Lines changed: 4 additions & 0 deletions

File tree

src/GoogleAdSense.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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";
45
import type { ScriptProps } from "next/script";
56
import 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" : ""

0 commit comments

Comments
 (0)