Skip to content
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

Initial implementation of statically optimized flight data of server component pages #35619

Merged
merged 23 commits into from Apr 1, 2022

Conversation

shuding
Copy link
Member

@shuding shuding commented Mar 26, 2022

Part of #31506 and #34179. This PR ensures that in the nodejs runtime, the flight data is statically stored as a JSON file if possible. Most of the touched code is related to conditions of static/SSG/SSR when runtime and/or RSC is involved.

Bug

  • Related issues linked using fixes #number
  • Integration tests added
  • Errors have helpful link attached, see contributing.md

Feature

  • Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
  • Related issues linked using fixes #number
  • Integration tests added
  • Documentation added
  • Telemetry added. In case of a feature if it's used or not.
  • Errors have helpful link attached, see contributing.md

Documentation / Examples

  • Make sure the linting passes by running yarn lint

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

This comment has been minimized.

@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

Comment on lines +558 to +560
startTransition(() => {
reactRoot.render(reactEl)
})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ijjk

This comment has been minimized.

@shuding shuding marked this pull request as ready for review March 29, 2022 15:50
@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ijjk

This comment was marked as outdated.

@shuding shuding requested a review from huozhi March 31, 2022 23:11
@ijjk

This comment has been minimized.

packages/next/client/index.tsx Show resolved Hide resolved
packages/next/server/render.tsx Show resolved Hide resolved
packages/next/server/render.tsx Outdated Show resolved Hide resolved
@ijjk

This comment has been minimized.

@ijjk
Copy link
Member

ijjk commented Apr 1, 2022

Stats from current PR

Default Build (Decrease detected ✓)
General Overall increase ⚠️
vercel/next.js canary shuding/next.js shu/dced Change
buildDuration 20.3s 20.2s -126ms
buildDurationCached 7.8s 7.6s -181ms
nodeModulesSize 477 MB 477 MB ⚠️ +11 kB
Page Load Tests Overall decrease ⚠️
vercel/next.js canary shuding/next.js shu/dced Change
/ failed reqs 0 0
/ total time (seconds) 4.403 4.401 0
/ avg req/sec 567.79 568.1 +0.31
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 2.115 2.142 ⚠️ +0.03
/error-in-render avg req/sec 1182.01 1167.29 ⚠️ -14.72
Client Bundles (main, webpack) Overall increase ⚠️
vercel/next.js canary shuding/next.js shu/dced Change
925.HASH.js gzip 179 B 179 B
framework-HASH.js gzip 42 kB 42 kB
main-HASH.js gzip 28.4 kB 28.4 kB ⚠️ +4 B
webpack-HASH.js gzip 1.44 kB 1.44 kB
Overall change 72.1 kB 72.1 kB ⚠️ +4 B
Legacy Client Bundles (polyfills)
vercel/next.js canary shuding/next.js shu/dced Change
polyfills-HASH.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary shuding/next.js shu/dced Change
_app-HASH.js gzip 1.36 kB 1.36 kB
_error-HASH.js gzip 192 B 192 B
amp-HASH.js gzip 309 B 309 B
css-HASH.js gzip 327 B 327 B
dynamic-HASH.js gzip 3.05 kB 3.05 kB
head-HASH.js gzip 351 B 351 B
hooks-HASH.js gzip 920 B 920 B
image-HASH.js gzip 5.48 kB 5.48 kB
index-HASH.js gzip 263 B 263 B
link-HASH.js gzip 2.26 kB 2.26 kB
routerDirect..HASH.js gzip 320 B 320 B
script-HASH.js gzip 387 B 387 B
withRouter-HASH.js gzip 319 B 319 B
85e02e95b279..7e3.css gzip 107 B 107 B
Overall change 15.7 kB 15.7 kB
Client Build Manifests
vercel/next.js canary shuding/next.js shu/dced Change
_buildManifest.js gzip 460 B 460 B
Overall change 460 B 460 B
Rendered Page Sizes Overall decrease ✓
vercel/next.js canary shuding/next.js shu/dced Change
index.html gzip 532 B 531 B -1 B
link.html gzip 546 B 545 B -1 B
withRouter.html gzip 526 B 525 B -1 B
Overall change 1.6 kB 1.6 kB -3 B

Diffs

Diff for main-HASH.js
@@ -1177,7 +1177,7 @@
           shouldHydrate ? markHydrateComplete : markRenderComplete
         );
         if (false) {
-          var ReactDOMClient;
+          var startTransition, ReactDOMClient;
         } else {
           // The check for `.hydrate` is there to support React alternatives like preact
           if (shouldHydrate) {
@@ -1318,7 +1318,9 @@
           initialServerDataWriter,
           initialServerDataBuffer,
           encoder,
+          ref1,
           createFromFetch,
+          createFromReadableStream,
           getCacheKey,
           useServerResponse,
           fetchFlight,
@@ -1679,7 +1681,7 @@
               var href = param.href,
                 asPath = param.asPath,
                 ssg = param.ssg,
-                rsc = param.rsc,
+                flight = param.flight,
                 locale = param.locale;
               var _this = this;
               var ref = (0, _parseRelativeUrl).parseRelativeUrl(href),
@@ -1690,7 +1692,7 @@
                 asPathname = ref1.pathname;
               var route = normalizeRoute(hrefPathname);
               var getHrefForSlug = function(path) {
-                if (rsc) {
+                if (flight) {
                   return path + search + (search ? "&" : "?") + "__flight__=1";
                 }
                 var dataRoute = (0, _getAssetPathFromRoute).default(
@@ -5869,10 +5871,10 @@
                     __N_RSC,
                     isValidElementType,
                     dataHref,
+                    useStreamedFlightData,
                     props,
-                    ref,
-                    fresh,
-                    data;
+                    data,
+                    __flight__;
                   return _runtimeJs.default.wrap(
                     function _callee$(_ctx) {
                       while (1)
@@ -5951,6 +5953,8 @@
                               )
                             );
                           case 17:
+                            useStreamedFlightData =
+                              (false || __N_SSP) && __N_RSC;
                             if (__N_SSG || __N_SSP || __N_RSC) {
                               dataHref = _this.pageLoader.getDataHref({
                                 href: (0, _formatUrl).formatWithValidation({
@@ -5959,13 +5963,14 @@
                                 }),
                                 asPath: resolvedAs,
                                 ssg: __N_SSG,
-                                rsc: __N_RSC,
+                                flight: useStreamedFlightData,
                                 locale: locale
                               });
                             }
-                            _ctx.next = 21;
+                            _ctx.next = 22;
                             return _this._getData(function() {
-                              return __N_SSG || __N_SSP
+                              return (__N_SSG || __N_SSP || __N_RSC) &&
+                                !useStreamedFlightData
                                 ? fetchNextData(
                                     dataHref,
                                     _this.isSsr,
@@ -5982,30 +5987,43 @@
                                     defaultLocale: _this.defaultLocale
                                   });
                             });
-                          case 21:
+                          case 22:
                             props = _ctx.sent;
                             if (!__N_RSC) {
-                              _ctx.next = 29;
+                              _ctx.next = 32;
                               break;
                             }
-                            _ctx.next = 25;
+                            if (!useStreamedFlightData) {
+                              _ctx.next = 31;
+                              break;
+                            }
+                            _ctx.next = 27;
                             return _this._getData(function() {
                               return _this._getFlightData(dataHref);
                             });
-                          case 25:
-                            ref = _ctx.sent;
-                            fresh = ref.fresh;
-                            data = ref.data;
+                          case 27:
+                            data = _ctx.sent.data;
                             props.pageProps = Object.assign(props.pageProps, {
-                              __flight_serialized__: data,
-                              __flight_fresh__: fresh
+                              __flight__: data
                             });
-                          case 29:
+                            _ctx.next = 32;
+                            break;
+                          case 31: {
+                            __flight__ = props.__flight__;
+                            props.pageProps = Object.assign(
+                              {},
+                              props.pageProps,
+                              {
+                                __flight__: __flight__
+                              }
+                            );
+                          }
+                          case 32:
                             routeInfo.props = props;
                             _this.components[route] = routeInfo;
                             return _ctx.abrupt("return", routeInfo);
-                          case 34:
-                            _ctx.prev = 34;
+                          case 37:
+                            _ctx.prev = 37;
                             _ctx.t1 = _ctx["catch"](0);
                             return _ctx.abrupt(
                               "return",
@@ -6017,14 +6035,14 @@
                                 routeProps
                               )
                             );
-                          case 37:
+                          case 40:
                           case "end":
                             return _ctx.stop();
                         }
                     },
                     _callee,
                     null,
-                    [[0, 34]]
+                    [[0, 37]]
                   );
                 })
               )();
@@ -6363,7 +6381,6 @@
               return fetchNextData(dataHref, true, true, this.sdc, false).then(
                 function(serialized) {
                   return {
-                    fresh: true,
                     data: serialized
                   };
                 }
Diff for index.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-c0f54c07c3697931.js"
+      src="/_next/static/chunks/main-2413143f15cbe12b.js"
       defer=""
     ></script>
     <script
Diff for link.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-c0f54c07c3697931.js"
+      src="/_next/static/chunks/main-2413143f15cbe12b.js"
       defer=""
     ></script>
     <script
Diff for withRouter.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-c0f54c07c3697931.js"
+      src="/_next/static/chunks/main-2413143f15cbe12b.js"
       defer=""
     ></script>
     <script

Default Build with SWC (Increase detected ⚠️)
General Overall increase ⚠️
vercel/next.js canary shuding/next.js shu/dced Change
buildDuration 23.9s 24.5s ⚠️ +640ms
buildDurationCached 8.1s 7.7s -417ms
nodeModulesSize 477 MB 477 MB ⚠️ +11 kB
Page Load Tests Overall decrease ⚠️
vercel/next.js canary shuding/next.js shu/dced Change
/ failed reqs 0 0
/ total time (seconds) 4.44 4.363 -0.08
/ avg req/sec 563.11 573 +9.89
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 2.08 2.195 ⚠️ +0.11
/error-in-render avg req/sec 1201.94 1139.15 ⚠️ -62.79
Client Bundles (main, webpack) Overall increase ⚠️
vercel/next.js canary shuding/next.js shu/dced Change
925.HASH.js gzip 178 B 178 B
framework-HASH.js gzip 42.3 kB 42.3 kB
main-HASH.js gzip 28.8 kB 28.8 kB ⚠️ +9 B
webpack-HASH.js gzip 1.45 kB 1.45 kB
Overall change 72.7 kB 72.7 kB ⚠️ +9 B
Legacy Client Bundles (polyfills)
vercel/next.js canary shuding/next.js shu/dced Change
polyfills-HASH.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary shuding/next.js shu/dced Change
_app-HASH.js gzip 1.35 kB 1.35 kB
_error-HASH.js gzip 179 B 179 B
amp-HASH.js gzip 313 B 313 B
css-HASH.js gzip 325 B 325 B
dynamic-HASH.js gzip 3.03 kB 3.03 kB
head-HASH.js gzip 351 B 351 B
hooks-HASH.js gzip 921 B 921 B
image-HASH.js gzip 5.59 kB 5.59 kB
index-HASH.js gzip 261 B 261 B
link-HASH.js gzip 2.33 kB 2.33 kB
routerDirect..HASH.js gzip 322 B 322 B
script-HASH.js gzip 388 B 388 B
withRouter-HASH.js gzip 317 B 317 B
85e02e95b279..7e3.css gzip 107 B 107 B
Overall change 15.8 kB 15.8 kB
Client Build Manifests
vercel/next.js canary shuding/next.js shu/dced Change
_buildManifest.js gzip 459 B 459 B
Overall change 459 B 459 B
Rendered Page Sizes Overall increase ⚠️
vercel/next.js canary shuding/next.js shu/dced Change
index.html gzip 532 B 532 B
link.html gzip 545 B 546 B ⚠️ +1 B
withRouter.html gzip 527 B 527 B
Overall change 1.6 kB 1.6 kB ⚠️ +1 B

Diffs

Diff for main-HASH.js
@@ -1177,7 +1177,7 @@
           shouldHydrate ? markHydrateComplete : markRenderComplete
         );
         if (false) {
-          var ReactDOMClient;
+          var startTransition, ReactDOMClient;
         } else {
           // The check for `.hydrate` is there to support React alternatives like preact
           if (shouldHydrate) {
@@ -1318,7 +1318,9 @@
           initialServerDataWriter,
           initialServerDataBuffer,
           encoder,
+          ref1,
           createFromFetch,
+          createFromReadableStream,
           getCacheKey,
           useServerResponse,
           fetchFlight,
@@ -1679,7 +1681,7 @@
               var href = param.href,
                 asPath = param.asPath,
                 ssg = param.ssg,
-                rsc = param.rsc,
+                flight = param.flight,
                 locale = param.locale;
               var _this = this;
               var ref = (0, _parseRelativeUrl).parseRelativeUrl(href),
@@ -1690,7 +1692,7 @@
                 asPathname = ref1.pathname;
               var route = normalizeRoute(hrefPathname);
               var getHrefForSlug = function(path) {
-                if (rsc) {
+                if (flight) {
                   return path + search + (search ? "&" : "?") + "__flight__=1";
                 }
                 var dataRoute = (0, _getAssetPathFromRoute).default(
@@ -5869,10 +5871,10 @@
                     __N_RSC,
                     isValidElementType,
                     dataHref,
+                    useStreamedFlightData,
                     props,
-                    ref,
-                    fresh,
-                    data;
+                    data,
+                    __flight__;
                   return _runtimeJs.default.wrap(
                     function _callee$(_ctx) {
                       while (1)
@@ -5951,6 +5953,8 @@
                               )
                             );
                           case 17:
+                            useStreamedFlightData =
+                              (false || __N_SSP) && __N_RSC;
                             if (__N_SSG || __N_SSP || __N_RSC) {
                               dataHref = _this.pageLoader.getDataHref({
                                 href: (0, _formatUrl).formatWithValidation({
@@ -5959,13 +5963,14 @@
                                 }),
                                 asPath: resolvedAs,
                                 ssg: __N_SSG,
-                                rsc: __N_RSC,
+                                flight: useStreamedFlightData,
                                 locale: locale
                               });
                             }
-                            _ctx.next = 21;
+                            _ctx.next = 22;
                             return _this._getData(function() {
-                              return __N_SSG || __N_SSP
+                              return (__N_SSG || __N_SSP || __N_RSC) &&
+                                !useStreamedFlightData
                                 ? fetchNextData(
                                     dataHref,
                                     _this.isSsr,
@@ -5982,30 +5987,43 @@
                                     defaultLocale: _this.defaultLocale
                                   });
                             });
-                          case 21:
+                          case 22:
                             props = _ctx.sent;
                             if (!__N_RSC) {
-                              _ctx.next = 29;
+                              _ctx.next = 32;
                               break;
                             }
-                            _ctx.next = 25;
+                            if (!useStreamedFlightData) {
+                              _ctx.next = 31;
+                              break;
+                            }
+                            _ctx.next = 27;
                             return _this._getData(function() {
                               return _this._getFlightData(dataHref);
                             });
-                          case 25:
-                            ref = _ctx.sent;
-                            fresh = ref.fresh;
-                            data = ref.data;
+                          case 27:
+                            data = _ctx.sent.data;
                             props.pageProps = Object.assign(props.pageProps, {
-                              __flight_serialized__: data,
-                              __flight_fresh__: fresh
+                              __flight__: data
                             });
-                          case 29:
+                            _ctx.next = 32;
+                            break;
+                          case 31: {
+                            __flight__ = props.__flight__;
+                            props.pageProps = Object.assign(
+                              {},
+                              props.pageProps,
+                              {
+                                __flight__: __flight__
+                              }
+                            );
+                          }
+                          case 32:
                             routeInfo.props = props;
                             _this.components[route] = routeInfo;
                             return _ctx.abrupt("return", routeInfo);
-                          case 34:
-                            _ctx.prev = 34;
+                          case 37:
+                            _ctx.prev = 37;
                             _ctx.t1 = _ctx["catch"](0);
                             return _ctx.abrupt(
                               "return",
@@ -6017,14 +6035,14 @@
                                 routeProps
                               )
                             );
-                          case 37:
+                          case 40:
                           case "end":
                             return _ctx.stop();
                         }
                     },
                     _callee,
                     null,
-                    [[0, 34]]
+                    [[0, 37]]
                   );
                 })
               )();
@@ -6363,7 +6381,6 @@
               return fetchNextData(dataHref, true, true, this.sdc, false).then(
                 function(serialized) {
                   return {
-                    fresh: true,
                     data: serialized
                   };
                 }
Diff for index.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-c0f54c07c3697931.js"
+      src="/_next/static/chunks/main-2413143f15cbe12b.js"
       defer=""
     ></script>
     <script
Diff for link.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-c0f54c07c3697931.js"
+      src="/_next/static/chunks/main-2413143f15cbe12b.js"
       defer=""
     ></script>
     <script
Diff for withRouter.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-c0f54c07c3697931.js"
+      src="/_next/static/chunks/main-2413143f15cbe12b.js"
       defer=""
     ></script>
     <script
Commit: a814c9f

@kodiakhq kodiakhq bot merged commit 0eb9f7e into vercel:canary Apr 1, 2022
@shuding shuding deleted the shu/dced branch April 1, 2022 16:16
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 2, 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.

None yet

3 participants