-
Notifications
You must be signed in to change notification settings - Fork 26.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provide revalidateReason to getStaticProps (#64258)
Provides a `revalidateReason` argument to `getStaticProps` ("stale" | "on-demand" | "build"). - Build indicates it was run at build time - On-demand indicates it was run as a side effect of [on-demand revalidation](https://nextjs.org/docs/pages/building-your-application/data-fetching/incremental-static-regeneration#on-demand-revalidation) - Stale indicates the resource was considered stale (either due to being in dev mode, or an expired revalidate period) This will allow changing behavior based on the context in which it's called. Closes NEXT-1900
- Loading branch information
Showing
8 changed files
with
130 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse<{ revalidated: boolean }> | ||
) { | ||
try { | ||
await res.revalidate('/') | ||
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,17 @@ | ||
import React from 'react' | ||
|
||
export async function getStaticProps({ params, revalidateReason }) { | ||
console.log( | ||
'revalidate-reason/pages/index.tsx revalidateReason:', | ||
revalidateReason | ||
) | ||
return { | ||
props: { | ||
hello: 'world', | ||
}, | ||
} | ||
} | ||
|
||
export default ({ hello }) => { | ||
return <p>hello: {hello}</p> | ||
} |
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,18 @@ | ||
import React from 'react' | ||
|
||
export async function getStaticProps({ params, revalidateReason }) { | ||
console.log( | ||
'revalidate-reason/pages/stale.tsx revalidateReason:', | ||
revalidateReason | ||
) | ||
return { | ||
props: { | ||
hello: 'world', | ||
}, | ||
revalidate: 5, | ||
} | ||
} | ||
|
||
export default ({ hello }) => { | ||
return <p>hello: {hello}</p> | ||
} |
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,61 @@ | ||
import { nextTestSetup } from 'e2e-utils' | ||
import { retry, waitFor } from 'next-test-utils' | ||
|
||
describe('revalidate-reason', () => { | ||
const { next, isNextDev } = nextTestSetup({ | ||
files: __dirname, | ||
}) | ||
|
||
if (isNextDev) { | ||
describe('development mode', () => { | ||
// in dev mode, the revalidateReason is called on every request, and will always be considered stale | ||
it('should support revalidateReason: "stale"', async () => { | ||
const res = await next.fetch('/') | ||
|
||
expect(res.status).toBe(200) | ||
|
||
expect(next.cliOutput).toContain( | ||
'revalidate-reason/pages/index.tsx revalidateReason: stale' | ||
) | ||
}) | ||
}) | ||
|
||
// skip the remaining tests as they do not apply in dev | ||
return | ||
} | ||
|
||
it('should support revalidateReason: "build"', async () => { | ||
expect(next.cliOutput).toContain( | ||
'revalidate-reason/pages/index.tsx revalidateReason: build' | ||
) | ||
expect(next.cliOutput).toContain( | ||
'revalidate-reason/pages/stale.tsx revalidateReason: build' | ||
) | ||
}) | ||
|
||
it('should support revalidateReason: "on-demand"', async () => { | ||
const res = await next.fetch('/api/revalidate') | ||
|
||
expect(res.status).toBe(200) | ||
|
||
expect(next.cliOutput).toContain( | ||
'revalidate-reason/pages/index.tsx revalidateReason: on-demand' | ||
) | ||
}) | ||
|
||
it('should support revalidateReason: "stale"', async () => { | ||
const res = await next.fetch('/stale') | ||
expect(res.status).toBe(200) | ||
|
||
// wait for the revalidation period | ||
await waitFor(5000) | ||
|
||
await retry(async () => { | ||
await next.fetch('/stale') | ||
|
||
expect(next.cliOutput).toContain( | ||
'revalidate-reason/pages/stale.tsx revalidateReason: stale' | ||
) | ||
}) | ||
}) | ||
}) |