Unverified Commit d498f9c1 authored by Jude Gao's avatar Jude Gao Committed by GitHub
parent b6a72f39
...@@ -126,7 +126,6 @@ export function refreshReducer( ...@@ -126,7 +126,6 @@ export function refreshReducer(
mutable.cache = cache mutable.cache = cache
mutable.patchedTree = newTree mutable.patchedTree = newTree
mutable.canonicalUrl = href
currentTree = newTree currentTree = newTree
} }
......
import { NextResponse } from 'next/server'
export function POST() {
const res = NextResponse.json({ message: 'successful' })
res.cookies.set('token', 'this is a token')
return res
}
'use client'
import { useRouter } from 'next/navigation'
import { useEffect } from 'react'
export default function Reload() {
const router = useRouter()
useEffect(() => {
fetch(new URL('/api/set-token', location.origin), {
method: 'POST',
credentials: 'include',
}).then(() => {
router.refresh()
})
}, [router])
return null
}
import Reload from './Reload'
export default function Page() {
return (
<div>
Authenticated, now reloading...
<Reload />
</div>
)
}
export default function Page() {
return <p>Dashboard</p>
}
...@@ -21,4 +21,13 @@ export function middleware(request) { ...@@ -21,4 +21,13 @@ export function middleware(request) {
if (request.nextUrl.pathname === '/redirect-middleware-to-dashboard') { if (request.nextUrl.pathname === '/redirect-middleware-to-dashboard') {
return NextResponse.redirect(new URL('/redirect-dest', request.url)) return NextResponse.redirect(new URL('/redirect-dest', request.url))
} }
if (request.nextUrl.pathname === '/redirect-on-refresh/auth') {
const cookie = request.cookies.get('token')
if (cookie) {
return NextResponse.redirect(
new URL('/redirect-on-refresh/dashboard', request.url)
)
}
}
} }
...@@ -909,4 +909,15 @@ describe('app dir - navigation', () => { ...@@ -909,4 +909,15 @@ describe('app dir - navigation', () => {
}) })
}) })
}) })
describe('middleware redirect', () => {
it('should change browser location when router.refresh() gets a redirect response', async () => {
const browser = await next.browser('/redirect-on-refresh/auth')
await retry(async () =>
expect(await browser.url()).toBe(
next.url + '/redirect-on-refresh/dashboard'
)
)
})
})
}) })
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