Skip to content
TypeScript

fix(web): app loads again after a chunk cycle blanked it at boot (#25295)

Theo Browne04a21bb1c1ce
diff --git a/apps/web/vite.config.ts b/apps/web/vite.config.ts
index 67853de8063..501bac5dba0 100644
--- a/apps/web/vite.config.ts
+++ b/apps/web/vite.config.ts
@@ -221,6 +221,30 @@ function devCompressionPlugin(): Plugin {
};
}
+// A static import cycle between chunks can run a chunk before the values it imports exist,
+// which blanks the app at boot. Fail the build instead of shipping it.
+function chunkCycleGuardPlugin(): Plugin {
+ return {
+ name: "t3code:chunk-cycle-guard",
+ apply: "build",
+ generateBundle(_options, bundle) {
+ const done = new Set<string>();
+ const visit = (fileName: string, path: string[]) => {
+ if (done.has(fileName)) return;
+ const start = path.indexOf(fileName);
+ if (start !== -1) {
+ this.error(`Chunk import cycle: ${[...path.slice(start), fileName].join(" -> ")}`);
+ }
+ const chunk = bundle[fileName];
+ if (chunk?.type !== "chunk") return;
+ for (const imported of chunk.imports) visit(imported, [...path, fileName]);
+ done.add(fileName);
+ };
+ for (const fileName of Object.keys(bundle)) visit(fileName, []);
+ },
+ };
+}
+
// Allow Tailnet hosts for sharing; Tailscale-controlled DNS prevents attacker rebinding.
const configuredAllowedHosts = (process.env.T3CODE_DEV_ALLOWED_HOSTS ?? "")
.split(",")
@@ -242,6 +266,7 @@ export default defineConfig(() => {
assetsInclude: ["**/*.wasm"],
plugins: [
devCompressionPlugin(),
+ chunkCycleGuardPlugin(),
builtDev
? undefined
: thirdPartyLicensesPlugin({
@@ -359,21 +384,6 @@ export default defineConfig(() => {
asset.originalFileNames.some((path) => /[/\\]pdfjs-dist[/\\]wasm[/\\]/.test(path))
? `assets/pdfjs-${pdfjsPkg.version}/[name][extname]`
: "assets/[name]-[hash][extname]",
- codeSplitting: {
- // Preview imports must not fold shared file icons back into main.
- groups: [
- {
- name: "react",
- test: /\/node_modules\/(?:react|react-dom|scheduler)\//,
- includeDependenciesRecursively: false,
- },
- {
- name: "icons",
- test: /\/(?:components\/chat\/PierreEntryIcon\.tsx|pierre-icons\.ts|mediaPreview\.ts)$/,
- includeDependenciesRecursively: false,
- },
- ],
- },
},
},
},