headstartwp icon indicating copy to clipboard operation
headstartwp copied to clipboard

update currentSite to match 404 and 500 pages

Open tobeycodes opened this issue 2 years ago • 6 comments

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]);

tobeycodes avatar Nov 29 '22 14:11 tobeycodes

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

nicholasio avatar Nov 29 '22 16:11 nicholasio

https://github.com/vercel/platforms/issues/174

tobeycodes avatar Dec 13 '22 09:12 tobeycodes

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 avatar Mar 14 '23 13:03 tobeycodes

@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?

nicholasio avatar Mar 28 '23 15:03 nicholasio

That is fine with me

tobeycodes avatar Mar 28 '23 15:03 tobeycodes

The Platforms Starter Kit now supports custom 404 pages :love-cry:

steven-tey avatar Jul 05 '23 22:07 steven-tey

With our upcoming release o app router this is now supported inside app router!

nicholasio avatar Aug 28 '24 22:08 nicholasio