Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(i18n): update strategy when defining manually astro i18n middleware #11362

Merged
merged 1 commit into from
Jul 1, 2024
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/brave-mayflies-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes an issue where creating manually the i18n middleware could break the logic of the functions of the virtual module `astro:i18n`
2 changes: 2 additions & 0 deletions packages/astro/src/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function requestIs404Or500(request: Request, base = '') {

return url.pathname.startsWith(`${base}/404`) || url.pathname.startsWith(`${base}/500`);
}

// Checks if the pathname has any locale
export function pathHasLocale(path: string, locales: Locales): boolean {
const segments = path.split('/');
Expand Down Expand Up @@ -70,6 +71,7 @@ type GetLocaleAbsoluteUrl = GetLocaleRelativeUrl & {
site: AstroConfig['site'];
isBuild: boolean;
};

/**
* The base URL
*/
Expand Down
6 changes: 4 additions & 2 deletions packages/astro/src/virtual-modules/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as I18nInternals from '../i18n/index.js';
import type { RedirectToFallback } from '../i18n/index.js';
import { toRoutingStrategy } from '../i18n/utils.js';
import type { I18nInternalConfig } from '../i18n/vite-plugin-i18n.js';

export { normalizeTheLocale, toCodes, toPaths } from '../i18n/index.js';

const { trailingSlash, format, site, i18n, isBuild } =
Expand All @@ -19,7 +20,7 @@ const { trailingSlash, format, site, i18n, isBuild } =
const { defaultLocale, locales, domains, fallback, routing } = i18n!;
const base = import.meta.env.BASE_URL;

const strategy = toRoutingStrategy(routing, domains);
let strategy = toRoutingStrategy(routing, domains);

export type GetLocaleOptions = I18nInternals.GetLocaleOptions;

Expand Down Expand Up @@ -372,10 +373,11 @@ export let middleware: (customOptions: NewAstroRoutingConfigWithoutManual) => Mi

if (i18n?.routing === 'manual') {
middleware = (customOptions: NewAstroRoutingConfigWithoutManual) => {
strategy = toRoutingStrategy(customOptions, {});
const manifest: SSRManifest['i18n'] = {
...i18n,
fallback: undefined,
strategy: toRoutingStrategy(customOptions, {}),
strategy,
domainLookupTable: {},
};
return I18nInternals.createMiddleware(manifest, base, trailingSlash, format);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
---
import {getRelativeLocaleUrl} from "astro:i18n";

const customUrl = getRelativeLocaleUrl("en", "/blog/title")
---
<html>
<head>
<title>Astro</title>
</head>
<body>
Hello
Hello <p>{customUrl}</p>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ describe('Dev server manual routing', () => {
const text = await response.text();
assert.equal(text.includes('ABOUT ME'), true);
});

it('should correctly print the relative locale url', async () => {
const response = await fixture.fetch('/en/start');
assert.equal(response.status, 200);
const html = await response.text();
const $ = cheerio.load(html);
assert.equal($('p').text(), '/en/blog/title/');
});
});
//
// // SSG
Expand All @@ -61,6 +69,12 @@ describe('SSG manual routing', () => {
let $ = cheerio.load(html);
assert.equal($('body').text().includes('ABOUT ME'), true);
});

it('should correctly print the relative locale url', async () => {
const html = await fixture.readFile('/en/start/index.html');
const $ = cheerio.load(html);
assert.equal($('p').text(), '/en/blog/title/');
});
});

// // SSR
Expand Down Expand Up @@ -94,4 +108,13 @@ describe('SSR manual routing', () => {
const text = await response.text();
assert.equal(text.includes('ABOUT ME'), true);
});

it('should correctly print the relative locale url', async () => {
let request = new Request('http://example.com/en/start');
let response = await app.render(request);
assert.equal(response.status, 200);
const html = await response.text();
const $ = cheerio.load(html);
assert.equal($('p').text(), '/en/blog/title/');
});
});
Loading