Unverified Commit 2baf4f74 authored by 栗原和也's avatar 栗原和也 Committed by GitHub

fix: Enable SearchParams to be displayed after redirect in Server Action (#62582)

### What?
Fixes https://github.com/vercel/next.js/issues/62525
Closes NEXT-2620

Since codes below do not consider searchParams, `redirect` in server
action removes all searchParams passed from client side.

https://github.com/vercel/next.js/commit/93e4bb823ccb6452ba591e29a3ec0b258b3a9b5c#diff-c809d50461027cdba7c092e564818b1172133d337abc5c513f829c94c8483dc6R186

So I just add conditional branch for searchParams.

---

lint, prettier was applied.
Also I have done tests by commands below. and it was all passed.

```
pnpm testonly --testPathPattern "integration" -t "redirect"
```

---------
Co-authored-by: 's avatarZack Tanner <zacktanner@gmail.com>
parent c5299239
...@@ -187,7 +187,7 @@ async function createRedirectRenderResult( ...@@ -187,7 +187,7 @@ async function createRedirectRenderResult(
const host = process.env.__NEXT_PRIVATE_HOST || originalHost.value const host = process.env.__NEXT_PRIVATE_HOST || originalHost.value
const fetchUrl = new URL( const fetchUrl = new URL(
`${proto}://${host}${basePath}${parsedRedirectUrl.pathname}` `${proto}://${host}${basePath}${parsedRedirectUrl.pathname}${parsedRedirectUrl.search}`
) )
if (staticGenerationStore.revalidatedTags) { if (staticGenerationStore.revalidatedTags) {
......
...@@ -1094,6 +1094,22 @@ createNextDescribe( ...@@ -1094,6 +1094,22 @@ createNextDescribe(
expect(responseCodes).toEqual([303]) expect(responseCodes).toEqual([303])
}) })
it('displays searchParams correctly when redirecting with SearchParams', async () => {
const browser = await next.browser('/redirects/action-redirect')
await browser.refresh()
expect(await browser.elementByCss('h2').text()).toBe('baz=')
// redirect with search params
await browser.elementById('redirect-with-search-params').click()
await check(
() => browser.url(),
/\/redirects\/action-redirect\/redirect-target\?baz=1/
)
// verify that the search params was set correctly
expect(await browser.elementByCss('h2').text()).toBe('baz=1')
})
it('merges cookies correctly when redirecting', async () => { it('merges cookies correctly when redirecting', async () => {
const browser = await next.browser('/redirects/action-redirect') const browser = await next.browser('/redirects/action-redirect')
......
import { cookies } from 'next/headers' import { cookies } from 'next/headers'
import { redirect } from 'next/navigation' import { redirect } from 'next/navigation'
export default function Page() { export default function Page({ searchParams }) {
const foo = cookies().get('foo') const foo = cookies().get('foo')
const bar = cookies().get('bar') const bar = cookies().get('bar')
return ( return (
...@@ -21,6 +21,17 @@ export default function Page() { ...@@ -21,6 +21,17 @@ export default function Page() {
Set Cookies and Redirect Set Cookies and Redirect
</button> </button>
</form> </form>
<h2>baz={searchParams.baz ?? ''}</h2>
<form
action={async () => {
'use server'
redirect('/redirects/action-redirect/redirect-target?baz=1')
}}
>
<button type="submit" id="redirect-with-search-params">
Redirect with SearchParams
</button>
</form>
</div> </div>
) )
} }
import { cookies } from 'next/headers' import { cookies } from 'next/headers'
export default function Page() { export default function Page({ searchParams }) {
const foo = cookies().get('foo') const foo = cookies().get('foo')
const bar = cookies().get('bar') const bar = cookies().get('bar')
return ( return (
...@@ -8,6 +8,7 @@ export default function Page() { ...@@ -8,6 +8,7 @@ export default function Page() {
<h1> <h1>
foo={foo ? foo.value : ''}; bar={bar ? bar.value : ''} foo={foo ? foo.value : ''}; bar={bar ? bar.value : ''}
</h1> </h1>
<h2>baz={searchParams.baz ?? ''}</h2>
</div> </div>
) )
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment