Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-i18n-redirect-header.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes `redirectToDefaultLocale` not working after the Advanced Routing refactoring. The internal `ROUTE_TYPE_HEADER` was being deleted by middleware finalization before the i18n handler could read it, preventing locale redirects and other i18n post-processing from being applied.
4 changes: 0 additions & 4 deletions packages/astro/src/core/middleware/astro-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { FetchState } from '../fetch/fetch-state.js';
import type { RewritePayload } from '../../types/public/common.js';
import type { APIContext } from '../../types/public/context.js';
import { type Pipeline, PipelineFeatures } from '../base-pipeline.js';
import { ROUTE_TYPE_HEADER } from '../constants.js';
import { attachCookiesToResponse } from '../cookies/index.js';
import { applyRewriteToState } from '../rewrites/handler.js';
import { callMiddleware } from './callMiddleware.js';
Expand Down Expand Up @@ -77,9 +76,6 @@ export class AstroMiddleware {
}

#finalize(state: FetchState, response: Response): Response {
if (response.headers.get(ROUTE_TYPE_HEADER)) {
response.headers.delete(ROUTE_TYPE_HEADER);
}
Comment thread
ematipico marked this conversation as resolved.
Resolved
// LEGACY: we put cookies on the response object,
// where the adapter might be expecting to read it.
// New code should be using `app.render({ addCookieHeader: true })` instead.
Expand Down
37 changes: 37 additions & 0 deletions packages/astro/test/units/i18n/i18n-app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,3 +537,40 @@ describe('i18n via App - domain with localhost and ports (#12385)', () => {
assert.equal($('#locale').text(), 'en');
});
});

describe('i18n via AstroHandler (no middleware) - prefix-always (#16800)', () => {
// This tests the handler-based i18n flow where I18n.finalize() runs
// as a post-processing step after AstroMiddleware, NOT as an internal
// middleware. This is the default code path for non-manual routing.
const i18n = makeI18nConfig({ strategy: 'pathname-prefix-always' });

function createHandlerApp() {
// Do NOT pass middleware — this forces i18n to be handled by
// AstroHandler's post-processing step (I18n.finalize), not by
// a middleware in the chain.
return createTestApp([localeCatchAll('en'), localeCatchAll('fr')], {
i18n,
});
}

it('redirects root / to /en/ via handler post-processing', async () => {
const app = createHandlerApp();
const res = await app.render(new Request('http://example.com/'));
assert.equal(res.status, 302);
assert.ok(res.headers.get('Location')?.includes('/en'));
});

it('renders locale-prefixed pages normally', async () => {
const app = createHandlerApp();
const res = await app.render(new Request('http://example.com/en/about'));
assert.equal(res.status, 200);
const $ = cheerio.load(await res.text());
assert.equal($('#locale').text(), 'en');
});

it('returns 404 for paths without locale prefix', async () => {
const app = createHandlerApp();
const res = await app.render(new Request('http://example.com/about'));
assert.equal(res.status, 404);
});
});
Loading