Skip to content

Commit

Permalink
refactor:  minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasio committed Jul 23, 2024
1 parent 3ee5265 commit e9836f9
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions packages/next/src/middlewares/appMidleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export async function AppMiddleware(
options: AppMidlewareOptions = { appRouter: false },
) {
let response = NextResponse.next();
const currentUrl = req.nextUrl.pathname;
const { pathname } = req.nextUrl;

if (isStaticAssetRequest(req) || isInternalRequest(req)) {
return response;
Expand All @@ -109,7 +109,7 @@ export async function AppMiddleware(
: [];

const locale = options.appRouter && appRouterLocale ? appRouterLocale : req.nextUrl.locale;
const urlLocale = req.nextUrl.pathname.split('/')[1];
const urlLocale = pathname.split('/')[1];
const hostname = req.headers.get('host') || '';

// if it's polylang integration, we should not be using locale to get site
Expand All @@ -126,8 +126,6 @@ export async function AppMiddleware(
throw new Error('Site not found.');
}

const { pathname } = req.nextUrl;

if (redirectStrategy === 'always') {
const redirect = await fetchRedirect(pathname, sourceUrl || '');

Expand All @@ -146,7 +144,6 @@ export async function AppMiddleware(
response = NextResponse.redirect(new URL(pathname.replace(`/${locale}`, ''), req.url));
}

// TODO: rework this, need to take into account url locale
if (
locale &&
options.appRouter &&
Expand All @@ -161,20 +158,18 @@ export async function AppMiddleware(
);
}

if (req.nextUrl.pathname.endsWith('/page/1') || req.nextUrl.pathname.endsWith('/page/1/')) {
if (pathname.endsWith('/page/1') || pathname.endsWith('/page/1/')) {
return NextResponse.redirect(req.url.replace('/page/1', ''));
}

if (isMultisiteRequest && !shouldRedirect) {
const url = req.nextUrl;

const pagesRouterRewrite = `/_sites/${hostname}${url.pathname}`;
const pagesRouterRewrite = `/_sites/${hostname}${pathname}`;
const appRouterRewrite = locale
? `/${locale}/${hostname}${url.pathname.replace(`/${locale}`, '')}`
: `/${hostname}${url.pathname}`;
? `/${locale}/${hostname}${pathname.replace(`/${locale}`, '')}`
: `/${hostname}${pathname}`;

response = NextResponse.rewrite(
new URL(options.appRouter ? appRouterRewrite : pagesRouterRewrite, url),
new URL(options.appRouter ? appRouterRewrite : pagesRouterRewrite, req.nextUrl),
);

response.headers.set('x-headstartwp-site', hostname);
Expand All @@ -184,7 +179,7 @@ export async function AppMiddleware(
response.headers.set('x-headstartwp-locale', locale);
}

response.headers.set('x-headstartwp-current-url', currentUrl);
response.headers.set('x-headstartwp-current-url', pathname);

return response;
}

0 comments on commit e9836f9

Please sign in to comment.