From 72b2f34e6f2b73e8ec732d4d1fdaf7e880307a39 Mon Sep 17 00:00:00 2001 From: LorisSigrist Date: Mon, 8 Jul 2024 14:52:05 +0200 Subject: [PATCH 1/2] Preserve query parameters when redirecting --- .changeset/breezy-forks-check.md | 11 +++++++++++ .../example/src/routes/redirect/+page.server.ts | 9 +++++++++ .../example/src/routes/redirect/+page.svelte | 3 +++ .../src/runtime/hooks/handle.server.ts | 6 +++++- 4 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 .changeset/breezy-forks-check.md create mode 100644 inlang/source-code/paraglide/paraglide-sveltekit/example/src/routes/redirect/+page.server.ts create mode 100644 inlang/source-code/paraglide/paraglide-sveltekit/example/src/routes/redirect/+page.svelte diff --git a/.changeset/breezy-forks-check.md b/.changeset/breezy-forks-check.md new file mode 100644 index 0000000000..fced09cdc9 --- /dev/null +++ b/.changeset/breezy-forks-check.md @@ -0,0 +1,11 @@ +--- +"@inlang/paraglide-sveltekit": patch +--- + +fix: Preserve query parameters when redirecting ([inlang-paraglide-js#168](https://github.com/opral/inlang-paraglide-js/issues/168)) + +`i18n.handle` redirects requests if the pathname does not fit the detected language. Previously this would remove any query parameters from the URL. This is no longer the case. + +```ts +redirect(303, "/login?from=/home") // will be redirected to //login?from=/home +``` \ No newline at end of file diff --git a/inlang/source-code/paraglide/paraglide-sveltekit/example/src/routes/redirect/+page.server.ts b/inlang/source-code/paraglide/paraglide-sveltekit/example/src/routes/redirect/+page.server.ts new file mode 100644 index 0000000000..13d994b98d --- /dev/null +++ b/inlang/source-code/paraglide/paraglide-sveltekit/example/src/routes/redirect/+page.server.ts @@ -0,0 +1,9 @@ +import { redirect } from "@sveltejs/kit" + +export const prerender = false + +export const actions = { + redirect: async () => { + redirect(303, "/?redirected=true") + }, +} diff --git a/inlang/source-code/paraglide/paraglide-sveltekit/example/src/routes/redirect/+page.svelte b/inlang/source-code/paraglide/paraglide-sveltekit/example/src/routes/redirect/+page.svelte new file mode 100644 index 0000000000..53589995a0 --- /dev/null +++ b/inlang/source-code/paraglide/paraglide-sveltekit/example/src/routes/redirect/+page.svelte @@ -0,0 +1,3 @@ +
+ +
\ No newline at end of file diff --git a/inlang/source-code/paraglide/paraglide-sveltekit/src/runtime/hooks/handle.server.ts b/inlang/source-code/paraglide/paraglide-sveltekit/src/runtime/hooks/handle.server.ts index c0b2a04411..45d5435ed8 100644 --- a/inlang/source-code/paraglide/paraglide-sveltekit/src/runtime/hooks/handle.server.ts +++ b/inlang/source-code/paraglide/paraglide-sveltekit/src/runtime/hooks/handle.server.ts @@ -113,13 +113,17 @@ export const createHandle = ( if (lang !== langFromUrl && !i18n.exclude(localisedPath)) { // redirect to the correct language + const localisedPathname = strategy.getLocalisedPath(localisedPath, lang) const fullPath = serializeRoute(localisedPathname, base, suffix) + const url = new URL(event.url) + url.pathname = fullPath + return new Response(undefined, { status: 302, headers: { - Location: fullPath, + Location: url.href, }, }) } From 65d1be30c4cb8af0cee6fa4f2b17225e10e953d4 Mon Sep 17 00:00:00 2001 From: LorisSigrist Date: Mon, 8 Jul 2024 15:05:04 +0200 Subject: [PATCH 2/2] fix prerendering --- .../src/runtime/hooks/handle.server.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/inlang/source-code/paraglide/paraglide-sveltekit/src/runtime/hooks/handle.server.ts b/inlang/source-code/paraglide/paraglide-sveltekit/src/runtime/hooks/handle.server.ts index 45d5435ed8..d60719c557 100644 --- a/inlang/source-code/paraglide/paraglide-sveltekit/src/runtime/hooks/handle.server.ts +++ b/inlang/source-code/paraglide/paraglide-sveltekit/src/runtime/hooks/handle.server.ts @@ -8,6 +8,7 @@ import type { I18nConfig } from "../adapter.server.js" import type { RoutingStrategy } from "../strategy.js" import type { ParaglideLocals } from "../locals.js" import { ALSContext, GlobalContext, type Context } from "./utils.js" +import { getHrefBetween } from "../utils/diff-urls.js" /** * The default lang attribute string that's in SvelteKit's `src/app.html` file. @@ -117,13 +118,15 @@ export const createHandle = ( const localisedPathname = strategy.getLocalisedPath(localisedPath, lang) const fullPath = serializeRoute(localisedPathname, base, suffix) - const url = new URL(event.url) - url.pathname = fullPath + const to = new URL(event.url) + to.pathname = fullPath + + const href = getHrefBetween(event.url, to) return new Response(undefined, { status: 302, headers: { - Location: url.href, + Location: href, }, }) }