Skip to content

Commit

Permalink
fix(i18n): localised index pages are overwritten (#10250)
Browse files Browse the repository at this point in the history
* fix(i18n): localised index pages are overwritten

* chore: add changeset

* chore: add pnpm lock
  • Loading branch information
log101 authored Feb 28, 2024
1 parent 8ae5d99 commit 57655a9
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-pianos-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes the overwriting of localised index pages with redirects
19 changes: 13 additions & 6 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,6 @@ async function getPathsForRoute(
if (route.pathname) {
paths.push(route.pathname);
builtPaths.add(route.pathname);
for (const virtualRoute of route.fallbackRoutes) {
if (virtualRoute.pathname) {
paths.push(virtualRoute.pathname);
builtPaths.add(virtualRoute.pathname);
}
}
} else {
const staticPaths = await callGetStaticPaths({
mod,
Expand Down Expand Up @@ -485,6 +479,19 @@ async function generatePath(
addPageName(pathname, options);
}

// Do not render the fallback route if there is already a translated page
// with the same path
if (
route.type === 'fallback' &&
// If route is index page, continue rendering. The index page should
// always be rendered
route.pathname !== '/' &&
// Check if there is a translated page with the same path
Object.values(options.allPages).some((val) => pathname.match(val.route.pattern))
) {
return;
}

const url = getUrlForPath(
pathname,
config.base,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { defineConfig } from "astro/config";

export default defineConfig({
base: "new-site",
i18n: {
defaultLocale: 'en',
locales: [
'en', 'pt', 'it'
],
fallback: {
"it": "en",
pt: "en"
}
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/i18n-routing-fallback-index",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<html>
<head>
<title>Astro</title>
<script>
console.log("this is a script")
</script>
</head>
<body>
Hello
</body>
</html>


Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
const currentLocale = Astro.currentLocale;
---

<html>
<head>
<title>Astro</title>
</head>
<body>
Oi essa e index: {currentLocale}
</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@test/i18n-routing-fallabck",
"name": "@test/i18n-routing-fallback",
"version": "0.0.0",
"private": true,
"dependencies": {
Expand Down
31 changes: 31 additions & 0 deletions packages/astro/test/i18n-routing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,37 @@ describe('[SSG] i18n routing', () => {
let $ = cheerio.load(html);
assert.equal($('script').text().includes('console.log("this is a script")'), true);
});

describe('with localised index pages', () => {
before(async () => {
fixture = await loadFixture({
root: './fixtures/i18n-routing-fallback-index/',
i18n: {
defaultLocale: 'en',
locales: [
'en',
'pt',
'it',
{
path: 'spanish',
codes: ['es', 'es-AR'],
},
],
fallback: {
it: 'en',
spanish: 'en',
},
},
});
await fixture.build();
});

it('should render correctly', async () => {
let html = await fixture.readFile('/pt/index.html');
let $ = cheerio.load(html);
assert.equal($('body').text().includes('Oi essa e index'), true);
});
})
});

describe('i18n routing with fallback and [pathname-prefix-always]', () => {
Expand Down
154 changes: 154 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 57655a9

Please sign in to comment.