Skip to content

Commit

Permalink
fix(i18n): pass search params to fallback (#12547)
Browse files Browse the repository at this point in the history
* fix(i18n): pass search params to fallback

* test(i18n): add test for search param fix

* test(i18n): resolve mantainer comments
  • Loading branch information
mtwilliams-code authored Dec 5, 2024
1 parent dec0305 commit 6b6e18d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/kind-mayflies-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a bug where URL search parameters weren't passed when using the i18n `fallback` feature.
4 changes: 2 additions & 2 deletions packages/astro/src/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
const currentLocale = Astro.currentLocale;
const search = Astro.url.search.toString()
---

<html>
Expand All @@ -8,5 +9,6 @@ const currentLocale = Astro.currentLocale;
</head>
<body>
Oi essa e start: {currentLocale}
Search: {search}
</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
---
const search = Astro.url.search
---
<html>
<head>
<title>Astro</title>
</head>
<body>
Start
Search: {search}
</body>
</html>
18 changes: 17 additions & 1 deletion packages/astro/test/i18n-routing.test.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit 6b6e18d

Please sign in to comment.