Skip to content

Conversation

@marcagba
Copy link

@marcagba marcagba commented Jan 27, 2026

Closes: #27204
Closes: #31447

Additional details

This PR introduces a mechanism to allow developers to manually inject the Cypress bootstrap script by placing a <script data-cy-bootstrap> tag in their HTML (e.g., in layout.tsx for Next.js).

  • Why was this change necessary?
    React frameworks (like Next.js app dir) throw hydration mismatches if the server-rendered HTML structure differs from the client-side structure. Cypress's automatic injection of the bootstrap script into <head> causes this mismatch because the React client bundle doesn't expect that script tag. Allowing developers to manually add the script enable them to leverage the React special suppressHydrationWarning attribute that instruct React to ignore hydration mismatch caused by the bootstrap script.

  • What is affected by this change?

    • packages/proxy: The rewriter logic now looks for the data-cy-bootstrap marker.
    • If found, it injects the bootstrap code into that tag instead of creating a new one.
    • if not found, it fallbacks to the existing behavior ( creating a new <script> tag in <head>).
    • It automatically adds the correct nonce if a CSP is active, as before.
    • It also preserves any standard HTML attributes present (like id or data-*), as before.
  • Implementation details:

    • Modified html function in rewriter.ts to regex match <script ... data-cy-bootstrap ...>.
    • If matched, the inner content is replaced with the bootstrap code, and the nonce attribute is appended if missing.

Steps to test

  1. Next.js App Router:
  2. React Router 7:
  3. Internal Verification:
    • Confirmed fix on a production-grade private React Router 7 application running in framework mode.

How has the user experience changed?

Before: Users on Next.js 13+/React-Router 7 see "Hydration failed" errors in the console during tests.
After: Users can add a specifically marked script tag to their app to eliminate these errors.

PR Tasks

marcagba and others added 2 commits January 13, 2026 15:46
…ration mismatches

Injects Cypress bootstrap code into a developer-provided <script data-cy-bootstrap> tag if present. This allows developers to add attributes like suppressHydrationWarning to the script tag to prevent React hydration mismatches.
@CLAassistant
Copy link

CLAassistant commented Jan 27, 2026

CLA assistant check
All committers have signed the CLA.

@cypress-app-bot
Copy link
Collaborator

@marcagba
Copy link
Author

I consider this a fix, but I understand it could be treated as a feature and can reword it if needed.

@jennifer-shehane
Copy link
Member

Thanks for the contribution Marc! The tests appear to be passing. We do intend to review this and the docs PR. It's a clever idea, we're thinking through if there are scenarios this may cause other issues or not.

@cacieprins cacieprins self-requested a review January 29, 2026 15:58
@cacieprins cacieprins self-assigned this Jan 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hydration failed React Router v7 Hydration errors in Cypress when using React 18, Next, and server components

5 participants