-
Notifications
You must be signed in to change notification settings - Fork 26.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix usage of
router.refresh()
with Draft Mode in App Router (#50941)
After enabling Draft Mode, `router.refresh()` was incorrectly causing the page to crash and do a full page refresh. This PR fixes this case. Co-authored-by: Steven <229881+styfle@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
70 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import { draftMode } from 'next/headers' | ||
import { redirect } from 'next/navigation' | ||
|
||
export function GET() { | ||
export function GET(req: Request) { | ||
draftMode().enable() | ||
return redirect('/some-other-page') | ||
const to = new URL(req.url).searchParams.get('to') ?? '/some-other-page' | ||
return redirect(to) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React from 'react' | ||
import { RouteRefresher } from '../../../components/RouterRefresh' | ||
|
||
export default async function Page() { | ||
return ( | ||
<main> | ||
<h1>With generateStaticParams</h1> | ||
<RouteRefresher /> | ||
</main> | ||
) | ||
} | ||
|
||
export function generateStaticParams() { | ||
return [{ id: 'foo' }] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use client' | ||
|
||
import { useRouter } from 'next/navigation' | ||
import React from 'react' | ||
|
||
export function RouteRefresher() { | ||
const router = useRouter() | ||
|
||
return ( | ||
<button id="refresh" onClick={() => router.refresh()}> | ||
Refresh | ||
</button> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters