Skip to content

Commit 5e71b91

Browse files
isvictoriousclaude
andcommitted
Fix: Wrap starlightRoute access in try-catch for prerender safety
Handle cases where starlightRoute is not available during prerender of non-Starlight pages (e.g., 404 page). Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
1 parent 298c069 commit 5e71b91

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

src/starlight-route-middleware.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ export const onRequest = defineRouteMiddleware(async (context, next) => {
1010
// so the sidebar has its final entries before we flatten single-child groups.
1111
await next();
1212

13-
const route = context.locals.starlightRoute;
14-
if (route?.sidebar?.length) {
15-
route.sidebar = flattenSidebarSingleChildGroups(route.sidebar);
13+
try {
14+
const route = context.locals.starlightRoute;
15+
if (route?.sidebar?.length) {
16+
route.sidebar = flattenSidebarSingleChildGroups(route.sidebar);
17+
}
18+
} catch {
19+
// starlightRoute may not be available during prerender of non-Starlight pages (e.g., 404)
1620
}
1721
});

0 commit comments

Comments
 (0)