Skip to content

Commit

Permalink
fix root page revalidation when redirecting in a server action
Browse files Browse the repository at this point in the history
  • Loading branch information
ztanner committed Apr 18, 2024
1 parent 843332f commit bae1fb5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,9 @@ export function serverActionReducer(
[''],
currentTree,
treePatch,
href
redirectLocation
? createHrefFromUrl(redirectLocation)
: state.canonicalUrl
)

if (newTree === null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { redirect } from 'next/navigation'

export default function Page() {
return (
<form
action={async () => {
'use server'
redirect('/')
}}
>
<button type="submit">Redirect</button>
</form>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,25 @@ createNextDescribe(
)
})

it('should refresh the correct page when a server action triggers a redirect', async () => {
const browser = await next.browser('/redirect')
await browser.elementByCss('button').click()

await browser.elementByCss("[href='/revalidate-modal']").click()

await retry(async () => {
// confirm there aren't any entries yet
expect((await browser.elementsByCss('#entries li')).length).toBe(0)
})

await browser.elementById('create-entry').click()

await retry(async () => {
// we created an entry and called revalidate, so we should have 1 entry
expect((await browser.elementsByCss('#entries li')).length).toBe(1)
})
})

describe.each([
{ basePath: '/refreshing', label: 'regular', withSearchParams: false },
{ basePath: '/refreshing', label: 'regular', withSearchParams: true },
Expand Down

0 comments on commit bae1fb5

Please sign in to comment.