-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support isr reason for on request error (#68845)
### What Provide `revalidateReason` in the `errorContext` argument of `onRequestError` API #### API ```ts revalidateReason?: 'on-demand' | 'stale' | undefined ``` ### Why With this property, developer is able to determine if current error is attached to an ISR request. * `stale` means it revalidated due to the cache is expired * `on-demand` means it revalidated due to a user initiated revalidation, e.g. using `res.revalidate` to revalidate certain path ### Misc * We don't generate edge entry of instrumentation when there's no edge routes but instrumentation is presented Note: for PPR case we'll tackle it later with another PPR specific task, since we need to provide the render is a postpone render or just normal render Closes NDX-23
- Loading branch information
Showing
22 changed files
with
287 additions
and
5 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
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
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
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,12 @@ | ||
export function getRevalidateReason(params: { | ||
isOnDemandRevalidate?: boolean | ||
isRevalidate?: boolean | ||
}): 'on-demand' | 'stale' | undefined { | ||
if (params.isOnDemandRevalidate) { | ||
return 'on-demand' | ||
} | ||
if (params.isRevalidate) { | ||
return 'stale' | ||
} | ||
return undefined | ||
} |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default function Page() { | ||
if (process.env.NEXT_PHASE !== 'phase-production-build') { | ||
throw new Error('app:on-demand') | ||
} | ||
return <p>{Date.now()}</p> | ||
} | ||
|
||
export const revalidate = 1000 |
8 changes: 8 additions & 0 deletions
8
test/e2e/on-request-error/isr/app/app/route/on-demand/route.js
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,8 @@ | ||
export function GET() { | ||
if (process.env.NEXT_PHASE !== 'phase-production-build') { | ||
throw new Error('app:route:on-demand') | ||
} | ||
return new Response('app:route') | ||
} | ||
|
||
export const revalidate = 1000 |
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,8 @@ | ||
export function GET() { | ||
if (process.env.NEXT_PHASE !== 'phase-production-build') { | ||
throw new Error('app:route:stale') | ||
} | ||
return new Response('app:route') | ||
} | ||
|
||
export const revalidate = 2 |
7 changes: 7 additions & 0 deletions
7
test/e2e/on-request-error/isr/app/app/self-revalidate/action.js
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,7 @@ | ||
'use server' | ||
|
||
import { revalidatePath } from 'next/cache' | ||
|
||
export async function revalidateSelf() { | ||
revalidatePath('/app/self-revalidate') | ||
} |
21 changes: 21 additions & 0 deletions
21
test/e2e/on-request-error/isr/app/app/self-revalidate/page.js
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,21 @@ | ||
'use client' | ||
|
||
import { revalidateSelf } from './action' | ||
|
||
export default function Page() { | ||
if (typeof window === 'undefined') { | ||
if (process.env.NEXT_PHASE !== 'phase-production-build') { | ||
throw new Error('app:self-revalidate') | ||
} | ||
} | ||
return ( | ||
<button | ||
onClick={() => { | ||
revalidateSelf() | ||
location.reload() | ||
}} | ||
> | ||
revalidate | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function Page() { | ||
if (process.env.NEXT_PHASE !== 'phase-production-build') | ||
throw new Error('app:stale') | ||
return <p>{Date.now()}</p> | ||
} | ||
|
||
export const revalidate = 2 |
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,12 @@ | ||
export const metadata = { | ||
title: 'Next.js', | ||
description: 'Generated by Next.js', | ||
} | ||
|
||
export default function RootLayout({ children }) { | ||
return ( | ||
<html lang="en"> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
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,31 @@ | ||
import fs from 'fs' | ||
import fsp from 'fs/promises' | ||
import path from 'path' | ||
|
||
const dir = path.dirname(new URL(import.meta.url).pathname) | ||
const logPath = path.join(dir, 'output-log.json') | ||
|
||
export async function register() { | ||
await fsp.writeFile(logPath, '{}', 'utf8') | ||
} | ||
|
||
// Since only Node.js runtime support ISR, we can just write the error state to a file here. | ||
// `onRequestError` will only be bundled within the Node.js runtime. | ||
export async function onRequestError(err, request, context) { | ||
const payload = { | ||
message: err.message, | ||
request, | ||
context, | ||
} | ||
|
||
const json = fs.existsSync(logPath) | ||
? JSON.parse(await fsp.readFile(logPath, 'utf8')) | ||
: {} | ||
|
||
json[payload.message] = payload | ||
|
||
console.log( | ||
`[instrumentation] write-log:${payload.message} ${payload.context.revalidateReason}` | ||
) | ||
await fsp.writeFile(logPath, JSON.stringify(json, null, 2), 'utf8') | ||
} |
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,93 @@ | ||
import { nextTestSetup } from 'e2e-utils' | ||
import { retry, waitFor } from 'next-test-utils' | ||
import { getOutputLogJson } from '../_testing/utils' | ||
|
||
const outputLogPath = 'output-log.json' | ||
|
||
describe('on-request-error - isr', () => { | ||
const { next, skipped, isNextDev } = nextTestSetup({ | ||
files: __dirname, | ||
skipDeployment: true, | ||
}) | ||
|
||
if (skipped) { | ||
return | ||
} | ||
|
||
if (isNextDev) { | ||
it('should skip in development mode', () => { | ||
// This ISR test is only applicable for production mode | ||
}) | ||
return | ||
} | ||
|
||
async function matchRevalidateReason( | ||
errorMessage: string, | ||
revalidateReason: string | ||
) { | ||
await retry(async () => { | ||
const json = await getOutputLogJson(next, outputLogPath) | ||
expect(json[errorMessage]).toMatchObject({ | ||
context: { | ||
revalidateReason, | ||
}, | ||
}) | ||
}) | ||
} | ||
|
||
describe('app router ISR', () => { | ||
it('should capture correct reason for stale errored page', async () => { | ||
await next.fetch('/app/stale') | ||
await waitFor(2 * 1000) // wait for revalidation | ||
await next.fetch('/app/stale') | ||
|
||
await matchRevalidateReason('app:stale', 'stale') | ||
}) | ||
|
||
it('should capture correct reason for on-demand revalidated page', async () => { | ||
await next.fetch('/api/revalidate-path?path=/app/on-demand') | ||
|
||
await matchRevalidateReason('app:on-demand', 'on-demand') | ||
}) | ||
|
||
it('should capture correct reason for build errored route', async () => { | ||
await next.fetch('/app/route/stale') | ||
await waitFor(2 * 1000) // wait for revalidation | ||
await next.fetch('/app/route/stale') | ||
|
||
await matchRevalidateReason('app:route:stale', 'stale') | ||
}) | ||
|
||
it('should capture correct reason for on-demand revalidated route', async () => { | ||
await next.fetch('/api/revalidate-path?path=/app/route/on-demand') | ||
|
||
await matchRevalidateReason('app:route:on-demand', 'on-demand') | ||
}) | ||
|
||
it('should capture revalidate from server action', async () => { | ||
const browser = await next.browser('/app/self-revalidate') | ||
const button = await browser.elementByCss('button') | ||
await button.click() | ||
|
||
await retry(async () => { | ||
await next.fetch('/app/self-revalidate') | ||
await matchRevalidateReason('app:self-revalidate', 'stale') | ||
}) | ||
}) | ||
}) | ||
|
||
describe('pages router ISR', () => { | ||
it('should capture correct reason for stale errored page', async () => { | ||
await next.fetch('/pages/stale') | ||
await waitFor(2 * 1000) // wait for revalidation | ||
await next.fetch('/pages/stale') | ||
|
||
await matchRevalidateReason('pages:stale', 'stale') | ||
}) | ||
|
||
it('should capture correct reason for on-demand revalidated page', async () => { | ||
await next.fetch('/api/revalidate-path?path=/pages/on-demand') | ||
await matchRevalidateReason('pages:on-demand', 'on-demand') | ||
}) | ||
}) | ||
}) |
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,5 @@ | ||
module.exports = { | ||
experimental: { | ||
instrumentationHook: true, | ||
}, | ||
} |
11 changes: 11 additions & 0 deletions
11
test/e2e/on-request-error/isr/pages/api/revalidate-path.js
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,11 @@ | ||
export default async function handler(req, res) { | ||
const { path } = req.query | ||
try { | ||
await res.revalidate(path) | ||
return res.json({ revalidated: true }) | ||
} catch (err) { | ||
console.error('Failed to revalidate:', err) | ||
} | ||
|
||
res.json({ revalidated: false }) | ||
} |
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,16 @@ | ||
export default function Page() { | ||
if (typeof window === 'undefined') { | ||
if (process.env.NEXT_PHASE !== 'phase-production-build') | ||
throw new Error('pages:on-demand') | ||
} | ||
return <p>{Date.now()}</p> | ||
} | ||
|
||
export async function getStaticProps() { | ||
return { | ||
props: { | ||
key: 'value', | ||
}, | ||
revalidate: 1000, | ||
} | ||
} |
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,17 @@ | ||
export default function Page() { | ||
if (typeof window === 'undefined') { | ||
if (process.env.NEXT_PHASE !== 'phase-production-build') | ||
throw new Error('pages:stale') | ||
} | ||
|
||
return <p>{Date.now()}</p> | ||
} | ||
|
||
export async function getStaticProps() { | ||
return { | ||
props: { | ||
key: 'value', | ||
}, | ||
revalidate: 2, | ||
} | ||
} |
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