fix(react-router): remove async/defer attributes during hydration to prevent warnings #6479
+209
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Fixes #6438
When using
asyncordeferattributes on script tags viauseHeadorScripts, React throws a hydration warning even ifsuppressHydrationWarningis used. This is because of how React handles hydration mismatches for specific attributes on script tags during streaming/suspense boundaries.This PR solves the root cause by stripping
async,defer, andsrcattributes from the script tag on the client-side only (during hydration).<script src="..." async />to ensure the browser loads it.<script />(viaAsset.tsx) that matches the server's hydration expectation for a controlled script tag, effectively preventing the mismatch warning.Solution
The
Assetcomponent now explicitly destructures and removes these attributes when!router.isServer:Verification
New tests added to cover these scenarios:
script-duplication.spec.ts): Verifies that no hydration warnings are logged to the console when loading a page withasyncscripts.Scripts.test.tsx): Verifies that the server output correctly includesasync/deferattributes (using regex to be resilient to attribute ordering).Summary by CodeRabbit
New Features
/async-scriptsroute demonstrating asynchronous and deferred script loading.Bug Fixes
asyncanddeferattributes without hydration warnings.Tests
✏️ Tip: You can customize this high-level summary in your review settings.