headstartwp
headstartwp copied to clipboard
update currentSite to match 404 and 500 pages
404 and 500 pages do not sit under the _sites/[site] middleware so do not have access to the query param. The site can be set here with the window.location.host value instead.
const currentSite = useMemo(() => {
if (router.query?.site && !Array.isArray(router.query.site)) {
return getSiteByHost(router.query.site, router.locale);
}
// 404.js and 500.js do not have a site query param.
if (typeof window !== 'undefined') {
return getSiteByHost(window.location.host, router.locale);
}
return {};
}, [router]);
This code would only work client-side. I think we'll eventually need to figure out a proper way to support this within Next.js
https://github.com/vercel/platforms/issues/174
export async function AppMiddleware(req: NextRequest) {
...
if (redirectStrategy === 'always' || notFoundStrategy === 'multisite') {
const source = await fetchSource(pathname, sourceUrl || ''); // rename from fetchRedirect
if (redirectStrategy === 'always' && source.location) {
return NextResponse.redirect(redirect.location, redirect.status);
}
if (notFoundStrategy === 'multisite' && source.status === 404) { // not sure if the key is status or not
const parts = request.nextUrl.pathname.split('/');
return NextResponse.rewrite(
new URL(`/_sites/${parts[2]}/404`, request.url),
);
}
}
...
}
@nicholasio This is pseudo code / not tested but could be an opt in idea.
@tobeycodes interesting idea, feels like more of a workaround but could be a viable option for the pages directory. I think this would be solvable for the app dir/router based on what I've seen though, so maybe we just want to fully solve this as part of app directory support?
That is fine with me
The Platforms Starter Kit now supports custom 404 pages 
With our upcoming release o app router this is now supported inside app router!