0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00

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

* fix(i18n): pass search params to fallback

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

* test(i18n): resolve mantainer comments
This commit is contained in:
mtwilliams 2024-12-04 22:23:30 -08:00 committed by GitHub
parent dec0305b75
commit 6b6e18d7a0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 30 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes a bug where URL search parameters weren't passed when using the i18n `fallback` feature.

View file

@ -375,9 +375,9 @@ export function redirectToFallback({
} }
if (fallbackType === 'rewrite') { if (fallbackType === 'rewrite') {
return await context.rewrite(newPathname); return await context.rewrite(newPathname + context.url.search);
} else { } else {
return context.redirect(newPathname); return context.redirect(newPathname + context.url.search);
} }
} }
} }

View file

@ -1,5 +1,6 @@
--- ---
const currentLocale = Astro.currentLocale; const currentLocale = Astro.currentLocale;
const search = Astro.url.search.toString()
--- ---
<html> <html>
@ -8,5 +9,6 @@ const currentLocale = Astro.currentLocale;
</head> </head>
<body> <body>
Oi essa e start: {currentLocale} Oi essa e start: {currentLocale}
Search: {search}
</body> </body>
</html> </html>

View file

@ -1,8 +1,12 @@
---
const search = Astro.url.search
---
<html> <html>
<head> <head>
<title>Astro</title> <title>Astro</title>
</head> </head>
<body> <body>
Start Start
Search: {search}
</body> </body>
</html> </html>

View file

@ -1,6 +1,6 @@
import * as cheerio from 'cheerio';
import * as assert from 'node:assert/strict'; import * as assert from 'node:assert/strict';
import { after, afterEach, before, describe, it } from 'node:test'; import { after, afterEach, before, describe, it } from 'node:test';
import * as cheerio from 'cheerio';
import testAdapter from './test-adapter.js'; import testAdapter from './test-adapter.js';
import { loadFixture } from './test-utils.js'; import { loadFixture } from './test-utils.js';
@ -1582,6 +1582,22 @@ describe('[SSR] i18n routing', () => {
assert.equal(response.status, 404); 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]', () => { describe('with routing strategy [pathname-prefix-always]', () => {
before(async () => { before(async () => {
fixture = await loadFixture({ fixture = await loadFixture({