Skip to content

Conversation

huozhi
Copy link
Member

@huozhi huozhi commented May 27, 2022

When using next/dynamic with suspense: true, the API will opt into React.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 makes next/dynamic has a significant difference from React.lazy api

x-ref: #37197 (comment)
x-ref: #37244

@ijjk ijjk added created-by: Next.js team PRs by the Next.js team. type: next labels May 27, 2022
@ijjk

This comment was marked as outdated.

@huozhi huozhi marked this pull request as draft May 27, 2022 14:53
@ijjk
Copy link
Member

ijjk commented May 27, 2022

Stats from current PR

Default Build (Decrease detected ✓)
General Overall decrease ✓
vercel/next.js canary huozhi/next.js preload/dynamic Change
buildDuration 14.8s 14.3s -532ms
buildDurationCached 5.6s 6.1s ⚠️ +509ms
nodeModulesSize 626 MB 626 MB -253 B
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 ⚠️ +0.07
/error-in-render avg req/sec 2058.31 1942.84 ⚠️ -115.47
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 ⚠️ +437ms
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 ⚠️ +0.01
/error-in-render avg req/sec 1850.34 1831.01 ⚠️ -19.33
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 ⚠️ +1 B
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 ⚠️ +1 B
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);
Commit: 00d846a

@huozhi huozhi marked this pull request as ready for review May 27, 2022 15:30
@huozhi huozhi requested a review from ijjk May 27, 2022 19:17
@kodiakhq kodiakhq bot merged commit df77964 into vercel:canary May 27, 2022
@huozhi huozhi deleted the preload/dynamic branch May 27, 2022 21:03
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants