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

Adds web worker support to <Script /> using Partytown #34244

Merged
merged 7 commits into from
Mar 11, 2022
Merged

Adds web worker support to <Script /> using Partytown #34244

merged 7 commits into from
Mar 11, 2022

Conversation

housseindjirdeh
Copy link
Collaborator

@housseindjirdeh housseindjirdeh commented Feb 11, 2022

Summary

This PR adds a new worker strategy to the <Script /> component that automatically relocates and executes the script in a web worker.

<Script 
  strategy="worker"
  ...
/>

Partytown is used under the hood to provide this functionality.

Behavior

  • This will land as an experimental feature and will only work behind an opt-in flag in next.config.js:

    experimental: {
      nextScriptWorkers: true
    }
  • This setup use a similar approach to how ESLint and Typescript is used in Next.js by showing an error to the user to install the dependency locally themselves if they've enabled the experimental nextScriptWorkers flag.

    Screen Shot 2022-03-03 at 2 33 13 PM
  • For Partytown to work, a number of static files must be served directly from the site (see docs). In this PR, these files are automatically copied to a ~partytown directory in .next/static during next build and next dev if the nextScriptWorkers flag is set to true.

Checklist

  • 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.

This PR fixes #31517.

@ijjk ijjk added created-by: Chrome Aurora PRs by the Google Chrome team: https://web.dev/aurora type: next labels Feb 11, 2022
@housseindjirdeh housseindjirdeh marked this pull request as draft February 11, 2022 19:37
@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@housseindjirdeh housseindjirdeh marked this pull request as ready for review February 14, 2022 22:06
@housseindjirdeh housseindjirdeh changed the title WIP: Adds web worker support to <Script> using partytown Adds web worker support to <Script> using Partytown Feb 14, 2022
@housseindjirdeh housseindjirdeh changed the title Adds web worker support to <Script> using Partytown Adds web worker support to <Script /> using Partytown Feb 14, 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.

@housseindjirdeh housseindjirdeh marked this pull request as draft February 18, 2022 21:54
@housseindjirdeh housseindjirdeh changed the title Adds web worker support to <Script /> using Partytown WIP: Adds web worker support to <Script /> using Partytown Feb 18, 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.

@housseindjirdeh housseindjirdeh marked this pull request as ready for review March 3, 2022 20:01
@housseindjirdeh housseindjirdeh changed the title WIP: Adds web worker support to <Script /> using Partytown Adds web worker support to <Script /> using Partytown Mar 3, 2022
docs/basic-features/script.md Outdated Show resolved Hide resolved
@ijjk

This comment has been minimized.

docs/basic-features/script.md Outdated Show resolved Hide resolved
@@ -455,6 +456,7 @@ export const defaultConfig: NextConfig = {
workerThreads: false,
pageEnv: false,
optimizeCss: false,
optimizeScripts: false,
Copy link
Member

Choose a reason for hiding this comment

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

Do we want to call this something more specific to workers e.g. nextScriptWorkers?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

My initial plan was to have a single generic flag that would eventually control multiple experimental next/script features in the future, but maybe that just adds unnecessary confusion?

Also you're not the first person to suggest making it more specific; I just updated it to nextScriptWorkers :)

@ijjk

This comment has been minimized.

docs/basic-features/script.md Outdated Show resolved Hide resolved
docs/basic-features/script.md Outdated Show resolved Hide resolved
@ijjk

This comment has been minimized.

@ijjk
Copy link
Member

ijjk commented Mar 11, 2022

Stats from current PR

Default Build (Increase detected ⚠️)
General Overall increase ⚠️
vercel/next.js canary housseindjirdeh/next.js partytown Change
buildDuration 15.2s 15.1s -136ms
buildDurationCached 6s 6.1s ⚠️ +80ms
nodeModulesSize 378 MB 378 MB ⚠️ +13.9 kB
Page Load Tests Overall decrease ⚠️
vercel/next.js canary housseindjirdeh/next.js partytown Change
/ failed reqs 0 0
/ total time (seconds) 2.916 2.939 ⚠️ +0.02
/ avg req/sec 857.49 850.54 ⚠️ -6.95
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 1.165 1.164 0
/error-in-render avg req/sec 2146.72 2147.86 +1.14
Client Bundles (main, webpack) Overall increase ⚠️
vercel/next.js canary housseindjirdeh/next.js partytown Change
925.HASH.js gzip 179 B 179 B
framework-HASH.js gzip 42 kB 42 kB
main-HASH.js gzip 27.9 kB 27.9 kB ⚠️ +38 B
webpack-HASH.js gzip 1.44 kB 1.44 kB
Overall change 71.6 kB 71.6 kB ⚠️ +38 B
Legacy Client Bundles (polyfills)
vercel/next.js canary housseindjirdeh/next.js partytown Change
polyfills-HASH.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary housseindjirdeh/next.js partytown 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 2.57 kB 2.57 kB
head-HASH.js gzip 351 B 351 B
hooks-HASH.js gzip 920 B 920 B
image-HASH.js gzip 5.09 kB 5.09 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 14.8 kB 14.8 kB
Client Build Manifests
vercel/next.js canary housseindjirdeh/next.js partytown Change
_buildManifest.js gzip 460 B 460 B
Overall change 460 B 460 B
Rendered Page Sizes Overall increase ⚠️
vercel/next.js canary housseindjirdeh/next.js partytown Change
index.html gzip 530 B 531 B ⚠️ +1 B
link.html gzip 544 B 545 B ⚠️ +1 B
withRouter.html gzip 524 B 526 B ⚠️ +2 B
Overall change 1.6 kB 1.6 kB ⚠️ +4 B

Diffs

Diff for main-HASH.js
@@ -3076,6 +3076,9 @@
             }
           }
         }
+        if (strategy === "worker") {
+          el.setAttribute("type", "text/partytown");
+        }
         el.setAttribute("data-nscript", strategy);
         document.body.appendChild(el);
       };
@@ -3141,11 +3144,9 @@
           },
           [props, strategy]
         );
-        if (strategy === "beforeInteractive") {
+        if (strategy === "beforeInteractive" || strategy === "worker") {
           if (updateScripts) {
-            scripts.beforeInteractive = (
-              scripts.beforeInteractive || []
-            ).concat([
+            scripts[strategy] = (scripts[strategy] || []).concat([
               _objectSpread(
                 {
                   src: src,
Diff for index.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-01320fb543a25c12.js"
+      src="/_next/static/chunks/main-ba59be0a2e2e35e6.js"
       defer=""
     ></script>
     <script
Diff for link.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-01320fb543a25c12.js"
+      src="/_next/static/chunks/main-ba59be0a2e2e35e6.js"
       defer=""
     ></script>
     <script
Diff for withRouter.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-01320fb543a25c12.js"
+      src="/_next/static/chunks/main-ba59be0a2e2e35e6.js"
       defer=""
     ></script>
     <script

Default Build with SWC (Increase detected ⚠️)
General Overall increase ⚠️
vercel/next.js canary housseindjirdeh/next.js partytown Change
buildDuration 18.7s 18.9s ⚠️ +196ms
buildDurationCached 6s 6.1s ⚠️ +62ms
nodeModulesSize 378 MB 378 MB ⚠️ +13.9 kB
Page Load Tests Overall increase ✓
vercel/next.js canary housseindjirdeh/next.js partytown Change
/ failed reqs 0 0
/ total time (seconds) 2.911 2.952 ⚠️ +0.04
/ avg req/sec 858.92 846.86 ⚠️ -12.06
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 1.199 1.159 -0.04
/error-in-render avg req/sec 2085.65 2157.75 +72.1
Client Bundles (main, webpack) Overall increase ⚠️
vercel/next.js canary housseindjirdeh/next.js partytown Change
925.HASH.js gzip 178 B 178 B
framework-HASH.js gzip 42.3 kB 42.3 kB
main-HASH.js gzip 28.2 kB 28.3 kB ⚠️ +38 B
webpack-HASH.js gzip 1.45 kB 1.45 kB
Overall change 72.2 kB 72.2 kB ⚠️ +38 B
Legacy Client Bundles (polyfills)
vercel/next.js canary housseindjirdeh/next.js partytown Change
polyfills-HASH.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary housseindjirdeh/next.js partytown 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 324 B 324 B
dynamic-HASH.js gzip 2.56 kB 2.56 kB
head-HASH.js gzip 351 B 351 B
hooks-HASH.js gzip 921 B 921 B
image-HASH.js gzip 5.23 kB 5.23 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 kB 15 kB
Client Build Manifests
vercel/next.js canary housseindjirdeh/next.js partytown Change
_buildManifest.js gzip 458 B 458 B
Overall change 458 B 458 B
Rendered Page Sizes Overall decrease ✓
vercel/next.js canary housseindjirdeh/next.js partytown Change
index.html gzip 531 B 531 B
link.html gzip 546 B 545 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
@@ -3076,6 +3076,9 @@
             }
           }
         }
+        if (strategy === "worker") {
+          el.setAttribute("type", "text/partytown");
+        }
         el.setAttribute("data-nscript", strategy);
         document.body.appendChild(el);
       };
@@ -3141,11 +3144,9 @@
           },
           [props, strategy]
         );
-        if (strategy === "beforeInteractive") {
+        if (strategy === "beforeInteractive" || strategy === "worker") {
           if (updateScripts) {
-            scripts.beforeInteractive = (
-              scripts.beforeInteractive || []
-            ).concat([
+            scripts[strategy] = (scripts[strategy] || []).concat([
               _objectSpread(
                 {
                   src: src,
Diff for index.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-01320fb543a25c12.js"
+      src="/_next/static/chunks/main-ba59be0a2e2e35e6.js"
       defer=""
     ></script>
     <script
Diff for link.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-01320fb543a25c12.js"
+      src="/_next/static/chunks/main-ba59be0a2e2e35e6.js"
       defer=""
     ></script>
     <script
Diff for withRouter.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-01320fb543a25c12.js"
+      src="/_next/static/chunks/main-ba59be0a2e2e35e6.js"
       defer=""
     ></script>
     <script
Commit: efa6939

@kodiakhq kodiakhq bot merged commit 79016b8 into vercel:canary Mar 11, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
created-by: Chrome Aurora PRs by the Google Chrome team: https://web.dev/aurora type: next
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants