Skip to content
Open
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
10 changes: 9 additions & 1 deletion packages/next/src/client/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,15 @@ export async function hydrate(opts?: { beforeRender?: () => Promise<void> }) {

// In development, error the navigation API usage in runtime,
// since it's not allowed to be used in pages router as it doesn't contain error boundary like app router.
if (isNextRouterError(initialErr)) {
if (
isNextRouterError(initialErr) &&
!initialErr.message.includes(
'Next.js navigation API is not allowed to be used in Middleware'
) &&
!initialErr.message.includes(
'Next.js navigation API is not allowed to be used in Proxy'
)
) {
error.message =
'Next.js navigation API is not allowed to be used in Pages Router.'
}
Expand Down
10 changes: 10 additions & 0 deletions test/development/app-dir/server-navigation-error/proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { notFound, redirect } from 'next/navigation'
import { NextRequest } from 'next/server'

export default function proxy(req: NextRequest) {
if (req.nextUrl.pathname === '/proxy/not-found') {
notFound()
} else if (req.nextUrl.pathname === '/proxy/redirect') {
redirect('/')
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,44 @@ describe('server-navigation-error', () => {
`)
})
})

describe('proxy', () => {
it('should error on navigation API redirect ', async () => {
const browser = await next.browser('/proxy/redirect')
// FIXME: the first request to middleware error load didn't show the redbox, need one more reload
await browser.refresh()

await expect(browser).toDisplayRedbox(`
{
"description": "Next.js navigation API is not allowed to be used in Proxy.",
"environmentLabel": null,
"label": "Runtime Error",
"source": "proxy.ts (8:13) @ proxy
> 8 | redirect('/')
| ^",
"stack": [
"proxy proxy.ts (8:13)",
],
}
`)
})

it('should error on navigation API not-found', async () => {
const browser = await next.browser('/proxy/not-found')

await expect(browser).toDisplayRedbox(`
{
"description": "Next.js navigation API is not allowed to be used in Proxy.",
"environmentLabel": null,
"label": "Runtime Error",
"source": "proxy.ts (6:13) @ proxy
> 6 | notFound()
| ^",
"stack": [
"proxy proxy.ts (6:13)",
],
}
`)
})
})
})
Loading