mirror of
https://github.com/hanzo-docs/docs.git
synced 2026-10-24 20:09:02 +00:00
fix: route _next assets to correct section origin via Referer header
Each federated docs section (IAM, KMS, Platform) builds unique layout and page chunks that only exist on its CF Pages origin. The Worker was routing all /_next/ requests to the main origin, causing 404s for section-specific chunks (e.g. app/docs/layout-*.js), which crashed client-side hydration and broke sidebar navigation. Now the Worker checks the Referer header on /_next/ requests to determine which section the browser is rendering, and routes to that section's origin first, falling back to main if the asset is missing.
This commit is contained in:
@@ -13,6 +13,22 @@ const ROUTES: Route[] = [
|
||||
|
||||
const MAIN_ORIGIN = 'hanzo-docs.pages.dev';
|
||||
|
||||
/**
|
||||
* Determine which section origin a request belongs to based on the Referer header.
|
||||
* When a browser loads /_next/ assets, the Referer is the page URL that triggered the load.
|
||||
*/
|
||||
function originFromReferer(request: Request): string | null {
|
||||
const referer = request.headers.get('Referer');
|
||||
if (!referer) return null;
|
||||
try {
|
||||
const refUrl = new URL(referer);
|
||||
for (const route of ROUTES) {
|
||||
if (refUrl.pathname.startsWith(route.prefix)) return route.origin;
|
||||
}
|
||||
} catch {}
|
||||
return null;
|
||||
}
|
||||
|
||||
export default {
|
||||
async fetch(request: Request): Promise<Response> {
|
||||
const url = new URL(request.url);
|
||||
@@ -32,6 +48,22 @@ export default {
|
||||
}
|
||||
}
|
||||
|
||||
// Static asset routing: /_next/ chunks, CSS, fonts, etc.
|
||||
// Each section build produces unique layout/page chunks that only exist on
|
||||
// that section's CF Pages origin. Use the Referer header to route assets
|
||||
// to the origin that built the page the browser is rendering.
|
||||
if (url.pathname.startsWith('/_next/')) {
|
||||
const sectionOrigin = originFromReferer(request);
|
||||
if (sectionOrigin) {
|
||||
const res = await fetch(
|
||||
new Request(`https://${sectionOrigin}${url.pathname}${url.search}`, request),
|
||||
);
|
||||
if (res.ok) return res;
|
||||
// Asset missing on section origin — fall through to main origin.
|
||||
// This handles shared framework chunks that may only exist on main.
|
||||
}
|
||||
}
|
||||
|
||||
// Default: main docs
|
||||
return fetch(new Request(`https://${MAIN_ORIGIN}${url.pathname}${url.search}`, request));
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user