-
Notifications
You must be signed in to change notification settings - Fork 30k
feat: support isr reason for on request error #68845
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3a14289
feat: support isr reason for on request error
huozhi 574f2a9
move to prod test
huozhi 468722a
pages isr
huozhi 1fdbef9
fix test for app page revalidate
huozhi 53b8d76
include app routes
huozhi 2b66ea2
skip ppr case
huozhi f341e18
merge canary
huozhi ed1987c
improve type and update tests
huozhi f70ff84
update tests
huozhi ee388f6
add sa revalidate case
huozhi e4208ec
add retry for ci
huozhi 7d5c4ba
bypass turbopack test
huozhi 96fdb7f
update ppr manifest
huozhi 4dbc216
rm unrelated fetch
huozhi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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: { | ||
huozhi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| isOnDemandRevalidate?: boolean | ||
| isRevalidate?: boolean | ||
| }): 'on-demand' | 'stale' | undefined { | ||
| if (params.isOnDemandRevalidate) { | ||
| return 'on-demand' | ||
| } | ||
| if (params.isRevalidate) { | ||
| return 'stale' | ||
| } | ||
| return undefined | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 | ||
| } | ||
huozhi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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', () => { | ||
huozhi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.