Skip to content
Discussion options

You must be logged in to vote

A conditional <Script> in _app can control when the script is first injected, but it cannot undo a script that has already loaded. _app persists across client-side navigation, and the third-party code may have already registered globals, listeners, or DOM changes. Removing the React component on the next route does not reverse those effects.

If the goal is "do not load it until one of these routes is visited", an allow-list in _app is fine:

import Script from "next/script"
import { useRouter } from "next/router"

const routesWithVendor = new Set(["/checkout", "/account"])

export default function MyApp({ Component, pageProps }) {
  const { pathname } = useRouter()

  return (
    <>
      <

Replies: 4 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by vipindigiqt
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
4 participants