Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(i18n): pass search params to fallback #12547

Merged
merged 5 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading