Skip to content

Commit

Permalink
feat: add query string for locale rewrites in middleware. add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobdubail committed Dec 12, 2024
1 parent c0e3061 commit 4c8ee34
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
10 changes: 10 additions & 0 deletions packages/next/src/middlewares/__tests__/appMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,16 @@ describe('appMiddleware', () => {

res = await AppMiddleware(req, { appRouter: true });
expect(res.headers.get('x-middleware-rewrite')).toBe('http://test.com/en/post-name');

// add default locale with a query string, expecting this to fail
req = new NextRequest('http://test.com/post-name?s=query', {
method: 'GET',
});

res = await AppMiddleware(req, { appRouter: true });
expect(res.headers.get('x-middleware-rewrite')).toBe(
'http://test.com/en/post-name?s=query',
);
});

it('[polylang no locale detection] supports locales with app router', async () => {
Expand Down
15 changes: 9 additions & 6 deletions packages/next/src/middlewares/appMidleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ export async function AppMiddleware(
const site = getSiteByHost(hostname, !hasPolylangIntegration ? locale : undefined);
const isMultisiteRequest = site !== null && typeof site.sourceUrl !== 'undefined';

// ensure we re-add the query string when rewriting/redirecing
const queryString = Array.from(searchParams.keys()).length ? `?${searchParams.toString()}` : '';

const {
redirectStrategy,
sourceUrl,
Expand Down Expand Up @@ -161,7 +164,7 @@ export async function AppMiddleware(
shouldRedirect = true;
const pathNameWithoutLocale = pathname.replace(`/${locale}`, '');
response = NextResponse.redirect(
new URL(pathNameWithoutLocale, req.url.replace(`/${locale}`, '')),
new URL(pathNameWithoutLocale + queryString, req.url.replace(`/${locale}`, '')),
);
}
// if we detected a non-default locale, there isn't a supported locale in the URL already
Expand All @@ -175,14 +178,17 @@ export async function AppMiddleware(
) {
shouldRedirect = true;
response = NextResponse.redirect(
new URL(`/${locale}${pathname.startsWith('/') ? '' : '/'}${pathname}`, req.url),
new URL(
`/${locale}${pathname.startsWith('/') ? '' : '/'}${pathname}${queryString}`,
req.url,
),
);
}
// nothing else and there's not a locale in path then rewrite to add default locale
else if (pathnameIsMissingLocale && !isValidLocale(firstPathSlice)) {
response = NextResponse.rewrite(
new URL(
`/${defaultAppRouterLocale}${pathname.startsWith('/') ? '' : '/'}${pathname}`,
`/${defaultAppRouterLocale}${pathname.startsWith('/') ? '' : '/'}${pathname}${queryString}`,
req.url,
),
);
Expand All @@ -191,9 +197,6 @@ export async function AppMiddleware(

if (isMultisiteRequest && !shouldRedirect) {
const hostNameOrSlug = site.slug || hostname;
const queryString = Array.from(searchParams.keys()).length
? `?${searchParams.toString()}`
: '';
const pagesRouterRewrite = `/_sites/${hostNameOrSlug}${pathname}${queryString}`;
const appRouterRewrite = locale
? `/${locale}/${hostNameOrSlug}${pathname.replace(`/${locale}`, '')}${queryString}`
Expand Down

0 comments on commit 4c8ee34

Please sign in to comment.