Skip to content

Creating a redirect query param - how to handle proper URL encoding? #576

Answered by franky47
shawngustaw asked this question in Q&A
Discussion options

You must be logged in to vote

when I pull it from the URL I can decode it manually

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 or location.search):

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…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by shawngustaw
Comment options

You must be logged in to vote
1 reply
@franky47
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants