Website template: "Encountered a script tag" error on 404 pages in development

Open
#19,577 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@peteruithoven is already working on this.

Since Nov 13, 2026.

  • #19580 by @peteruithoven — open
  • #19581 by @peteruithoven — open

Assessment

Difficulty
3/5
Estimated time
Half a day
Newbie friendliness
25/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Domain
frontend

Research direction

The error comes from src/providers/Theme/InitTheme/index.tsx, which renders the theme script via next/script in the root layout; the ThemeProvider sets data-theme in an effect. Reproduce by visiting a non-existent route in the website template in dev and checking the browser console. Done means no 'Encountered a script tag' error on not-found pages while the theme still applies. Open PR #19580 already targets this, so check it before starting.

Written by the indexing model from the issue text.

Description

area: templates Bug Payload OSS
Describe the Bug

In the website template, visiting any page that doesn't exist (which calls notFound()) logs this error in the browser console during development:

Encountered a script tag while rendering React component. Scripts inside React components are never executed when rendering on the client. Consider using template tag instead (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template).

Regular pages don't have this error.

The error comes from src/providers/Theme/InitTheme/index.tsx, which renders the theme script with next/script and strategy="beforeInteractive" in the root layout. On a notFound() page, Next.js renders the root layout on the client instead of hydrating server-rendered HTML. React then encounters the <script> during a client render, where it never executes, and logs the error.

The theme is still applied on those pages, because ThemeProvider also sets data-theme in an effect, so the script isn't needed in that case.

A possible fix is to render a plain <script> only when the component is server-rendered or hydrated, and render nothing during a fresh client render. For example, use useSyncExternalStore with a server snapshot of true and a client snapshot of false. I'm using this in my project and I'm happy to open a PR if that approach is acceptable.

ecommerce and with-vercel-website use the same InitTheme component.

Link to the code that reproduces this issue

https://github.com/peteruithoven/payload-website-404-issue

Reproduction Steps

The linked repo is an unmodified website template, created with pnpx create-payload-app@latest -t website (SQLite, Payload 3.93.0).

  1. Clone the repo and run pnpm install
  2. cp .env.example .env, then set DATABASE_URL=file:./payload-website-404-issue.db and PAYLOAD_SECRET to any string
  3. pnpm dev
  4. Open http://localhost:3000/this-page-does-not-exist
  5. Open the browser console: the "Encountered a script tag while rendering React component" error is logged.
  6. Open http://localhost:3000/ for comparison: no error.

No seeding is needed; it reproduces with an empty database.

Which area(s) are affected?

area: templates

Environment Info
Payload: 3.93.0
Next.js: 16.4.1
React: 19.2.9
Node.js: 24.22.3
Dominant language
TypeScript
Stars
45.8k
Forks
4.4k
Avg merge
2d 16h
Merged PRs (30d)
251

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from payloadcms/payload

All issues in payloadcms/payload

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.