Skip to content

Commit 19f53eb

Browse files
benosmacflorian-lefebvreematipico
authored
Fix: [i18n] Dynamic spread route in [locale] folder causes false-positive match when i18n.fallbackType = "rewrite" (#14059)
Co-authored-by: Emanuele Stoppa <[email protected]> Co-authored-by: florian-lefebvre <[email protected]> Co-authored-by: ematipico <[email protected]>
1 parent 42cb647 commit 19f53eb

File tree

5 files changed

+41
-5
lines changed

5 files changed

+41
-5
lines changed

.changeset/rare-lights-camp.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes a bug in i18n implementation, where Astro didn't emit the correct pages when `fallback` is enabled, and a locale uses a catch-all route, e.g. `src/pages/es/[...catchAll].astro`

packages/astro/src/core/build/generate.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
joinPaths,
1515
removeLeadingForwardSlash,
1616
removeTrailingForwardSlash,
17+
trimSlashes,
1718
} from '../../core/path.js';
1819
import { toFallbackType, toRoutingStrategy } from '../../i18n/utils.js';
1920
import { runHookBuildGenerated, toIntegrationResolvedRoute } from '../../integrations/hooks.js';
@@ -525,15 +526,23 @@ async function generatePath(
525526
// Do not render the fallback route if there is already a translated page
526527
// with the same path
527528
if (route.type === 'fallback' && route.pathname !== '/') {
528-
// Get the locale from the pathname
529-
let locale = removeLeadingForwardSlash(pathname).split('/')[0];
530529
if (
531530
Object.values(options.allPages).some((val) => {
532531
if (val.route.pattern.test(pathname)) {
533532
// Check if we've matched a dynamic route
534-
if (val.route.segments && val.route.segments.length !== 0) {
535-
// Check that the route is in a matching locale folder
536-
if (val.route.segments[0][0].content !== locale) return false;
533+
if (val.route.params && val.route.params.length !== 0) {
534+
// Make sure the pathname matches an entry in distURL
535+
if (
536+
val.route.distURL &&
537+
!val.route.distURL.find(
538+
(url) =>
539+
url.href
540+
.replace(config.outDir.toString(), '')
541+
.replace(/(?:\/index\.html|\.html)$/, '') == trimSlashes(pathname),
542+
)
543+
) {
544+
return false;
545+
}
537546
}
538547
// Route matches
539548
return true;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
export async function getStaticPaths() {
3+
return [
4+
{ params: { tags: 'tags/tag-1' } },
5+
];
6+
}
7+
const { tags } = Astro.params;
8+
---
9+
<span id="tag">{tags}</span>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
3+
---
4+
<span id="test">test</span>
5+

packages/astro/test/i18n-routing.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,10 @@ describe('[SSG] i18n routing', () => {
10951095
let html = await fixture.readFile('/es/page/page-1/index.html');
10961096
assert.equal(html.includes('page/page-1'), true);
10971097
});
1098+
it('should rewrite a fallback route when a dynamic spread route exists in the locale folder', async () => {
1099+
let html = await fixture.readFile('/es/test/index.html');
1100+
assert.equal(html.includes('test'), true);
1101+
});
10981102
});
10991103

11001104
describe('i18n routing with fallback rewrite from dynamic route and config.build.format: file', () => {
@@ -1123,6 +1127,10 @@ describe('[SSG] i18n routing', () => {
11231127
let html = await fixture.readFile('/es/page/page-1.html');
11241128
assert.equal(html.includes('page/page-1'), true);
11251129
});
1130+
it('should rewrite a fallback route when a dynamic spread route exists in the locale folder', async () => {
1131+
let html = await fixture.readFile('/es/test.html');
1132+
assert.equal(html.includes('test'), true);
1133+
});
11261134
});
11271135

11281136
describe('i18n routing with fallback rewrite from dynamic route with base', () => {

0 commit comments

Comments
 (0)