feat: add "use worker" directive for server Worker Threads and client Web Workers#312
Open
feat: add "use worker" directive for server Worker Threads and client Web Workers#312
Conversation
❌ 1 Tests Failed:
View the top 1 failed test(s) by shortest run time
To view more test analytics, go to the Test Analytics Dashboard |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This PR introduces the
"use worker"directive — a new framework-level primitive that lets developers offload computation to Node.js Worker Threads on the server and Web Workers in the browser using a single, unified API.Features
"use worker"directive"use worker"directive at the top of any module to mark all its exports as worker functions.node:worker_threadsor the browserWorkerAPI automatically.RSC Flight protocol serialization
use()), and ReadableStreams.ReadableStreamvalues are transferred (zero-copy) between threads viapostMessagetransfer lists.Server-side Worker Threads
AsyncLocalStorage) provides access to an abort signal for cancellation.Client-side Web Workers
"use worker"directive works in client components.after()APIafter()server API (exported from@lazarv/react-server) that schedules callbacks to run after the response has been sent to the client.Build & Dev integration
react-server:use-worker) handles module transformation, virtual module resolution, and worker entry generation for both dev and production builds.ModuleRunnerinside workers for HMR-compatible execution.Flight streaming improvements
toStream/fromStreamhelpers in@lazarv/react-server/rsc(andrsc/browser) to support streaming serialization of complex React trees, Promises, and ReadableStreams across thread boundaries.flight-streaming.test.mjs) covering primitives, nested objects, React elements, Suspense, error propagation, streams, and concurrent calls.New files
packages/react-server/lib/plugins/use-worker.mjs"use worker"directivepackages/react-server/server/worker-proxy.mjspackages/react-server/server/worker-proxy-edge.mjspackages/react-server/server/worker-context.mjsAsyncLocalStoragecontext for workerspackages/react-server/client/worker-proxy.mjspackages/react-server/server/after.mjsafter()API implementationpackages/react-server/lib/dev/worker.mjspackages/react-server/lib/start/worker.mjsdocs/src/pages/en/(pages)/framework/worker.mdxexamples/use-worker/test/__test__/apps/use-worker.spec.mjspackages/rsc/__tests__/flight-streaming.test.mjsModified files
packages/rsc/client/shared.mjs/packages/rsc/server/shared.mjs— Enhanced Flight streaming helpers (toStream,fromStream) for cross-thread serialization.packages/react-server/server/index.mjs/index.d.ts— Export newafter()andloggerAPIs.packages/react-server/server/symbols.mjs— New runtime symbols for worker context.packages/react-server/server/runtime.mjs— Runtime support forafter()callbacks.packages/react-server/server/render-rsc.jsx/request.mjs— Wire upafter()execution post-response.packages/react-server/lib/build/server.mjs/client.mjs— Include worker plugin in build pipeline.packages/react-server/lib/dev/create-server.mjs— Dev server worker thread setup.packages/react-server/lib/start/create-server.mjs— Production server worker setup.packages/react-server/lib/http/middleware.mjs—after()callback execution in HTTP middleware.packages/react-server/client/ClientProvider.jsx/error-overlay.mjs— Client-side worker integration.docs/— Updated framework docs index and added worker documentation page.