10up / headstartwp

Build a headless website fast with WordPress, the world’s most popular CMS, and Next.js, the most popular React framework. A free and open source solution by the experts at 10up.
https://headstartwp.10up.com
164 stars 17 forks source link

update currentSite to match 404 and 500 pages #251

Closed tobeycodes closed 2 months ago

tobeycodes commented 1 year ago

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]);
nicholasio commented 1 year ago

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

tobeycodes commented 1 year ago

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

tobeycodes commented 1 year ago
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.

nicholasio commented 1 year ago

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

tobeycodes commented 1 year ago

That is fine with me

steven-tey commented 1 year ago

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

nicholasio commented 2 months ago

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