Skip to content

Optimize SSR module warmup #508

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sdk/src/runtime/worker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react";
import { transformRscToHtmlStream } from "./render/transformRscToHtmlStream";
import { renderToRscStream } from "./render/renderToRscStream";

import { ssrLoadModule, ssrWebpackRequire } from "rwsdk/__ssr_bridge";
import { ssrWebpackRequire } from "rwsdk/__ssr_bridge";
import { rscActionHandler } from "./register/worker";
import { injectRSCPayload } from "rsc-html-stream/server";
import { ErrorResponse } from "./error";
Expand Down
63 changes: 48 additions & 15 deletions sdk/src/vite/ssrBridgePlugin.mts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,9 @@ export const ssrBridgePlugin = (): Plugin => {

let devServer: ViteDevServer;
let isDev = false;
let promisedSSRWarmup: Promise<void> | undefined;
const ssrBareImportPromises = new Map<string, Promise<any>>();

const ensureWarmupSSRModules = async () => {
if (promisedSSRWarmup) {
log("SSR warmup already in progress");
return promisedSSRWarmup;
}

promisedSSRWarmup = doWarmupSSRModules();
return promisedSSRWarmup;
};

const doWarmupSSRModules = async () => {
const startSSRModulesWarmup = async () => {
log("Warming up SSR modules");

const files = [
Expand Down Expand Up @@ -56,6 +46,7 @@ export const ssrBridgePlugin = (): Plugin => {
async configureServer(server) {
devServer = server;
log("Configured dev server");
startSSRModulesWarmup();
},
config(_, { command, isPreview }) {
isDev = !isPreview && command === "serve";
Expand Down Expand Up @@ -166,17 +157,46 @@ export const ssrBridgePlugin = (): Plugin => {
id.startsWith(VIRTUAL_SSR_PREFIX) &&
this.environment.name === "worker"
) {
await ensureWarmupSSRModules();
const realId = id.slice(VIRTUAL_SSR_PREFIX.length);
const realId = id.slice(VIRTUAL_SSR_PREFIX.length).split("?")[0];
log("Virtual SSR module load: id=%s, realId=%s", id, realId);

if (isDev) {
log(
"Dev mode: warming up and fetching SSR module for realPath=%s",
"Dev mode: warming up and fetching SSR module for realId=%s",
realId,
);

let deferredLoad: PromiseWithResolvers<any> | undefined;

// context(justinvdm, 09 Jun 2025): For bare imports, we need to avoid
// race conditions occuring because of optimize deps being called concurrently
// for the same dependency. So if we are trying to load a bare import, we first make sure
// any existing loads for that import have completed
if (isBareImport(realId)) {
let promise = ssrBareImportPromises.get(realId);

if (promise) {
log(
"Bare import promise already exists for realId=%s, waiting for it",
realId,
);
await promise;
} else {
log(
"Bare import promise does not exist for realId=%s, creating it",
realId,
);
deferredLoad = Promise.withResolvers();
ssrBareImportPromises.set(realId, deferredLoad.promise);
}
}

const result = await devServer?.environments.ssr.fetchModule(realId);

if (deferredLoad) {
deferredLoad.resolve(undefined);
}

verboseLog("Fetch module result: id=%s, result=%O", realId, result);

if (!result) {
Expand Down Expand Up @@ -225,3 +245,16 @@ const invalidateModule = (
verboseLog("Module not found: id=%s, environment=%s", id, environment);
}
};

const isBareImport = (id: string) => {
// A bare import is one that doesn't start with '.', '..', '/', or a protocol
// Examples: 'react', 'lodash', '@babel/core'
return (
!id.startsWith("./") &&
!id.startsWith("../") &&
!id.startsWith("/") &&
!id.includes("://") &&
!id.startsWith("virtual:") &&
!id.startsWith("node:")
);
};
Loading