Skip to content

Commit bae1fb5

Browse files
committed
fix root page revalidation when redirecting in a server action
1 parent 843332f commit bae1fb5

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

packages/next/src/client/components/router-reducer/reducers/server-action-reducer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,9 @@ export function serverActionReducer(
225225
[''],
226226
currentTree,
227227
treePatch,
228-
href
228+
redirectLocation
229+
? createHrefFromUrl(redirectLocation)
230+
: state.canonicalUrl
229231
)
230232

231233
if (newTree === null) {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { redirect } from 'next/navigation'
2+
3+
export default function Page() {
4+
return (
5+
<form
6+
action={async () => {
7+
'use server'
8+
redirect('/')
9+
}}
10+
>
11+
<button type="submit">Redirect</button>
12+
</form>
13+
)
14+
}

test/e2e/app-dir/parallel-routes-revalidation/parallel-routes-revalidation.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,25 @@ createNextDescribe(
191191
)
192192
})
193193

194+
it('should refresh the correct page when a server action triggers a redirect', async () => {
195+
const browser = await next.browser('/redirect')
196+
await browser.elementByCss('button').click()
197+
198+
await browser.elementByCss("[href='/revalidate-modal']").click()
199+
200+
await retry(async () => {
201+
// confirm there aren't any entries yet
202+
expect((await browser.elementsByCss('#entries li')).length).toBe(0)
203+
})
204+
205+
await browser.elementById('create-entry').click()
206+
207+
await retry(async () => {
208+
// we created an entry and called revalidate, so we should have 1 entry
209+
expect((await browser.elementsByCss('#entries li')).length).toBe(1)
210+
})
211+
})
212+
194213
describe.each([
195214
{ basePath: '/refreshing', label: 'regular', withSearchParams: false },
196215
{ basePath: '/refreshing', label: 'regular', withSearchParams: true },

0 commit comments

Comments
 (0)