Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
🐛 Load web app chunks from the asset host in CORS mode (#3601)
Luke Vellab0380cd5c9ee
diff --git a/apps/web/next.config.ts b/apps/web/next.config.tsindex 79729cfa9..abc568324 100644--- a/apps/web/next.config.ts+++ b/apps/web/next.config.ts@@ -33,6 +33,13 @@ const nextConfig: NextConfig = {// bundles off Vercel's bandwidth bill. Unset everywhere else.assetPrefix:process.env.NEXT_PUBLIC_ASSET_PREFIX ?? process.env.NEXT_PUBLIC_BASE_URL,+ // Loads chunks from that host as CORS requests instead of opaque no-cors+ // ones. Since the move, WebKit has kept broken cached copies of the opaque+ // chunks, which stops the page from hydrating. The asset host must send+ // Access-Control-Allow-Origin on every response, or no browser will run+ // the scripts. Without a prefix the chunks come from the base URL, which+ // sends no such header, so an instance opened on another host would break.+ crossOrigin: process.env.NEXT_PUBLIC_ASSET_PREFIX ? "anonymous" : undefined,webpack(config) {config.module.rules.push({test: /\.svg$/,diff --git a/apps/web/src/lib/chunk-recovery.test.ts b/apps/web/src/lib/chunk-recovery.test.tsindex d82f4fb1a..56b01744d 100644--- a/apps/web/src/lib/chunk-recovery.test.ts+++ b/apps/web/src/lib/chunk-recovery.test.ts@@ -90,21 +90,41 @@ describe("reloadOnceForChunkError", () => {afterEach(() => {vi.useRealTimers();vi.unstubAllGlobals();+ vi.unstubAllEnvs();vi.restoreAllMocks();reload.mockReset();fetch.mockReset();});it("refetches static assets past the cache before reloading", async () => {+ vi.stubEnv("NEXT_PUBLIC_ASSET_PREFIX", "");reloadOnceForChunkError(factoryError);await vi.runAllTimersAsync();expect(fetch).toHaveBeenCalledWith(chunk,- expect.objectContaining({ cache: "reload", mode: "no-cors" }),+ expect.objectContaining({+ cache: "reload",+ mode: "no-cors",+ credentials: "include",+ }),);expect(reload).toHaveBeenCalledTimes(1);});+ it("refetches in CORS mode when chunks come from an asset host", async () => {+ vi.stubEnv("NEXT_PUBLIC_ASSET_PREFIX", "https://static.rallly.co");+ reloadOnceForChunkError(factoryError);+ await vi.runAllTimersAsync();+ expect(fetch).toHaveBeenCalledWith(+ chunk,+ expect.objectContaining({+ cache: "reload",+ mode: "cors",+ credentials: "same-origin",+ }),+ );+ });+it("still reloads when a refetch fails", async () => {fetch.mockRejectedValue(new TypeError("Load failed"));reloadOnceForChunkError(factoryError);diff --git a/apps/web/src/lib/chunk-recovery.ts b/apps/web/src/lib/chunk-recovery.tsindex 3ae66ce59..363e53c66 100644--- a/apps/web/src/lib/chunk-recovery.ts+++ b/apps/web/src/lib/chunk-recovery.ts@@ -104,18 +104,25 @@ export function staticAssetUrls(entries: Pick<PerformanceEntry, "name">[]) {}// cache: "reload" skips the HTTP cache and stores the fresh response, which-// overwrites a bad copy. Chunk script tags carry no crossorigin attribute, so-// the request mirrors theirs (no-cors, credentials included) to hit the same-// cache entry.+// overwrites a bad copy. The request mirrors the chunk script tags to hit the+// same cache entry: crossorigin="anonymous" (cors, same-origin credentials)+// when an asset host is set, no attribute (no-cors, credentials included)+// otherwise. See crossOrigin in next.config.ts.+function refetchRequestMode(): Pick<RequestInit, "mode" | "credentials"> {+ return process.env.NEXT_PUBLIC_ASSET_PREFIX+ ? { mode: "cors", credentials: "same-origin" }+ : { mode: "no-cors", credentials: "include" };+}+async function refetchStaticAssets(urls: string[]) {const controller = new AbortController();const timeout = setTimeout(() => controller.abort(), REFETCH_TIMEOUT_MS);+ const requestMode = refetchRequestMode();await Promise.allSettled(urls.map((url) =>fetch(url, {cache: "reload",- mode: "no-cors",- credentials: "include",+ ...requestMode,signal: controller.signal,}),),