diff --git a/.changeset/kind-mayflies-argue.md b/.changeset/kind-mayflies-argue.md new file mode 100644 index 0000000000..8b5704ad40 --- /dev/null +++ b/.changeset/kind-mayflies-argue.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes a bug where URL search parameters weren't passed when using the i18n `fallback` feature. diff --git a/packages/astro/src/i18n/index.ts b/packages/astro/src/i18n/index.ts index af08db4083..455e2e1414 100644 --- a/packages/astro/src/i18n/index.ts +++ b/packages/astro/src/i18n/index.ts @@ -375,9 +375,9 @@ export function redirectToFallback({ } if (fallbackType === 'rewrite') { - return await context.rewrite(newPathname); + return await context.rewrite(newPathname + context.url.search); } else { - return context.redirect(newPathname); + return context.redirect(newPathname + context.url.search); } } } diff --git a/packages/astro/test/fixtures/i18n-routing-fallback/src/pages/pt/start.astro b/packages/astro/test/fixtures/i18n-routing-fallback/src/pages/pt/start.astro index e0ae5bb5fc..efab575112 100644 --- a/packages/astro/test/fixtures/i18n-routing-fallback/src/pages/pt/start.astro +++ b/packages/astro/test/fixtures/i18n-routing-fallback/src/pages/pt/start.astro @@ -1,5 +1,6 @@ --- const currentLocale = Astro.currentLocale; +const search = Astro.url.search.toString() --- @@ -8,5 +9,6 @@ const currentLocale = Astro.currentLocale; Oi essa e start: {currentLocale} +Search: {search} diff --git a/packages/astro/test/fixtures/i18n-routing-fallback/src/pages/start.astro b/packages/astro/test/fixtures/i18n-routing-fallback/src/pages/start.astro index 990baecd9a..13cdaa2741 100644 --- a/packages/astro/test/fixtures/i18n-routing-fallback/src/pages/start.astro +++ b/packages/astro/test/fixtures/i18n-routing-fallback/src/pages/start.astro @@ -1,8 +1,12 @@ +--- +const search = Astro.url.search +--- Astro Start +Search: {search} diff --git a/packages/astro/test/i18n-routing.test.js b/packages/astro/test/i18n-routing.test.js index 2acef6b9f3..abea9d6e4a 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'; @@ -1582,6 +1582,22 @@ describe('[SSR] i18n routing', () => { assert.equal(response.status, 404); }); + it('should pass search to render when using requested locale', async () => { + let request = new Request('http://example.com/new-site/pt/start?search=1'); + let response = await app.render(request); + assert.equal(response.status, 200); + const text = await response.text(); + assert.match(text, /Oi essa e start/); + assert.match(text, /search=1/); + }); + + it('should include search on the redirect when using fallback', async () => { + let request = new Request('http://example.com/new-site/it/start?search=1'); + let response = await app.render(request); + assert.equal(response.status, 302); + assert.equal(response.headers.get('location'), '/new-site/start?search=1'); + }); + describe('with routing strategy [pathname-prefix-always]', () => { before(async () => { fixture = await loadFixture({