Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: preserve lazy chunk execution order

[._.]/ Adam Eivye2098472d209
diff --git a/client/src/test/viteChunkGroups.test.js b/client/src/test/viteChunkGroups.test.js
index 9feec113b6..5a6b3b8b4e 100644
--- a/client/src/test/viteChunkGroups.test.js
+++ b/client/src/test/viteChunkGroups.test.js
@@ -88,6 +88,12 @@ describe('vite chunk groups', () => {
expect(loaders.includeDependenciesRecursively).toBe(false);
});
+ it('preserves module execution order when a group opts out of recursive capture', () => {
+ const resolved = viteConfig({ command: 'build', mode: 'production' });
+ expect(CHUNK_GROUPS.some((group) => group.includeDependenciesRecursively === false)).toBe(true);
+ expect(resolved.build.rolldownOptions.output.strictExecutionOrder).toBe(true);
+ });
+
it('keeps package names from bleeding across the separator', () => {
// A declared name must match a whole path segment: `react` must not swallow
// `react-redux`. The trailing separator is what enforces that.
diff --git a/client/src/utils/lazyWithReload.js b/client/src/utils/lazyWithReload.js
index 74e7f70a3b..fc4540b2a7 100644
--- a/client/src/utils/lazyWithReload.js
+++ b/client/src/utils/lazyWithReload.js
@@ -31,7 +31,7 @@ export const importWithRetry = (importFn, retriesLeft = MAX_RETRIES) =>
// can see it.
export const lazyWithReload = (importFn) => lazy(() =>
importWithRetry(importFn).catch(err => {
- if (isStaleChunkError(err) && reloadOnceForStaleChunk()) {
+ if (isStaleChunkError(err, { duringImport: true }) && reloadOnceForStaleChunk()) {
return new Promise(() => {});
}
throw err;
diff --git a/client/src/utils/staleChunkReload.js b/client/src/utils/staleChunkReload.js
index 9e3a029371..e70a366687 100644
--- a/client/src/utils/staleChunkReload.js
+++ b/client/src/utils/staleChunkReload.js
@@ -13,12 +13,15 @@ const STALE_CHUNK_PATTERNS = [
'failed to fetch dynamically imported module',
'error loading dynamically imported module',
'importing a module script failed',
- 'mime type',
- // A mixed old/new chunk graph can resolve the module request but provide a
- // stale superclass export, which fails while evaluating the imported module.
- 'superclass is not a constructor'
+ 'mime type'
];
+// These can mean an old/new module graph supplied incompatible runtime exports,
+// but the same messages can come from real application bugs. Match them only
+// while handling a rejected dynamic import, never from global error handlers.
+const MODULE_EVALUATION_PATTERNS = ['superclass is not a constructor'];
+const SAFARI_RUNTIME_EXPORTS = ['useState', 'jsx'];
+
const RELOAD_FLAG = 'portos.staleChunkReloadAttempted';
// The service worker names every cache it owns with this prefix
@@ -32,9 +35,15 @@ const CACHE_PREFIX = 'portos-';
// must never leave the user stuck on the error screen — reload regardless.
const PURGE_TIMEOUT_MS = 1500;
-export const isStaleChunkError = (err) => {
+export const isStaleChunkError = (err, { duringImport = false } = {}) => {
const msg = (err?.message || String(err || '')).toLowerCase();
- return STALE_CHUNK_PATTERNS.some(p => msg.includes(p));
+ if (STALE_CHUNK_PATTERNS.some(p => msg.includes(p))) return true;
+ if (!duringImport) return false;
+
+ return MODULE_EVALUATION_PATTERNS.some((p) => msg.includes(p))
+ || (msg.includes('undefined is not an object')
+ && msg.includes('evaluating')
+ && SAFARI_RUNTIME_EXPORTS.some((name) => msg.includes(`.${name.toLowerCase()}`)));
};
// Anti-loop guard: stash the build id we already attempted a reload for. A
diff --git a/client/src/utils/staleChunkReload.test.js b/client/src/utils/staleChunkReload.test.js
index 4315e9ed91..08a232fe27 100644
--- a/client/src/utils/staleChunkReload.test.js
+++ b/client/src/utils/staleChunkReload.test.js
@@ -59,11 +59,25 @@ describe('isStaleChunkError', () => {
'Failed to fetch dynamically imported module',
'error loading dynamically imported module',
'Expected a JavaScript module but got MIME type text/html',
- 'The superclass is not a constructor.',
])('matches %s', (msg) => {
expect(isStaleChunkError(new Error(msg))).toBe(true);
});
+ it.each([
+ 'The superclass is not a constructor.',
+ "undefined is not an object (evaluating 'A.useState')",
+ "undefined is not an object (evaluating '$.jsx')",
+ ])('treats %s as stale only in dynamic-import recovery', (msg) => {
+ const error = new Error(msg);
+ expect(isStaleChunkError(error)).toBe(false);
+ expect(isStaleChunkError(error, { duringImport: true })).toBe(true);
+ });
+
+ it('does not treat unrelated Safari undefined-object errors as stale imports', () => {
+ const error = new Error("undefined is not an object (evaluating 'A.someValue')");
+ expect(isStaleChunkError(error, { duringImport: true })).toBe(false);
+ });
+
it('is case-insensitive and accepts non-Error values', () => {
expect(isStaleChunkError('IMPORTING A MODULE SCRIPT FAILED')).toBe(true);
});
diff --git a/client/vite.config.js b/client/vite.config.js
index 21425b2d20..32ec55b50a 100644
--- a/client/vite.config.js
+++ b/client/vite.config.js
@@ -233,7 +233,13 @@ export default defineConfig(({ command, mode }) => {
// to derive `test` and by its installed-package test), not a
// rolldown option, so it's the one field left out here.
groups: CHUNK_GROUPS.map(({ packages, ...group }) => group)
- }
+ },
+ // `vendor-three-loaders` disables recursive dependency capture to
+ // keep the loaders out of the main three chunk. Rolldown warns that
+ // this can create invalid execution order across chunks; preserve
+ // source module order so lazy routes don't evaluate against missing
+ // runtime exports.
+ strictExecutionOrder: true,
}
},
// Enable source maps for debugging in production