Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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/two-eels-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes an issue where i18n domains would return 404 when `trailingSlash` is set to `never`.
7 changes: 6 additions & 1 deletion packages/astro/src/core/app/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,12 @@ export abstract class BaseApp<P extends Pipeline = AppPipeline> {
pathname = prependForwardSlash(
joinPaths(normalizeTheLocale(locale), this.removeBase(url.pathname)),
);
if (url.pathname.endsWith('/')) {
if (this.manifest.trailingSlash === 'always') {
pathname = appendForwardSlash(pathname);
} else if (this.manifest.trailingSlash === 'never') {
pathname = removeTrailingForwardSlash(pathname);
} else if (url.pathname.endsWith('/')) {
// trailingSlash === 'ignore': preserve the original trailing slash
pathname = appendForwardSlash(pathname);
}
}
Expand Down
92 changes: 89 additions & 3 deletions packages/astro/test/units/i18n/i18n-app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import { createI18nMiddleware } from '../../../dist/i18n/middleware.js';
import { createComponent, render } from '../../../dist/runtime/server/index.js';
import type { Locales } from '../../../dist/types/public/config.js';
import { createPage, createTestApp } from '../mocks.ts';
import { dynamicPart, staticPart } from '../routing/test-helpers.ts';
import { dynamicPart, spreadPart, staticPart } from '../routing/test-helpers.ts';

interface I18nConfigOverrides {
defaultLocale?: string;
locales?: Locales;
strategy?: RoutingStrategies;
fallbackType?: 'redirect' | 'rewrite';
fallback?: Record<string, string>;
domains?: Record<string, string>;
domainLookupTable?: Record<string, string>;
}

function makeI18nConfig(overrides: I18nConfigOverrides = {}) {
Expand All @@ -23,8 +25,8 @@ function makeI18nConfig(overrides: I18nConfigOverrides = {}) {
strategy: overrides.strategy ?? ('pathname-prefix-always' as RoutingStrategies),
fallbackType: overrides.fallbackType ?? ('rewrite' as const),
fallback: 'fallback' in overrides ? overrides.fallback : ({} as Record<string, string>),
domains: {} as Record<string, string>,
domainLookupTable: {} as Record<string, string>,
domains: overrides.domains ?? ({} as Record<string, string>),
domainLookupTable: overrides.domainLookupTable ?? ({} as Record<string, string>),
};
}

Expand Down Expand Up @@ -280,6 +282,90 @@ describe('i18n via App - domains-prefix-always', () => {
});
});

describe('i18n via App - domains-prefix-always with trailingSlash: never', () => {
const i18n = makeI18nConfig({
strategy: 'domains-prefix-always',
locales: ['fi', 'en'],
defaultLocale: 'fi',
domainLookupTable: {
'https://example.com': 'en',
'https://example.fi': 'fi',
},
domains: {
en: 'https://example.com',
fi: 'https://example.fi',
},
});

const middleware = createI18nMiddleware(i18n, '/', 'never', 'directory');

/** Like localeCatchAll but with spread param and trailingSlash: never */
function localeSpreadCatchAll(locale: string) {
return createPage(localePage, {
route: `/${locale}/[...slug]`,
segments: [[staticPart(locale)], [spreadPart('slug')]],
pathname: undefined,
trailingSlash: 'never',
});
}

function createDomainApp() {
return createTestApp([localeSpreadCatchAll('fi'), localeSpreadCatchAll('en')], {
i18n,
trailingSlash: 'never',
middleware: () => ({ onRequest: middleware }),
});
}

it('root path of en domain-mapped locale returns 200 (not 404)', async () => {
const app = createDomainApp();
const res = await app.render(
new Request('https://example.com/', {
headers: { 'X-Forwarded-Host': 'example.com', 'X-Forwarded-Proto': 'https' },
}),
);
assert.equal(res.status, 200);
const $ = cheerio.load(await res.text());
assert.equal($('#locale').text(), 'en');
});

it('non-root path of en domain-mapped locale returns 200', async () => {
const app = createDomainApp();
const res = await app.render(
new Request('https://example.com/about', {
headers: { 'X-Forwarded-Host': 'example.com', 'X-Forwarded-Proto': 'https' },
}),
);
assert.equal(res.status, 200);
const $ = cheerio.load(await res.text());
assert.equal($('#locale').text(), 'en');
});

it('root path of fi domain-mapped locale returns 200 (not 404)', async () => {
const app = createDomainApp();
const res = await app.render(
new Request('https://example.fi/', {
headers: { 'X-Forwarded-Host': 'example.fi', 'X-Forwarded-Proto': 'https' },
}),
);
assert.equal(res.status, 200);
const $ = cheerio.load(await res.text());
assert.equal($('#locale').text(), 'fi');
});

it('non-root path of fi domain-mapped locale returns 200', async () => {
const app = createDomainApp();
const res = await app.render(
new Request('https://example.fi/about', {
headers: { 'X-Forwarded-Host': 'example.fi', 'X-Forwarded-Proto': 'https' },
}),
);
assert.equal(res.status, 200);
const $ = cheerio.load(await res.text());
assert.equal($('#locale').text(), 'fi');
});
});

describe('i18n via App - domains-prefix-other-locales', () => {
const i18n = makeI18nConfig({
strategy: 'domains-prefix-other-locales',
Expand Down
Loading