From 2013807ee0d6b108b08d369ff2e72b7a1e289575 Mon Sep 17 00:00:00 2001 From: Adam <2363879+adamdotdevin@users.noreply.github.com> Date: Fri, 6 Nov 2026 14:08:20 -0600 Subject: [PATCH] fix(site): keep failed route preloads from reloading the page (#62416) --- services/site/script/check-client-bundle.ts | 2 +- services/site/src/app.tsx | 7 +- services/site/src/lib/route-preloads.ts | 34 ++++++++++ services/site/test/route-preloads.test.ts | 73 +++++++++++++++++++++ 4 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 services/site/src/lib/route-preloads.ts create mode 100644 services/site/test/route-preloads.test.ts diff --git a/services/site/script/check-client-bundle.ts b/services/site/script/check-client-bundle.ts index 865e8f2c722..72ec8cad347 100644 --- a/services/site/script/check-client-bundle.ts +++ b/services/site/script/check-client-bundle.ts @@ -80,7 +80,7 @@ const budgets: Record = { "mini.tsx": 126.3, "mobile.tsx": 126.3, "press/index.tsx": 112, - "product/index.tsx": 113.3, + "product/index.tsx": 113.5, "sdk.tsx": 126.3, "terminal.tsx": 126.3, "video-editor/index.tsx": 49, diff --git a/services/site/src/app.tsx b/services/site/src/app.tsx index f4e0e79938b..177b8518964 100644 --- a/services/site/src/app.tsx +++ b/services/site/src/app.tsx @@ -15,6 +15,7 @@ import { ogImage } from "~/lib/og" import { strip } from "~/lib/language" import { HeadMetaProvider, LiveMeta } from "~/component/head-meta" import { viewportContent } from "~/lib/viewport" +import { quietPreloads } from "~/lib/route-preloads" // Before the router, so its popstate listener comes first. import "~/component/site/popstate" import { monoFontFaces } from "~/component/site/mono-fonts" @@ -82,6 +83,10 @@ if (!isServer) { }) } +// The file routes, with preloads that leave the page alone when they fail (lib/route-preloads.ts). A +// component on both sides, as was, so hydration counts it the same. +const Routes = () => (isServer ? FileRoutes() : quietPreloads(FileRoutes())) + export default function App() { return ( )} > - + ) } diff --git a/services/site/src/lib/route-preloads.ts b/services/site/src/lib/route-preloads.ts new file mode 100644 index 00000000000..ae926583b59 --- /dev/null +++ b/services/site/src/lib/route-preloads.ts @@ -0,0 +1,34 @@ +// The parts of SolidStart's file routes (FileRoutes) that preloading uses. +type Route = { + component?: { preload?: () => unknown } + $component?: { import: () => Promise } + children?: Route[] +} + +const quiet = new WeakSet() + +/** + * SolidStart reloads the page when a route's code fails to load (reloadOnRouteChunkError), so a page + * left open across a deploy picks up the new build. The router's preloads (a link hovered, focused or + * touched, or scrolled into view on a touch screen) run the same loader, so one that failed on a + * flaky connection reloaded the page under the reader, and one that Safari or Firefox cancelled as + * the reader followed a link to another page reloaded the page they were leaving instead. Here a + * route's preload loads its code without that reload and hands it to the route once it has loaded; + * one that fails leaves the route as it was, to load (and recover) when it's opened. + */ +export function quietPreloads(routes: T[]) { + routes.forEach((route) => { + if (route.children) quietPreloads(route.children) + const component = route.component + const load = route.$component?.import + const preload = component?.preload + if (!component || !load || !preload || quiet.has(component)) return + quiet.add(component) + component.preload = () => + load().then( + () => preload(), + () => undefined, + ) + }) + return routes +} diff --git a/services/site/test/route-preloads.test.ts b/services/site/test/route-preloads.test.ts new file mode 100644 index 00000000000..5c68fa6ad8c --- /dev/null +++ b/services/site/test/route-preloads.test.ts @@ -0,0 +1,73 @@ +import { describe, expect, test } from "bun:test" +import { quietPreloads } from "~/lib/route-preloads" + +// A file route as SolidStart builds it: a lazy component whose preload is the reloading loader, and +// the route's own import of its code. +function route(code: () => Promise) { + const calls = { preload: 0, import: 0 } + return { + calls, + route: { + component: { preload: () => void calls.preload++ }, + $component: { + import: () => { + calls.import++ + return code() + }, + }, + }, + } +} + +describe("quietPreloads", () => { + test("hands a route its code once the code has loaded", async () => { + const page = route(() => Promise.resolve({})) + quietPreloads([page.route]) + + await page.route.component.preload() + + expect(page.calls).toEqual({ preload: 1, import: 1 }) + }) + + test("leaves the route as it was when its code fails to load", async () => { + const page = route(() => Promise.reject(new TypeError("Failed to fetch dynamically imported module"))) + quietPreloads([page.route]) + + expect(await page.route.component.preload()).toBeUndefined() + expect(page.calls).toEqual({ preload: 0, import: 1 }) + }) + + // quietPreloads relies on the shape SolidStart gives each file route, and skips any route without + // it, so a SolidStart or Solid upgrade that changes it would turn the fix off without a sound. If + // one of these fails, check that preloads still can't reload the page (src/lib/route-preloads.ts), + // then update the expectations. + test("SolidStart's file routes still have the shape it relies on", async () => { + const start = (file: string) => + Bun.file(new URL(`../node_modules/@solidjs/start/dist/${file}`, import.meta.url)).text() + // The route's own import of its code, unwrapped: `$component: { src, build, import }`. + expect(await start("config/fs-routes/index.js")).toContain("import: `_$() => import('${buildId}')$_`") + const router = await start("router.jsx") + // Each route keeps its `$component`, and its component is a lazy one over the reloading loader. + expect(router).toContain("...route,") + expect(router).toContain("lazy(loadComponent(route))") + expect(router).toContain("withRouteChunkReload(route.$component.import)") + // The router preloads a route through its component's `preload`, which a lazy component has. + expect(await Bun.file(new URL("../node_modules/@solidjs/router/dist/index.js", import.meta.url)).text()).toContain( + "route.component && route.component.preload && route.component.preload()", + ) + expect(await Bun.file(new URL("../node_modules/solid-js/dist/solid.js", import.meta.url)).text()).toContain( + "wrap.preload = () =>", + ) + }) + + test("reaches nested routes and wraps each preload once", async () => { + const page = route(() => Promise.resolve({})) + const routes = [{ children: [page.route] }] + quietPreloads(routes) + quietPreloads(routes) + + await page.route.component.preload() + + expect(page.calls).toEqual({ preload: 1, import: 1 }) + }) +})