mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-18 15:28:48 +03:00
fix(site): keep failed route preloads from reloading the page (#62416)
This commit is contained in:
@@ -80,7 +80,7 @@ const budgets: Record<string, number> = {
|
||||
"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,
|
||||
|
||||
@@ -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 <FileRoutes /> was, so hydration counts it the same.
|
||||
const Routes = () => (isServer ? FileRoutes() : quietPreloads(FileRoutes()))
|
||||
|
||||
export default function App() {
|
||||
return (
|
||||
<Router
|
||||
@@ -105,7 +110,7 @@ export default function App() {
|
||||
</LanguageProvider>
|
||||
)}
|
||||
>
|
||||
<FileRoutes />
|
||||
<Routes />
|
||||
</Router>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
// The parts of SolidStart's file routes (FileRoutes) that preloading uses.
|
||||
type Route = {
|
||||
component?: { preload?: () => unknown }
|
||||
$component?: { import: () => Promise<unknown> }
|
||||
children?: Route[]
|
||||
}
|
||||
|
||||
const quiet = new WeakSet<object>()
|
||||
|
||||
/**
|
||||
* 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<T extends Route>(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
|
||||
}
|
||||
@@ -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<unknown>) {
|
||||
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 })
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user