Last active
November 4, 2025 12:49
-
-
Save Karnak19/1fb931ee087bc99e1d9d5694bf311a4e to your computer and use it in GitHub Desktop.
Ramp loading for NextJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "use client"; | |
| import { usePathname } from "next/navigation"; | |
| import Script from "next/script"; | |
| import { useEffect, useState } from "react"; | |
| type RampWindow = Window & { | |
| ramp?: { | |
| que: Array<() => void>; | |
| spaNewPage: (pathname: string) => void; | |
| passiveMode?: boolean; | |
| }; | |
| }; | |
| declare const window: RampWindow; | |
| export function PlaywireAds({ | |
| PUB_ID, | |
| WEBSITE_ID, | |
| }: { | |
| PUB_ID: string; | |
| WEBSITE_ID: string; | |
| }) { | |
| const [scriptLoaded, setScriptLoaded] = useState(false); | |
| const pathname = usePathname(); | |
| // Initialize ramp on mount | |
| useEffect(() => { | |
| if (!(PUB_ID && WEBSITE_ID)) { | |
| // biome-ignore lint/suspicious/noConsole: Error logging for missing configuration | |
| console.error("PUB_ID or WEBSITE_ID is not set"); | |
| return; | |
| } | |
| if (!scriptLoaded) { | |
| if (!window.ramp) { | |
| window.ramp = { | |
| que: [], | |
| spaNewPage: (_pathname: string) => { | |
| // Placeholder until Playwire script initializes | |
| }, | |
| }; | |
| } | |
| window.ramp.que = window.ramp.que || []; | |
| window.ramp.passiveMode = true; | |
| } | |
| }, [PUB_ID, WEBSITE_ID, scriptLoaded]); | |
| // Handle SPA navigation on pathname change | |
| useEffect(() => { | |
| // Only fire if script is loaded | |
| if (!(scriptLoaded && window.ramp?.spaNewPage)) { | |
| return; | |
| } | |
| // Fire spaNewPage when pathname changes to update ads for the new page | |
| window.ramp.que.push(() => { | |
| window.ramp?.spaNewPage(pathname); | |
| }); | |
| }, [pathname, scriptLoaded]); | |
| const handleScriptLoad = () => { | |
| setScriptLoaded(true); | |
| }; | |
| if (!(PUB_ID && WEBSITE_ID)) { | |
| return null; | |
| } | |
| return ( | |
| <Script | |
| onLoad={handleScriptLoad} | |
| src={`//cdn.intergient.com/${PUB_ID}/${WEBSITE_ID}/ramp.js`} | |
| strategy="afterInteractive" | |
| /> | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment