-
-
Notifications
You must be signed in to change notification settings - Fork 463
[tabs][slider] Exclude the prehydration script from client bundles #5003
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
michaldudak
wants to merge
9
commits into
mui:master
Choose a base branch
from
michaldudak:tabs-prehydration-script-stub
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0e11b9d
[tabs][slider] Exclude prehydration scripts from client bundles
michaldudak a90c486
[tabs][slider] Encapsulate prehydration script rendering
michaldudak a0c8f41
Merge remote-tracking branch 'upstream/master' into tabs-prehydration…
michaldudak 849178e
Merge branch 'master' into tabs-prehydration-script-stub
michaldudak 472caf8
Install infra from https://github.com/mui/mui-public/pull/1533
michaldudak 28ccc1d
Mention webpack 4 workaround
michaldudak 2de9186
Merge remote-tracking branch 'upstream/master' into tabs-prehydration…
michaldudak 46d7bc2
Rewrite the webpack 4 callout
michaldudak 788f7b5
Dedupe
michaldudak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| 'use client'; | ||
| import * as React from 'react'; | ||
| import { useIsHydrating } from '../utils/useIsHydrating'; | ||
| import { useCSPContext } from './csp-context/CSPContext'; | ||
|
|
||
| /** | ||
| * Renders an inline script that runs before React hydrates, used by components that need | ||
| * to position server-rendered content ahead of hydration (e.g. `Tabs.Indicator`, | ||
| * `Slider.Thumb`). | ||
| * | ||
| * The `script` source is imported by the caller through the package's `#prehydration/*` | ||
| * subpath import, whose `browser` condition resolves to an empty string — so the script | ||
| * body is excluded from client bundles. It only ever executes from server-rendered HTML. | ||
| * | ||
| * Render this only when the script should be emitted (i.e. gate `renderBeforeHydration` | ||
| * and any structural conditions at the call site). The element is still rendered (with | ||
| * empty content) on the client during the hydration pass so the React tree matches the | ||
| * server markup; `suppressHydrationWarning` bridges the content difference and React keeps | ||
| * the already-executed server script. Once `isHydrating` flips to `false` the element | ||
| * unmounts. | ||
| * | ||
| * The component must stay in client bundles: returning `null` on the client (e.g. by | ||
| * stubbing the whole component) would drop an element the server emitted and trigger a | ||
| * recoverable hydration error (React #418) in consumers' apps. Only the script body is | ||
| * excluded from client bundles, via the `#prehydration/*` `browser` condition. | ||
| */ | ||
| export function PrehydrationScript(props: PrehydrationScript.Props) { | ||
| const { script } = props; | ||
| const { nonce } = useCSPContext(); | ||
| const isHydrating = useIsHydrating(); | ||
|
|
||
| if (!isHydrating) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <script | ||
| nonce={nonce} | ||
| // eslint-disable-next-line react/no-danger | ||
| dangerouslySetInnerHTML={{ __html: script }} | ||
| suppressHydrationWarning | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| export namespace PrehydrationScript { | ||
| export interface Props { | ||
| /** | ||
| * The script source, imported through the `#prehydration/*` subpath import. | ||
| * Empty in client bundles. | ||
| */ | ||
| script: string; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // Browser-bundle replacement for `prehydrationScript.min.ts`, selected via the `browser` | ||
| // condition of the `#prehydration/*` entry in package.json `imports`. The real script | ||
| // only ever executes from server-rendered HTML, so client bundles don't need its contents — | ||
| // during hydration the thumb renders the same `<script>` element with empty content, | ||
| // which React adopts without patching. | ||
| export const script = ''; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // Browser-bundle replacement for `prehydrationScript.min.ts`, selected via the `browser` | ||
| // condition of the `#prehydration/*` entry in package.json `imports`. The real script | ||
| // only ever executes from server-rendered HTML, so client bundles don't need its contents — | ||
| // during hydration the indicator renders the same `<script>` element with empty content, | ||
| // which React adopts without patching. | ||
| export const script = ''; |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll look into adding package.json import conditions support.
As a small optimization. Have you considered a
PrehydrationScriptcomponent that encapsulates reading thenoncefrom context as well as theisHydratinglogic? Then you can render it statically inprehydrationScript.min.tsand just anullin this stub.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See mui/mui-public#1533
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that it doesn't support extra conditions like
browserin the imports map ("Unsupported import. Only a string or mui-src object supported")There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking into it. globs are tricky, we're expanding them for codesandbox (I believe, right @brijeshb42 ?).
@michaldudak Did you check whether codesandbox supports conditional imports?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose I could just list all the scripts manually, so it's not a blocker we they're not supported.
It seems it doesn't :/
https://codesandbox.io/p/sandbox/4mjx6h?file=%2Fsrc%2FApp.tsx%3A23%2C4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I'm beginning to think that we should just build and ship our own sandbox environment, which is fast as csb, but not so buggy (or at least debuggable). We don't necessarily need the full Node.js environment that stackblitz supports (for 90% of our use-cases at least)