Skip to content

Commit c83196d

Browse files
committed
Fix client edits
1 parent de62f82 commit c83196d

File tree

3 files changed

+739
-690
lines changed

3 files changed

+739
-690
lines changed

src/components/MDX/Sandpack/sandpack-rsc/sandbox-code/src/rsc-client.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,14 @@ export function initClient() {
165165
}
166166

167167
function triggerRender() {
168+
// Close any in-flight streams from previous renders
169+
Object.keys(chunkControllers).forEach(function (id) {
170+
try {
171+
chunkControllers[id].close();
172+
} catch (e) {}
173+
delete chunkControllers[id];
174+
});
175+
168176
renderRequestId++;
169177
var reqId = renderRequestId;
170178

@@ -255,6 +263,11 @@ export function initClient() {
255263
// Evaluate compiled client modules and register them in the webpack cache
256264
// so RSDW client can resolve them via __webpack_require__.
257265
function registerClientModules(compiledClients, clientEntries) {
266+
// Clear stale client modules from previous deploys
267+
Object.keys(globalThis.__webpack_module_cache__).forEach(function (key) {
268+
delete globalThis.__webpack_module_cache__[key];
269+
});
270+
258271
// Store all compiled code for lazy evaluation
259272
var allCompiled = compiledClients;
260273

@@ -269,7 +282,8 @@ export function initClient() {
269282

270283
var mod = {exports: {}};
271284
// Register before evaluating to handle circular deps
272-
globalThis.__webpack_module_cache__[moduleId] = {exports: mod.exports};
285+
var cacheEntry = {exports: mod.exports};
286+
globalThis.__webpack_module_cache__[moduleId] = cacheEntry;
273287

274288
var clientRequire = function (id) {
275289
if (id === 'react') return React;
@@ -304,8 +318,8 @@ export function initClient() {
304318
console.error('Error executing client module ' + moduleId + ':', err);
305319
return mod.exports;
306320
}
307-
// Update cache with actual exports (handles non-circular case)
308-
globalThis.__webpack_module_cache__[moduleId] = {exports: mod.exports};
321+
// Update the SAME cache entry's exports (don't replace the wrapper)
322+
cacheEntry.exports = mod.exports;
309323
return mod.exports;
310324
}
311325

src/components/MDX/Sandpack/sandpack-rsc/sandbox-code/src/rsc-server.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ function resolvePath(from, to) {
9292
// Deploy new server code into the Worker
9393
// Receives raw source files — compiles them with Sucrase before execution.
9494
function deploy(files) {
95+
serverActionsRegistry = {};
96+
9597
// Build a require function for the server module scope
9698
var modules = {
9799
react: React,
@@ -318,9 +320,17 @@ self.onmessage = function (e) {
318320
} else if (msg.type === 'render') {
319321
try {
320322
var streamPromise = render();
321-
Promise.resolve(streamPromise).then(function (stream) {
322-
sendStream(msg.requestId, stream);
323-
});
323+
Promise.resolve(streamPromise)
324+
.then(function (stream) {
325+
sendStream(msg.requestId, stream);
326+
})
327+
.catch(function (err) {
328+
self.postMessage({
329+
type: 'rsc-error',
330+
requestId: msg.requestId,
331+
error: String(err),
332+
});
333+
});
324334
} catch (err) {
325335
self.postMessage({
326336
type: 'rsc-error',
@@ -330,9 +340,17 @@ self.onmessage = function (e) {
330340
}
331341
} else if (msg.type === 'callAction') {
332342
try {
333-
callAction(msg.actionId, msg.encodedArgs).then(function (stream) {
334-
sendStream(msg.requestId, stream);
335-
});
343+
callAction(msg.actionId, msg.encodedArgs)
344+
.then(function (stream) {
345+
sendStream(msg.requestId, stream);
346+
})
347+
.catch(function (err) {
348+
self.postMessage({
349+
type: 'rsc-error',
350+
requestId: msg.requestId,
351+
error: String(err),
352+
});
353+
});
336354
} catch (err) {
337355
self.postMessage({
338356
type: 'rsc-error',

0 commit comments

Comments
 (0)