RFC: Web Worker support in next/script #31517
Replies: 3 comments 2 replies
-
YES. I can't get a higher score due to the |
Beta Was this translation helpful? Give feedback.
-
For this interested - you can use Parytown with Nextjs easily today! More info over here https://github.com/BuilderIO/partytown/wiki/Getting-Started#add-partytown-snippet-to-the-head import Head from "next/head";
import { Partytown } from "@builder.io/partytown/react";
const Home = () => {
return (
<>
<Head>
<title>My App</title>
<Partytown />
<script src="https://example.com/analytics.js" type="text/partytown"></script>
</Head>
<main>...</main>
</>
);
};
export default Home; |
Beta Was this translation helpful? Give feedback.
-
disclaimer: i'm not a pro in this topic, i'm just asking a question Is it possible to execute even the 1st party scripts (those served on I have a SSG deployed website, but lighthouse still "complains" about Or if I can safely ignore those warnings (since i'm using SSG anyway), that'd be cool to know |
Beta Was this translation helpful? Give feedback.
-
This is a proposal to allow Next.js developers to off-load any third-party script to a Web Worker in order to run in a background thread.
Background
Web workers are scripts that run in a background thread independent of those that execute and run directly on the browser’s main thread. Isolating JavaScript that does not populate or interact with the immediate UI of a page to a web worker frees up main thread processing, which can improve page load performance.
The Script component in Next.js simplifies the process of loading third-party scripts in a few ways:
next/head
or be placed in_document.js
strategy
property. There are three strategies that are currently supported:beforeInteractive
: Load script before page becomes interactiveafterInteractive
: Load script immediately after page becomes interactivelazyOnload
: Load script during browser idle timeProposal
Although the Script component supports two strategies (
beforeInteractive
,afterInteractive
) to defer loading of third-party scripts and prevent them from being render-blocking, this proposal suggests the addition of a fourth strategy;worker
.Why?
The following table provides data on the average and median download times of render-blocking, third-party scripts across all Next.js sites grouped by category.
(e.g. Adobe Tag Manager, Google Tag Manager)
(e.g. Google Analytics, Optimizely)
(e.g. Google Maps, Mapbox, Stripe)
(e.g. Google/Doubleclick Ads, Criteo)
(e.g. Google/Doubleclick Ads, Criteo)
(e.g. Mailchimp, Hubspot)
This data shows that a significant number of third-party scripts used in Next.js sites that introduce extrinsic functionality (i.e. not critical to rendering immediate UI) are render-blocking with a non-trivial download time (~1s). The current recommendation is to fetch these types of scripts after the page becomes interactive or during idle time, but this still opens up the possibility of them competing with other resources that load late. For a lot of these scripts, the ideal approach would be to move their processing entirely to a separate thread in order to not compete with any page resources whatsoever, but there is no easy way to do this.
How?
The concept of using web workers to move processing of heavy, non-critical scripts that have been fetched externally is new and highly experimental. Web workers communicate asynchronously with the main thread and cannot access the DOM, where many third-party scripts directly access the DOM in a synchronous manner.
With that being said, there are two different approaches being considered to include experimental web worker support for
next/script
:1. Dynamically import and use PartyTown
PartyTown is a 6 kB library that can isolate resource heavy scripts into a web worker. PartyTown can be ncc’d directly as a dependency in Next.js and lazy-loaded only when a component uses a Script component to move processing of a third-party script into a worker.
Pros:
Cons:
/~partytown/
directory must be present to serve static files. This would would have to be taken care of in the Next.js app static server or via a copy task as a bundler step2. Build a custom web worker solution
Pros:
Cons:
Using web workers to handle processing of third-party scripts is a very experimental technique and not technically possible without extra “magic”. For this reason, the first approach of using PartyTown is the proposed solution. If this feature gains adoption, switching to a custom solution will be considered in the future if that comes with advantages of its own.
The next steps are for myself to quickly prototype embedding PartyTown directly in Next.js, verify its feasibility, and then gather data on how much it can improve sample sites that use many third-party scripts.
Any thoughts? Suggestions? Please share! 💬
Beta Was this translation helpful? Give feedback.
All reactions