From 0d0572097d2f4ae0863dff356cd2f5d0bce7f5c6 Mon Sep 17 00:00:00 2001 From: mtwilliams Date: Thu, 19 Dec 2024 14:16:27 -0500 Subject: [PATCH 1/8] test(i18n): add failing tests for currentLocale bug --- .../fixtures/i18n-routing-fallback/src/pages/index.astro | 7 +++++++ packages/astro/test/i18n-routing.test.js | 5 ++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/astro/test/fixtures/i18n-routing-fallback/src/pages/index.astro b/packages/astro/test/fixtures/i18n-routing-fallback/src/pages/index.astro index 34b39fcd6747..c2965ac81262 100644 --- a/packages/astro/test/fixtures/i18n-routing-fallback/src/pages/index.astro +++ b/packages/astro/test/fixtures/i18n-routing-fallback/src/pages/index.astro @@ -1,3 +1,6 @@ +--- +const locale = Astro.currentLocale +--- Astro @@ -7,6 +10,10 @@ Hello +

+ + locale - {locale} +

diff --git a/packages/astro/test/i18n-routing.test.js b/packages/astro/test/i18n-routing.test.js index 27491069d37c..7cc259b73208 100644 --- a/packages/astro/test/i18n-routing.test.js +++ b/packages/astro/test/i18n-routing.test.js @@ -1,6 +1,6 @@ +import * as cheerio from 'cheerio'; import * as assert from 'node:assert/strict'; import { after, afterEach, before, describe, it } from 'node:test'; -import * as cheerio from 'cheerio'; import testAdapter from './test-adapter.js'; import { loadFixture } from './test-utils.js'; @@ -2021,6 +2021,7 @@ describe('Fallback rewrite dev server', () => { it('should correctly rewrite to en', async () => { const html = await fixture.fetch('/fr').then((res) => res.text()); assert.match(html, /Hello/); + assert.match(html, /locale - fr/); // assert.fail() }); @@ -2074,6 +2075,7 @@ describe('Fallback rewrite SSG', () => { it('should correctly rewrite to en', async () => { const html = await fixture.readFile('/fr/index.html'); assert.match(html, /Hello/); + assert.match(html, /locale - fr/); // assert.fail() }); @@ -2127,6 +2129,7 @@ describe('Fallback rewrite SSR', () => { const response = await app.render(request); assert.equal(response.status, 200); const html = await response.text(); + assert.match(html, /locale - fr/); assert.match(html, /Hello/); }); From dde15942688b996a4660ff32f815418ab7ae1add Mon Sep 17 00:00:00 2001 From: mtwilliams Date: Thu, 19 Dec 2024 14:16:27 -0500 Subject: [PATCH 2/8] test(i18n): add failing tests for currentLocale bug --- .../fixtures/i18n-routing-fallback/src/pages/index.astro | 7 +++++++ packages/astro/test/i18n-routing.test.js | 5 ++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/astro/test/fixtures/i18n-routing-fallback/src/pages/index.astro b/packages/astro/test/fixtures/i18n-routing-fallback/src/pages/index.astro index 34b39fcd6747..c2965ac81262 100644 --- a/packages/astro/test/fixtures/i18n-routing-fallback/src/pages/index.astro +++ b/packages/astro/test/fixtures/i18n-routing-fallback/src/pages/index.astro @@ -1,3 +1,6 @@ +--- +const locale = Astro.currentLocale +--- Astro @@ -7,6 +10,10 @@ Hello +

+ + locale - {locale} +

diff --git a/packages/astro/test/i18n-routing.test.js b/packages/astro/test/i18n-routing.test.js index 8075d7e52d49..36b91b3b1f93 100644 --- a/packages/astro/test/i18n-routing.test.js +++ b/packages/astro/test/i18n-routing.test.js @@ -1,6 +1,6 @@ +import * as cheerio from 'cheerio'; import * as assert from 'node:assert/strict'; import { after, afterEach, before, describe, it } from 'node:test'; -import * as cheerio from 'cheerio'; import testAdapter from './test-adapter.js'; import { loadFixture } from './test-utils.js'; @@ -2032,6 +2032,7 @@ describe('Fallback rewrite dev server', () => { it('should correctly rewrite to en', async () => { const html = await fixture.fetch('/fr').then((res) => res.text()); assert.match(html, /Hello/); + assert.match(html, /locale - fr/); // assert.fail() }); @@ -2085,6 +2086,7 @@ describe('Fallback rewrite SSG', () => { it('should correctly rewrite to en', async () => { const html = await fixture.readFile('/fr/index.html'); assert.match(html, /Hello/); + assert.match(html, /locale - fr/); // assert.fail() }); @@ -2138,6 +2140,7 @@ describe('Fallback rewrite SSR', () => { const response = await app.render(request); assert.equal(response.status, 200); const html = await response.text(); + assert.match(html, /locale - fr/); assert.match(html, /Hello/); }); From da060ba529585b443fdd7af113f59a1e91a68492 Mon Sep 17 00:00:00 2001 From: mtwilliams Date: Fri, 27 Dec 2024 13:46:13 -0600 Subject: [PATCH 3/8] add failing tests --- packages/astro/test/i18n-routing.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/astro/test/i18n-routing.test.js b/packages/astro/test/i18n-routing.test.js index 36b91b3b1f93..dc338c7db0b7 100644 --- a/packages/astro/test/i18n-routing.test.js +++ b/packages/astro/test/i18n-routing.test.js @@ -1,6 +1,6 @@ -import * as cheerio from 'cheerio'; import * as assert from 'node:assert/strict'; import { after, afterEach, before, describe, it } from 'node:test'; +import * as cheerio from 'cheerio'; import testAdapter from './test-adapter.js'; import { loadFixture } from './test-utils.js'; @@ -2014,13 +2014,13 @@ describe('Fallback rewrite dev server', () => { locales: ['en', 'fr', 'es', 'it', 'pt'], routing: { prefixDefaultLocale: false, + fallbackType: 'rewrite', }, fallback: { fr: 'en', it: 'en', es: 'pt', }, - fallbackType: 'rewrite', }, }); devServer = await fixture.startDevServer(); From ab17c3417884f796a0578714d719350d083f81da Mon Sep 17 00:00:00 2001 From: mtwilliams Date: Fri, 27 Dec 2024 14:10:19 -0600 Subject: [PATCH 4/8] find fallback route for astro.currentLocale --- packages/astro/src/core/render-context.ts | 16 ++++++++++++++-- packages/astro/test/i18n-routing.test.js | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/astro/src/core/render-context.ts b/packages/astro/src/core/render-context.ts index e001461593e2..ba8a0fd2174f 100644 --- a/packages/astro/src/core/render-context.ts +++ b/packages/astro/src/core/render-context.ts @@ -591,8 +591,20 @@ export class RenderContext { computedLocale = computeCurrentLocale(referer, locales, defaultLocale); } } else { - const pathname = - routeData.pathname && !isRoute404or500(routeData) ? routeData.pathname : url.pathname; + // For SSG we match the route naively, for dev we handle fallback on 404, for SSR we find route from fallbackRoutes + const route = + (routeData.pattern.test(url.pathname) + ? routeData + : routeData.fallbackRoutes.find((fallbackRoute) => + fallbackRoute.pattern.test(url.pathname), + )) ?? routeData; + const pathname = route.pathname && !isRoute404or500(route) ? route.pathname : url.pathname; + // console.log('route', route); + // console.table({ + // routePath: routeData.pathname, + // is404Or500: isRoute404or500(routeData), + // urlPathname: url.pathname, + // }); computedLocale = computeCurrentLocale(pathname, locales, defaultLocale); } diff --git a/packages/astro/test/i18n-routing.test.js b/packages/astro/test/i18n-routing.test.js index dc338c7db0b7..9d98a8cf0016 100644 --- a/packages/astro/test/i18n-routing.test.js +++ b/packages/astro/test/i18n-routing.test.js @@ -1,6 +1,6 @@ +import * as cheerio from 'cheerio'; import * as assert from 'node:assert/strict'; import { after, afterEach, before, describe, it } from 'node:test'; -import * as cheerio from 'cheerio'; import testAdapter from './test-adapter.js'; import { loadFixture } from './test-utils.js'; From 048dd65c4a3d8823167247b4f98401778a4dd4d6 Mon Sep 17 00:00:00 2001 From: mtwilliams Date: Fri, 27 Dec 2024 14:12:45 -0600 Subject: [PATCH 5/8] add changeset --- .changeset/happy-pianos-report.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/happy-pianos-report.md diff --git a/.changeset/happy-pianos-report.md b/.changeset/happy-pianos-report.md new file mode 100644 index 000000000000..5e10b02a6671 --- /dev/null +++ b/.changeset/happy-pianos-report.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix Astro.currentLocale returning the incorrect locale when using fallback rewrites in SSR mode From 5cdb8a3e47624d15d17c1185418eef741913972f Mon Sep 17 00:00:00 2001 From: mtwilliams Date: Fri, 27 Dec 2024 14:18:07 -0600 Subject: [PATCH 6/8] remove comments --- packages/astro/src/core/render-context.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/packages/astro/src/core/render-context.ts b/packages/astro/src/core/render-context.ts index ba8a0fd2174f..dd75f101c65f 100644 --- a/packages/astro/src/core/render-context.ts +++ b/packages/astro/src/core/render-context.ts @@ -599,12 +599,6 @@ export class RenderContext { fallbackRoute.pattern.test(url.pathname), )) ?? routeData; const pathname = route.pathname && !isRoute404or500(route) ? route.pathname : url.pathname; - // console.log('route', route); - // console.table({ - // routePath: routeData.pathname, - // is404Or500: isRoute404or500(routeData), - // urlPathname: url.pathname, - // }); computedLocale = computeCurrentLocale(pathname, locales, defaultLocale); } From 92a8906c5730e0248c37e87496881fab04e41961 Mon Sep 17 00:00:00 2001 From: mtwilliams Date: Tue, 7 Jan 2025 15:38:25 -0500 Subject: [PATCH 7/8] chore: dont use ternary --- packages/astro/src/core/render-context.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/astro/src/core/render-context.ts b/packages/astro/src/core/render-context.ts index dd75f101c65f..0d8c4ce2ed40 100644 --- a/packages/astro/src/core/render-context.ts +++ b/packages/astro/src/core/render-context.ts @@ -592,12 +592,14 @@ export class RenderContext { } } else { // For SSG we match the route naively, for dev we handle fallback on 404, for SSR we find route from fallbackRoutes - const route = - (routeData.pattern.test(url.pathname) - ? routeData - : routeData.fallbackRoutes.find((fallbackRoute) => - fallbackRoute.pattern.test(url.pathname), - )) ?? routeData; + let route = routeData; + if (!routeData.pattern.test(url.pathname) && routeData.fallbackRoutes) { + // if + route = + routeData.fallbackRoutes.find((fallbackRoute) => + fallbackRoute.pattern.test(url.pathname), + ) || routeData; + } const pathname = route.pathname && !isRoute404or500(route) ? route.pathname : url.pathname; computedLocale = computeCurrentLocale(pathname, locales, defaultLocale); } From bab3349f8ff2da78db8c72886239231928c92e32 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Fri, 10 Jan 2025 15:03:33 +0000 Subject: [PATCH 8/8] more branching to help readability --- packages/astro/src/core/render-context.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/astro/src/core/render-context.ts b/packages/astro/src/core/render-context.ts index 0d8c4ce2ed40..63b8abcad2b4 100644 --- a/packages/astro/src/core/render-context.ts +++ b/packages/astro/src/core/render-context.ts @@ -592,15 +592,16 @@ export class RenderContext { } } else { // For SSG we match the route naively, for dev we handle fallback on 404, for SSR we find route from fallbackRoutes - let route = routeData; - if (!routeData.pattern.test(url.pathname) && routeData.fallbackRoutes) { - // if - route = - routeData.fallbackRoutes.find((fallbackRoute) => - fallbackRoute.pattern.test(url.pathname), - ) || routeData; + let pathname = routeData.pathname; + if (!routeData.pattern.test(url.pathname)) { + for (const fallbackRoute of routeData.fallbackRoutes) { + if (fallbackRoute.pattern.test(url.pathname)) { + pathname = fallbackRoute.pathname; + break; + } + } } - const pathname = route.pathname && !isRoute404or500(route) ? route.pathname : url.pathname; + pathname = pathname && !isRoute404or500(routeData) ? pathname : url.pathname; computedLocale = computeCurrentLocale(pathname, locales, defaultLocale); }