Skip to content

Commit 900ec95

Browse files
authored
fix: Correct virtual SSR module invalidation and import side-effect logic (#587)
Follow-up to previous PR (#586) - there were some issues in it for establishing proper module graph linkage for virtual SSR modules. Fixes: * Corrected environment scope for invalidation: Virtual SSR modules must be invalidated in the worker environment, not client, to ensure proper propagation without triggering full reloads. * Handled modules without code (e.g. CSS): CSS and other plugin-handled modules can return undefined from fetchModule(). We now avoid skipping these entirely and still extract specifiers to maintain accurate graph linkage. * Replaced the switch-wrapped `ssrImport()` + return logic with direct inline injection of `import()` side effects. This change restores correct behaviour for some module types (notably CSS), though the exact cause of breakage in the previous version is not yet fully understood.
1 parent 1766a8f commit 900ec95

File tree

3 files changed

+5
-19
lines changed

3 files changed

+5
-19
lines changed

sdk/src/vite/directivesPlugin.mts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { transformClientComponents } from "./transformClientComponents.mjs";
66
import { transformServerFunctions } from "./transformServerFunctions.mjs";
77
import { normalizeModulePath } from "./normalizeModulePath.mjs";
88
import type { ViteDevServer } from "vite";
9-
import { invalidateModule } from "./invalidateModule.mjs";
109

1110
const log = debug("rwsdk:vite:rsc-directives-plugin");
1211
const verboseLog = debug("verbose:rwsdk:vite:rsc-directives-plugin");
@@ -89,8 +88,6 @@ export const directivesPlugin = ({
8988
lookupModule,
9089
id,
9190
);
92-
93-
//invalidateModule(devServer, environment, `virtual:use-${kind}-lookup`);
9491
}
9592
}
9693
};

sdk/src/vite/miniflareHMRPlugin.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export const miniflareHMRPlugin = (givenOptions: {
196196
);
197197

198198
if (virtualSSRModule) {
199-
ctx.server.environments.client.moduleGraph.invalidateModule(
199+
ctx.server.environments.worker.moduleGraph.invalidateModule(
200200
virtualSSRModule,
201201
new Set(),
202202
ctx.timestamp,

sdk/src/vite/ssrBridgePlugin.mts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -148,20 +148,12 @@ export const ssrBridgePlugin = ({
148148

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

151-
if (!result) {
152-
return;
153-
}
154-
155151
const code = "code" in result ? result.code : undefined;
156152
log("Fetched SSR module code length: %d", code?.length || 0);
157153

158-
if (!code) {
159-
return;
160-
}
161-
162154
const { imports, dynamicImports } = findSsrImportSpecifiers(
163155
realId,
164-
code,
156+
code || "",
165157
verboseLog,
166158
);
167159

@@ -176,17 +168,14 @@ export const ssrBridgePlugin = ({
176168

177169
const transformedCode = `
178170
await (async function(__vite_ssr_import__, __vite_ssr_dynamic_import__) {${code}})(
179-
(id, ...args) => ssrImport(id, false, ...args),
180-
(id, ...args) => ssrImport(id, true, ...args)
171+
(id, ...args) => {ssrImport(id); return __vite_ssr_import__('/@id/${VIRTUAL_SSR_PREFIX}' + id, ...args);},
172+
(id, ...args) => {ssrImport(id); return __vite_ssr_dynamic_import__('/@id/${VIRTUAL_SSR_PREFIX}' + id, ...args);}
181173
);
182174
183-
function ssrImport(id, isDynamic, ...args) {
175+
function ssrImport(id) {
184176
switch (id) {
185177
${switchCases}
186178
}
187-
188-
const virtualId = '/@id/${VIRTUAL_SSR_PREFIX}' + id;
189-
return isDynamic ? __vite_ssr_dynamic_import__(virtualId, ...args) : __vite_ssr_import__(virtualId, ...args);
190179
}
191180
`;
192181

0 commit comments

Comments
 (0)