Creating a redirect query param - how to handle proper URL encoding? #576
-
I have a route for my auth flow that has multiple query params for managing state, and a redirect param for where to go upon successfully authenticating:
When the user gets pushed to this route, the query param looks like this: which correctly lands me on
but then as soon as I call
Is there a way to work around this? I essentially just want to have the URL perform no encoding/decoding on my behalf. I'd like to throw it into the URL already encoded, and then when I pull it from the URL I can decode it manually. Is this currently possible? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
The thing is anything in the URL is automatically decoded when reading from I ran an experiment using your example and the value is indeed shown un-encoded in the URL, but is correctly interpreted when read back (either via Screen.Recording.2024-06-18.at.21.14.43.mov// page.tsx
'use client'
import Link from 'next/link'
import { parseAsStringLiteral, useQueryState } from 'nuqs'
import { Suspense } from 'react'
export default function Page() {
return (
<>
<Link
href={`/app/repro-576?redirect=${encodeURIComponent('/from?a=b&c=/')}`}
>
Apply redirect
</Link>
<Suspense>
<Client />
</Suspense>
</>
)
}
const signInSteps = ['email', 'password']
function Client() {
const [, setStep] = useQueryState(
'step',
parseAsStringLiteral(signInSteps).withDefault('email')
)
const [redirectFromNuqs] = useQueryState('redirect')
const redirectFromLocation = new URLSearchParams(
typeof location === 'undefined' ? '' : location.search
).get('redirect')
console.dir({
redirectFromNuqs,
redirectFromLocation
})
return (
<>
<p>Redirect from nuqs: {redirectFromNuqs}</p>
<p>Redirect from location: {redirectFromLocation}</p>
<button id="start" onClick={() => setStep('password')}>
Go to password step
</button>
</>
)
} |
Beta Was this translation helpful? Give feedback.
-
agh interesting. I am actually seeing the same behavior and just had a different app router problem that made it seem like it was coming from the url encoding. I had a server side auth redirect and the browser cache kept that page cached (with the redirect) and because I'd log in quickly and I'd hit that cached page again with the redirect back to auth again :). Fun NextJS stuff! Thanks for the quick response though! Much appreciated! |
Beta Was this translation helpful? Give feedback.
The thing is anything in the URL is automatically decoded when reading from
location.search
.I ran an experiment using your example and the value is indeed shown un-encoded in the URL, but is correctly interpreted when read back (either via
nuqs
orlocation.search
):Screen.Recording.2024-06-18.at.21.14.43.mov