-
Notifications
You must be signed in to change notification settings - Fork 29.3k
Preload chunks for next/dynamic in suspense mode #37245
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
Merged
Merged
Conversation
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 comment was marked as outdated.
This comment was marked as outdated.
Stats from current PRDefault Build (Decrease detected ✓)General Overall decrease ✓
Page Load Tests Overall decrease
|
vercel/next.js canary | huozhi/next.js preload/dynamic | Change | |
---|---|---|---|
/ failed reqs | 0 | 0 | ✓ |
/ total time (seconds) | 3.55 | 3.517 | -0.03 |
/ avg req/sec | 704.16 | 710.91 | +6.75 |
/error-in-render failed reqs | 0 | 0 | ✓ |
/error-in-render total time (seconds) | 1.215 | 1.287 | |
/error-in-render avg req/sec | 2058.31 | 1942.84 |
Client Bundles (main, webpack)
vercel/next.js canary | huozhi/next.js preload/dynamic | Change | |
---|---|---|---|
925.HASH.js gzip | 179 B | 179 B | ✓ |
framework-HASH.js gzip | 42 kB | 42 kB | ✓ |
main-HASH.js gzip | 29.2 kB | 29.2 kB | ✓ |
webpack-HASH.js gzip | 1.54 kB | 1.54 kB | ✓ |
Overall change | 72.9 kB | 72.9 kB | ✓ |
Legacy Client Bundles (polyfills)
vercel/next.js canary | huozhi/next.js preload/dynamic | Change | |
---|---|---|---|
polyfills-HASH.js gzip | 31 kB | 31 kB | ✓ |
Overall change | 31 kB | 31 kB | ✓ |
Client Pages Overall decrease ✓
vercel/next.js canary | huozhi/next.js preload/dynamic | Change | |
---|---|---|---|
_app-HASH.js gzip | 1.36 kB | 1.36 kB | ✓ |
_error-HASH.js gzip | 193 B | 193 B | ✓ |
amp-HASH.js gzip | 308 B | 308 B | ✓ |
css-HASH.js gzip | 327 B | 327 B | ✓ |
dynamic-HASH.js gzip | 2.71 kB | 2.7 kB | -5 B |
head-HASH.js gzip | 359 B | 359 B | ✓ |
hooks-HASH.js gzip | 920 B | 920 B | ✓ |
image-HASH.js gzip | 5.74 kB | 5.74 kB | ✓ |
index-HASH.js gzip | 263 B | 263 B | ✓ |
link-HASH.js gzip | 2.65 kB | 2.65 kB | ✓ |
routerDirect..HASH.js gzip | 320 B | 320 B | ✓ |
script-HASH.js gzip | 391 B | 391 B | ✓ |
withRouter-HASH.js gzip | 318 B | 318 B | ✓ |
85e02e95b279..7e3.css gzip | 107 B | 107 B | ✓ |
Overall change | 16 kB | 16 kB | -5 B |
Client Build Manifests
vercel/next.js canary | huozhi/next.js preload/dynamic | Change | |
---|---|---|---|
_buildManifest.js gzip | 459 B | 459 B | ✓ |
Overall change | 459 B | 459 B | ✓ |
Rendered Page Sizes
vercel/next.js canary | huozhi/next.js preload/dynamic | Change | |
---|---|---|---|
index.html gzip | 533 B | 533 B | ✓ |
link.html gzip | 547 B | 547 B | ✓ |
withRouter.html gzip | 528 B | 528 B | ✓ |
Overall change | 1.61 kB | 1.61 kB | ✓ |
Diffs
Diff for _buildManifest.js
@@ -8,7 +8,7 @@ self.__BUILD_MANIFEST = {
"static\u002Fchunks\u002Fpages\u002Fcss-436fa6bb26fc4120.js"
],
"/dynamic": [
- "static\u002Fchunks\u002Fpages\u002Fdynamic-4df33a4fcfd9ed63.js"
+ "static\u002Fchunks\u002Fpages\u002Fdynamic-8c21bb05a273a9f1.js"
],
"/head": ["static\u002Fchunks\u002Fpages\u002Fhead-544bb68363445a0e.js"],
"/hooks": ["static\u002Fchunks\u002Fpages\u002Fhooks-c1372eeb4916d32c.js"],
Diff for dynamic-HASH.js
@@ -100,16 +100,12 @@
}
// Support for passing options, eg: dynamic(import('../hello-world'), {loading: () => <p>Loading something</p>})
loadableOptions = _objectSpread({}, loadableOptions, options);
- var suspenseOptions = loadableOptions;
// Error if Fizz rendering is not enabled and `suspense` option is set to true
- if (true && suspenseOptions.suspense) {
+ if (true && loadableOptions.suspense) {
throw new Error(
"Invalid suspense option usage in next/dynamic. Read more: https://nextjs.org/docs/messages/invalid-dynamic-suspense"
);
}
- if (suspenseOptions.suspense) {
- return loadableFn(suspenseOptions);
- }
// coming from build/babel/plugins/react-loadable-plugin.js
if (loadableOptions.loadableGenerated) {
loadableOptions = _objectSpread(
@@ -119,8 +115,12 @@
);
delete loadableOptions.loadableGenerated;
}
- // support for disabling server side rendering, eg: dynamic(import('../hello-world'), {ssr: false})
- if (typeof loadableOptions.ssr === "boolean") {
+ // support for disabling server side rendering, eg: dynamic(import('../hello-world'), {ssr: false}).
+ // skip `ssr` for suspense mode and opt-in React.lazy directly
+ if (
+ typeof loadableOptions.ssr === "boolean" &&
+ !loadableOptions.suspense
+ ) {
if (!loadableOptions.ssr) {
delete loadableOptions.ssr;
return noSSR(loadableFn, loadableOptions);
@@ -308,11 +308,19 @@
}
return subscription.promise();
};
- var LoadableImpl = function LoadableImpl(props, ref) {
+ var useLoadableModule = function useLoadableModule() {
init();
var context = _react.default.useContext(
_loadableContext.LoadableContext
);
+ if (context && Array.isArray(opts.modules)) {
+ opts.modules.forEach(function(moduleName) {
+ context(moduleName);
+ });
+ }
+ };
+ var LoadableImpl = function LoadableImpl(props, ref) {
+ useLoadableModule();
var state = useSyncExternalStore(
subscription.subscribe,
subscription.getCurrentValue,
@@ -327,11 +335,6 @@
},
[]
);
- if (context && Array.isArray(opts.modules)) {
- opts.modules.forEach(function(moduleName) {
- context(moduleName);
- });
- }
return _react.default.useMemo(
function() {
if (state.loading || state.error) {
@@ -355,6 +358,7 @@
);
};
var LazyImpl = function LazyImpl(props, ref) {
+ useLoadableModule();
return _react.default.createElement(
opts.lazy,
_objectSpread({}, props, {
@@ -382,7 +386,7 @@
if (false) {
}
// Client only
- if (!initialized && "object" !== "undefined" && !opts.suspense) {
+ if (!initialized && "object" !== "undefined") {
// require.resolveWeak check is needed for environments that don't have it available like Jest
var moduleIds =
opts.webpack && "function" === "function"
@@ -424,7 +428,7 @@
}
var LoadableComponent = opts.suspense ? LazyImpl : LoadableImpl;
LoadableComponent.preload = function() {
- return !opts.suspense && init();
+ return init();
};
LoadableComponent.displayName = "LoadableComponent";
return _react.default.forwardRef(LoadableComponent);
Default Build with SWC (Decrease detected ✓)
General Overall decrease ✓
vercel/next.js canary | huozhi/next.js preload/dynamic | Change | |
---|---|---|---|
buildDuration | 17.4s | 17.9s | |
buildDurationCached | 6.3s | 6.1s | -273ms |
nodeModulesSize | 626 MB | 626 MB | -253 B |
Page Load Tests Overall increase ✓
vercel/next.js canary | huozhi/next.js preload/dynamic | Change | |
---|---|---|---|
/ failed reqs | 0 | 0 | ✓ |
/ total time (seconds) | 3.878 | 3.754 | -0.12 |
/ avg req/sec | 644.72 | 665.99 | +21.27 |
/error-in-render failed reqs | 0 | 0 | ✓ |
/error-in-render total time (seconds) | 1.351 | 1.365 | |
/error-in-render avg req/sec | 1850.34 | 1831.01 |
Client Bundles (main, webpack)
vercel/next.js canary | huozhi/next.js preload/dynamic | Change | |
---|---|---|---|
925.HASH.js gzip | 178 B | 178 B | ✓ |
framework-HASH.js gzip | 42.7 kB | 42.7 kB | ✓ |
main-HASH.js gzip | 29.7 kB | 29.7 kB | ✓ |
webpack-HASH.js gzip | 1.54 kB | 1.54 kB | ✓ |
Overall change | 74 kB | 74 kB | ✓ |
Legacy Client Bundles (polyfills)
vercel/next.js canary | huozhi/next.js preload/dynamic | Change | |
---|---|---|---|
polyfills-HASH.js gzip | 31 kB | 31 kB | ✓ |
Overall change | 31 kB | 31 kB | ✓ |
Client Pages Overall increase ⚠️
vercel/next.js canary | huozhi/next.js preload/dynamic | Change | |
---|---|---|---|
_app-HASH.js gzip | 1.35 kB | 1.35 kB | ✓ |
_error-HASH.js gzip | 179 B | 179 B | ✓ |
amp-HASH.js gzip | 311 B | 311 B | ✓ |
css-HASH.js gzip | 324 B | 324 B | ✓ |
dynamic-HASH.js gzip | 2.9 kB | 2.9 kB | |
head-HASH.js gzip | 357 B | 357 B | ✓ |
hooks-HASH.js gzip | 920 B | 920 B | ✓ |
image-HASH.js gzip | 5.84 kB | 5.84 kB | ✓ |
index-HASH.js gzip | 261 B | 261 B | ✓ |
link-HASH.js gzip | 2.78 kB | 2.78 kB | ✓ |
routerDirect..HASH.js gzip | 322 B | 322 B | ✓ |
script-HASH.js gzip | 392 B | 392 B | ✓ |
withRouter-HASH.js gzip | 317 B | 317 B | ✓ |
85e02e95b279..7e3.css gzip | 107 B | 107 B | ✓ |
Overall change | 16.4 kB | 16.4 kB |
Client Build Manifests
vercel/next.js canary | huozhi/next.js preload/dynamic | Change | |
---|---|---|---|
_buildManifest.js gzip | 458 B | 458 B | ✓ |
Overall change | 458 B | 458 B | ✓ |
Rendered Page Sizes
vercel/next.js canary | huozhi/next.js preload/dynamic | Change | |
---|---|---|---|
index.html gzip | 532 B | 532 B | ✓ |
link.html gzip | 545 B | 545 B | ✓ |
withRouter.html gzip | 527 B | 527 B | ✓ |
Overall change | 1.6 kB | 1.6 kB | ✓ |
Diffs
Diff for _buildManifest.js
@@ -8,7 +8,7 @@ self.__BUILD_MANIFEST = {
"static\u002Fchunks\u002Fpages\u002Fcss-436fa6bb26fc4120.js"
],
"/dynamic": [
- "static\u002Fchunks\u002Fpages\u002Fdynamic-4df33a4fcfd9ed63.js"
+ "static\u002Fchunks\u002Fpages\u002Fdynamic-8c21bb05a273a9f1.js"
],
"/head": ["static\u002Fchunks\u002Fpages\u002Fhead-544bb68363445a0e.js"],
"/hooks": ["static\u002Fchunks\u002Fpages\u002Fhooks-c1372eeb4916d32c.js"],
Diff for dynamic-HASH.js
@@ -100,16 +100,12 @@
}
// Support for passing options, eg: dynamic(import('../hello-world'), {loading: () => <p>Loading something</p>})
loadableOptions = _objectSpread({}, loadableOptions, options);
- var suspenseOptions = loadableOptions;
// Error if Fizz rendering is not enabled and `suspense` option is set to true
- if (true && suspenseOptions.suspense) {
+ if (true && loadableOptions.suspense) {
throw new Error(
"Invalid suspense option usage in next/dynamic. Read more: https://nextjs.org/docs/messages/invalid-dynamic-suspense"
);
}
- if (suspenseOptions.suspense) {
- return loadableFn(suspenseOptions);
- }
// coming from build/babel/plugins/react-loadable-plugin.js
if (loadableOptions.loadableGenerated) {
loadableOptions = _objectSpread(
@@ -119,8 +115,12 @@
);
delete loadableOptions.loadableGenerated;
}
- // support for disabling server side rendering, eg: dynamic(import('../hello-world'), {ssr: false})
- if (typeof loadableOptions.ssr === "boolean") {
+ // support for disabling server side rendering, eg: dynamic(import('../hello-world'), {ssr: false}).
+ // skip `ssr` for suspense mode and opt-in React.lazy directly
+ if (
+ typeof loadableOptions.ssr === "boolean" &&
+ !loadableOptions.suspense
+ ) {
if (!loadableOptions.ssr) {
delete loadableOptions.ssr;
return noSSR(loadableFn, loadableOptions);
@@ -308,11 +308,19 @@
}
return subscription.promise();
};
- var LoadableImpl = function LoadableImpl(props, ref) {
+ var useLoadableModule = function useLoadableModule() {
init();
var context = _react.default.useContext(
_loadableContext.LoadableContext
);
+ if (context && Array.isArray(opts.modules)) {
+ opts.modules.forEach(function(moduleName) {
+ context(moduleName);
+ });
+ }
+ };
+ var LoadableImpl = function LoadableImpl(props, ref) {
+ useLoadableModule();
var state = useSyncExternalStore(
subscription.subscribe,
subscription.getCurrentValue,
@@ -327,11 +335,6 @@
},
[]
);
- if (context && Array.isArray(opts.modules)) {
- opts.modules.forEach(function(moduleName) {
- context(moduleName);
- });
- }
return _react.default.useMemo(
function() {
if (state.loading || state.error) {
@@ -355,6 +358,7 @@
);
};
var LazyImpl = function LazyImpl(props, ref) {
+ useLoadableModule();
return _react.default.createElement(
opts.lazy,
_objectSpread({}, props, {
@@ -382,7 +386,7 @@
if (false) {
}
// Client only
- if (!initialized && "object" !== "undefined" && !opts.suspense) {
+ if (!initialized && "object" !== "undefined") {
// require.resolveWeak check is needed for environments that don't have it available like Jest
var moduleIds =
opts.webpack && "function" === "function"
@@ -424,7 +428,7 @@
}
var LoadableComponent = opts.suspense ? LazyImpl : LoadableImpl;
LoadableComponent.preload = function() {
- return !opts.suspense && init();
+ return init();
};
LoadableComponent.displayName = "LoadableComponent";
return _react.default.forwardRef(LoadableComponent);
ijjk
reviewed
May 27, 2022
ijjk
approved these changes
May 27, 2022
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
When using
next/dynamic
withsuspense: true
, the API will opt intoReact.lazy
with react 18. But previously it doesn't preload the dynamic chunks. This pr will include the chunks into initial html for faster hydration instead of loading the chunk until the script is executed. This makesnext/dynamic
has a significant difference fromReact.lazy
apix-ref: #37197 (comment)
x-ref: #37244