next.js/test/e2e/nonce-head-manager/app/pages/index.js
index.js38 lines1.0 KB
import React from 'react'
import Script from 'next/script'

const Page = () => {
  const [counter, setCounter] = React.useState(0)
  const [scriptURL, toggleScriptSource] = React.useReducer(
    (currentScriptURL, id) => {
      const scriptSource =
        typeof currentScriptURL === 'string'
          ? currentScriptURL
          : currentScriptURL.pathname
      if (scriptSource === '/src-1.js') {
        return new URL(`/src-2.js?key=${Math.random()}`, window.location)
      } else {
        return new URL(`/src-1.js?key=${Math.random()}`, window.location)
      }
    },
    '/src-1.js'
  )
  const scriptSource =
    typeof scriptURL === 'string' ? scriptURL : scriptURL.href

  return (
    <>
      <Script key={scriptSource} nonce="abc123" src={scriptSource} />
      <h1 id="h1">{'Count ' + counter}</h1>
      <button id="force-rerender" onClick={() => setCounter(counter + 1)}>
        Re-render
      </button>
      <button id="change-script" onClick={toggleScriptSource}>
        Change script src
      </button>
    </>
  )
}

export default Page
Quest for Codev2.0.0
/
SIGN IN