diff --git a/tests/fixtures/middleware-i18n/middleware.js b/tests/fixtures/middleware-i18n/middleware.js index 24517d72de..0057764f47 100644 --- a/tests/fixtures/middleware-i18n/middleware.js +++ b/tests/fixtures/middleware-i18n/middleware.js @@ -81,6 +81,11 @@ export async function middleware(request) { return Response.redirect(new URL('/new-home#fragment', url)) } + if (url.locale !== 'en' && url.pathname === '/redirect-to-same-page-but-default-locale') { + url.locale = 'en' + return Response.redirect(url) + } + if (url.pathname.includes('/json')) { return NextResponse.json({ requestUrlPathname: new URL(request.url).pathname, diff --git a/tests/integration/edge-handler.test.ts b/tests/integration/edge-handler.test.ts index 78949d2d6c..f938a1dc12 100644 --- a/tests/integration/edge-handler.test.ts +++ b/tests/integration/edge-handler.test.ts @@ -503,6 +503,30 @@ describe('page router', () => { expect(response.status).toBe(302) }) + test('should support redirects to default locale without changing path', async (ctx) => { + await createFixture('middleware-i18n', ctx) + await runPlugin(ctx) + const origin = await LocalServer.run(async (req, res) => { + res.write( + JSON.stringify({ + url: req.url, + headers: req.headers, + }), + ) + res.end() + }) + ctx.cleanup?.push(() => origin.stop()) + const response = await invokeEdgeFunction(ctx, { + functions: ['___netlify-edge-handler-middleware'], + origin, + url: `/fr/redirect-to-same-page-but-default-locale`, + redirect: 'manual', + }) + const url = new URL(response.headers.get('location') ?? '', 'http://n/') + expect(url.pathname).toBe('/redirect-to-same-page-but-default-locale') + expect(response.status).toBe(302) + }) + test('should preserve locale in request.nextUrl', async (ctx) => { await createFixture('middleware-i18n', ctx) await runPlugin(ctx)