Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix root page revalidation when redirecting in a server action #64730

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 7 additions & 1 deletion test/e2e/app-dir/parallel-routes-revalidation/app/actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use server'
import { revalidatePath } from 'next/cache'
import { redirect } from 'next/navigation'

const data = []
let data = []

export async function addData(newData: string) {
// sleep 1s
Expand All @@ -21,3 +22,8 @@ export async function redirectAction() {
await new Promise((res) => setTimeout(res, 1000))
redirect('/')
}

export async function clearData() {
data = []
revalidatePath('/')
}
5 changes: 4 additions & 1 deletion test/e2e/app-dir/parallel-routes-revalidation/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Link from 'next/link'
import { getData } from './actions'
import { clearData, getData } from './actions'

export default async function Home() {
const data = await getData()
Expand All @@ -14,6 +14,9 @@ export default async function Home() {
<Link href="/redirect-modal">Open Redirect Modal</Link>
<Link href="/detail-page">Intercepted Detail Page</Link>
<div id="random-number">{randomNumber}</div>
<form action={clearData}>
<button id="clear-entries">Clear Entries</button>
</form>
<div>
<h1>Current Data</h1>
<ul id="entries">
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,29 @@ 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 check(() => browser.hasElementByCssSelector('#create-entry'), true)

await browser.elementById('clear-entries').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
Loading