Skip to content

Commit 2013807

Browse files
authored
fix(site): keep failed route preloads from reloading the page (#62416)
1 parent ec53c3b commit 2013807

4 files changed

Lines changed: 114 additions & 2 deletions

File tree

‎services/site/script/check-client-bundle.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const budgets: Record<string, number> = {
8080
"mini.tsx": 126.3,
8181
"mobile.tsx": 126.3,
8282
"press/index.tsx": 112,
83-
"product/index.tsx": 113.3,
83+
"product/index.tsx": 113.5,
8484
"sdk.tsx": 126.3,
8585
"terminal.tsx": 126.3,
8686
"video-editor/index.tsx": 49,

‎services/site/src/app.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { ogImage } from "~/lib/og"
1515
import { strip } from "~/lib/language"
1616
import { HeadMetaProvider, LiveMeta } from "~/component/head-meta"
1717
import { viewportContent } from "~/lib/viewport"
18+
import { quietPreloads } from "~/lib/route-preloads"
1819
// Before the router, so its popstate listener comes first.
1920
import "~/component/site/popstate"
2021
import { monoFontFaces } from "~/component/site/mono-fonts"
@@ -82,6 +83,10 @@ if (!isServer) {
8283
})
8384
}
8485

86+
// The file routes, with preloads that leave the page alone when they fail (lib/route-preloads.ts). A
87+
// component on both sides, as <FileRoutes /> was, so hydration counts it the same.
88+
const Routes = () => (isServer ? FileRoutes() : quietPreloads(FileRoutes()))
89+
8590
export default function App() {
8691
return (
8792
<Router
@@ -105,7 +110,7 @@ export default function App() {
105110
</LanguageProvider>
106111
)}
107112
>
108-
<FileRoutes />
113+
<Routes />
109114
</Router>
110115
)
111116
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// The parts of SolidStart's file routes (FileRoutes) that preloading uses.
2+
type Route = {
3+
component?: { preload?: () => unknown }
4+
$component?: { import: () => Promise<unknown> }
5+
children?: Route[]
6+
}
7+
8+
const quiet = new WeakSet<object>()
9+
10+
/**
11+
* SolidStart reloads the page when a route's code fails to load (reloadOnRouteChunkError), so a page
12+
* left open across a deploy picks up the new build. The router's preloads (a link hovered, focused or
13+
* touched, or scrolled into view on a touch screen) run the same loader, so one that failed on a
14+
* flaky connection reloaded the page under the reader, and one that Safari or Firefox cancelled as
15+
* the reader followed a link to another page reloaded the page they were leaving instead. Here a
16+
* route's preload loads its code without that reload and hands it to the route once it has loaded;
17+
* one that fails leaves the route as it was, to load (and recover) when it's opened.
18+
*/
19+
export function quietPreloads<T extends Route>(routes: T[]) {
20+
routes.forEach((route) => {
21+
if (route.children) quietPreloads(route.children)
22+
const component = route.component
23+
const load = route.$component?.import
24+
const preload = component?.preload
25+
if (!component || !load || !preload || quiet.has(component)) return
26+
quiet.add(component)
27+
component.preload = () =>
28+
load().then(
29+
() => preload(),
30+
() => undefined,
31+
)
32+
})
33+
return routes
34+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { describe, expect, test } from "bun:test"
2+
import { quietPreloads } from "~/lib/route-preloads"
3+
4+
// A file route as SolidStart builds it: a lazy component whose preload is the reloading loader, and
5+
// the route's own import of its code.
6+
function route(code: () => Promise<unknown>) {
7+
const calls = { preload: 0, import: 0 }
8+
return {
9+
calls,
10+
route: {
11+
component: { preload: () => void calls.preload++ },
12+
$component: {
13+
import: () => {
14+
calls.import++
15+
return code()
16+
},
17+
},
18+
},
19+
}
20+
}
21+
22+
describe("quietPreloads", () => {
23+
test("hands a route its code once the code has loaded", async () => {
24+
const page = route(() => Promise.resolve({}))
25+
quietPreloads([page.route])
26+
27+
await page.route.component.preload()
28+
29+
expect(page.calls).toEqual({ preload: 1, import: 1 })
30+
})
31+
32+
test("leaves the route as it was when its code fails to load", async () => {
33+
const page = route(() => Promise.reject(new TypeError("Failed to fetch dynamically imported module")))
34+
quietPreloads([page.route])
35+
36+
expect(await page.route.component.preload()).toBeUndefined()
37+
expect(page.calls).toEqual({ preload: 0, import: 1 })
38+
})
39+
40+
// quietPreloads relies on the shape SolidStart gives each file route, and skips any route without
41+
// it, so a SolidStart or Solid upgrade that changes it would turn the fix off without a sound. If
42+
// one of these fails, check that preloads still can't reload the page (src/lib/route-preloads.ts),
43+
// then update the expectations.
44+
test("SolidStart's file routes still have the shape it relies on", async () => {
45+
const start = (file: string) =>
46+
Bun.file(new URL(`../node_modules/@solidjs/start/dist/${file}`, import.meta.url)).text()
47+
// The route's own import of its code, unwrapped: `$component: { src, build, import }`.
48+
expect(await start("config/fs-routes/index.js")).toContain("import: `_$() => import('${buildId}')$_`")
49+
const router = await start("router.jsx")
50+
// Each route keeps its `$component`, and its component is a lazy one over the reloading loader.
51+
expect(router).toContain("...route,")
52+
expect(router).toContain("lazy(loadComponent(route))")
53+
expect(router).toContain("withRouteChunkReload(route.$component.import)")
54+
// The router preloads a route through its component's `preload`, which a lazy component has.
55+
expect(await Bun.file(new URL("../node_modules/@solidjs/router/dist/index.js", import.meta.url)).text()).toContain(
56+
"route.component && route.component.preload && route.component.preload()",
57+
)
58+
expect(await Bun.file(new URL("../node_modules/solid-js/dist/solid.js", import.meta.url)).text()).toContain(
59+
"wrap.preload = () =>",
60+
)
61+
})
62+
63+
test("reaches nested routes and wraps each preload once", async () => {
64+
const page = route(() => Promise.resolve({}))
65+
const routes = [{ children: [page.route] }]
66+
quietPreloads(routes)
67+
quietPreloads(routes)
68+
69+
await page.route.component.preload()
70+
71+
expect(page.calls).toEqual({ preload: 1, import: 1 })
72+
})
73+
})

0 commit comments

Comments
 (0)