Skip to content

Commit

Permalink
provide revalidateReason to getStaticProps
Browse files Browse the repository at this point in the history
  • Loading branch information
ztanner committed Apr 9, 2024
1 parent 8c9c1d2 commit 2e24333
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/next/src/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ export async function renderToHTMLImpl(
},
},
() =>
getStaticProps!({
getStaticProps({
...(pageIsDynamic
? { params: query as ParsedUrlQuery }
: undefined),
Expand All @@ -855,6 +855,11 @@ export async function renderToHTMLImpl(
locales: renderOpts.locales,
locale: renderOpts.locale,
defaultLocale: renderOpts.defaultLocale,
revalidateReason: renderOpts.isOnDemandRevalidate
? 'on-demand'
: isBuildTimeSSG
? 'build'
: 'stale',
})
)
} catch (staticPropsError: any) {
Expand Down
1 change: 1 addition & 0 deletions packages/next/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ export type GetStaticPropsContext<
locale?: string
locales?: string[]
defaultLocale?: string
revalidateReason?: 'on-demand' | 'build' | 'stale'
}

/**
Expand Down
15 changes: 15 additions & 0 deletions test/e2e/revalidate-reason/pages/api/revalidate.ts
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 })
}
17 changes: 17 additions & 0 deletions test/e2e/revalidate-reason/pages/index.tsx
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>
}
18 changes: 18 additions & 0 deletions test/e2e/revalidate-reason/pages/stale.tsx
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>
}
61 changes: 61 additions & 0 deletions test/e2e/revalidate-reason/revalidate-reason.test.ts
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'
)
})
})
})

0 comments on commit 2e24333

Please sign in to comment.