Files
Jiachi LiuandGitHub d714bb32aa 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
2024-08-14 21:44:51 +02:00

94 lines
2.8 KiB
TypeScript

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')
})
})
})