Skip to content

Commit 1916736

Browse files
committed
Merge branch 'rs/verify-no-forms' of github.com:netlify/netlify-plugin-nextjs into rs/verify-no-forms
2 parents ccd1351 + 56de105 commit 1916736

File tree

10 files changed

+181
-76
lines changed

10 files changed

+181
-76
lines changed

package-lock.json

+28-28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@
5050
"devDependencies": {
5151
"@fastly/http-compute-js": "1.1.4",
5252
"@netlify/blobs": "^7.3.0",
53-
"@netlify/build": "^29.49.1",
53+
"@netlify/build": "^29.50.1",
5454
"@netlify/edge-bundler": "^12.1.1",
5555
"@netlify/edge-functions": "^2.8.1",
5656
"@netlify/eslint-config-node": "^7.0.1",
5757
"@netlify/functions": "^2.8.0",
5858
"@netlify/serverless-functions-api": "^1.18.5",
59-
"@netlify/zip-it-and-ship-it": "^9.37.1",
59+
"@netlify/zip-it-and-ship-it": "^9.37.2",
6060
"@opentelemetry/api": "^1.8.0",
6161
"@opentelemetry/exporter-trace-otlp-http": "^0.51.0",
6262
"@opentelemetry/resources": "^1.24.0",

src/run/handlers/cache.cts

+21-7
Original file line numberDiff line numberDiff line change
@@ -326,19 +326,33 @@ export class NetlifyCacheHandler implements CacheHandler {
326326
if (requestContext?.didPagesRouterOnDemandRevalidate) {
327327
const tag = `_N_T_${key === '/index' ? '/' : key}`
328328
getLogger().debug(`Purging CDN cache for: [${tag}]`)
329-
purgeCache({ tags: [tag] }).catch((error) => {
330-
// TODO: add reporting here
331-
getLogger()
332-
.withError(error)
333-
.error(`[NetlifyCacheHandler]: Purging the cache for tag ${tag} failed`)
334-
})
329+
requestContext.trackBackgroundWork(
330+
purgeCache({ tags: [tag] }).catch((error) => {
331+
// TODO: add reporting here
332+
getLogger()
333+
.withError(error)
334+
.error(`[NetlifyCacheHandler]: Purging the cache for tag ${tag} failed`)
335+
}),
336+
)
335337
}
336338
}
337339
})
338340
}
339341

340342
// eslint-disable-next-line @typescript-eslint/no-explicit-any
341343
async revalidateTag(tagOrTags: string | string[], ...args: any) {
344+
const revalidateTagPromise = this.doRevalidateTag(tagOrTags, ...args)
345+
346+
const requestContext = getRequestContext()
347+
if (requestContext) {
348+
requestContext.trackBackgroundWork(revalidateTagPromise)
349+
}
350+
351+
return revalidateTagPromise
352+
}
353+
354+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
355+
private async doRevalidateTag(tagOrTags: string | string[], ...args: any) {
342356
getLogger().withFields({ tagOrTags, args }).debug('NetlifyCacheHandler.revalidateTag')
343357

344358
const tags = Array.isArray(tagOrTags) ? tagOrTags : [tagOrTags]
@@ -357,7 +371,7 @@ export class NetlifyCacheHandler implements CacheHandler {
357371
}),
358372
)
359373

360-
purgeCache({ tags }).catch((error) => {
374+
await purgeCache({ tags }).catch((error) => {
361375
// TODO: add reporting here
362376
getLogger()
363377
.withError(error)

src/run/revalidate.ts

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,34 @@
11
import type { ServerResponse } from 'node:http'
2+
import { isPromise } from 'node:util/types'
3+
4+
import type { NextApiResponse } from 'next'
25

36
import type { RequestContext } from './handlers/request-context.cjs'
47

8+
type ResRevalidateMethod = NextApiResponse['revalidate']
9+
10+
function isRevalidateMethod(
11+
key: string,
12+
nextResponseField: unknown,
13+
): nextResponseField is ResRevalidateMethod {
14+
return key === 'revalidate' && typeof nextResponseField === 'function'
15+
}
16+
517
// Needing to proxy the response object to intercept the revalidate call for on-demand revalidation on page routes
618
export const nextResponseProxy = (res: ServerResponse, requestContext: RequestContext) => {
719
return new Proxy(res, {
8-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
9-
get(target: any[string], key: string) {
10-
const originalValue = target[key]
11-
if (key === 'revalidate') {
12-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
13-
return async function newRevalidate(...args: any[]) {
20+
get(target: ServerResponse, key: string) {
21+
const originalValue = Reflect.get(target, key)
22+
if (isRevalidateMethod(key, originalValue)) {
23+
return function newRevalidate(...args: Parameters<ResRevalidateMethod>) {
1424
requestContext.didPagesRouterOnDemandRevalidate = true
15-
return originalValue?.apply(target, args)
25+
26+
const result = originalValue.apply(target, args)
27+
if (result && isPromise(result)) {
28+
requestContext.trackBackgroundWork(result)
29+
}
30+
31+
return result
1632
}
1733
}
1834
return originalValue

tests/e2e/page-router.test.ts

+41-14
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,35 @@ export async function check(
5050

5151
test.describe('Simple Page Router (no basePath, no i18n)', () => {
5252
test.describe('On-demand revalidate works correctly', () => {
53-
for (const { label, prerendered, pagePath, expectedH1Content } of [
53+
for (const { label, prerendered, pagePath, revalidateApiBasePath, expectedH1Content } of [
5454
{
55-
label: 'prerendered page with static path',
55+
label: 'prerendered page with static path and awaited res.revalidate()',
5656
prerendered: true,
5757
pagePath: '/static/revalidate-manual',
58+
revalidateApiBasePath: '/api/revalidate',
5859
expectedH1Content: 'Show #71',
5960
},
6061
{
61-
label: 'prerendered page with dynamic path',
62+
label: 'prerendered page with dynamic path and awaited res.revalidate()',
6263
prerendered: true,
6364
pagePath: '/products/prerendered',
65+
revalidateApiBasePath: '/api/revalidate',
6466
expectedH1Content: 'Product prerendered',
6567
},
6668
{
67-
label: 'not prerendered page with dynamic path',
69+
label: 'not prerendered page with dynamic path and awaited res.revalidate()',
6870
prerendered: false,
6971
pagePath: '/products/not-prerendered',
72+
revalidateApiBasePath: '/api/revalidate',
7073
expectedH1Content: 'Product not-prerendered',
7174
},
75+
{
76+
label: 'not prerendered page with dynamic path and not awaited res.revalidate()',
77+
prerendered: false,
78+
pagePath: '/products/not-prerendered-and-not-awaited-revalidation',
79+
revalidateApiBasePath: '/api/revalidate-no-await',
80+
expectedH1Content: 'Product not-prerendered-and-not-awaited-revalidation',
81+
},
7282
]) {
7383
test(label, async ({ page, pollUntilHeadersMatch, pageRouter }) => {
7484
// in case there is retry or some other test did hit that path before
@@ -192,7 +202,7 @@ test.describe('Simple Page Router (no basePath, no i18n)', () => {
192202
expect(data2?.pageProps?.time).toBe(date1)
193203

194204
const revalidate = await page.goto(
195-
new URL(`/api/revalidate?path=${pagePath}`, pageRouter.url).href,
205+
new URL(`${revalidateApiBasePath}?path=${pagePath}`, pageRouter.url).href,
196206
)
197207
expect(revalidate?.status()).toBe(200)
198208

@@ -411,25 +421,35 @@ test.describe('Simple Page Router (no basePath, no i18n)', () => {
411421

412422
test.describe('Page Router with basePath and i18n', () => {
413423
test.describe('Static revalidate works correctly', () => {
414-
for (const { label, prerendered, pagePath, expectedH1Content } of [
424+
for (const { label, prerendered, pagePath, revalidateApiBasePath, expectedH1Content } of [
415425
{
416-
label: 'prerendered page with static path',
426+
label: 'prerendered page with static path and awaited res.revalidate()',
417427
prerendered: true,
418428
pagePath: '/static/revalidate-manual',
429+
revalidateApiBasePath: '/api/revalidate',
419430
expectedH1Content: 'Show #71',
420431
},
421432
{
422-
label: 'prerendered page with dynamic path',
433+
label: 'prerendered page with dynamic path and awaited res.revalidate()',
423434
prerendered: true,
424435
pagePath: '/products/prerendered',
436+
revalidateApiBasePath: '/api/revalidate',
425437
expectedH1Content: 'Product prerendered',
426438
},
427439
{
428-
label: 'not prerendered page with dynamic path',
440+
label: 'not prerendered page with dynamic path and awaited res.revalidate()',
429441
prerendered: false,
430442
pagePath: '/products/not-prerendered',
443+
revalidateApiBasePath: '/api/revalidate',
431444
expectedH1Content: 'Product not-prerendered',
432445
},
446+
{
447+
label: 'not prerendered page with dynamic path and not awaited res.revalidate()',
448+
prerendered: false,
449+
pagePath: '/products/not-prerendered-and-not-awaited-revalidation',
450+
revalidateApiBasePath: '/api/revalidate-no-await',
451+
expectedH1Content: 'Product not-prerendered-and-not-awaited-revalidation',
452+
},
433453
]) {
434454
test.describe(label, () => {
435455
test(`default locale`, async ({ page, pollUntilHeadersMatch, pageRouterBasePathI18n }) => {
@@ -622,7 +642,10 @@ test.describe('Page Router with basePath and i18n', () => {
622642

623643
// revalidate implicit locale path
624644
const revalidateImplicit = await page.goto(
625-
new URL(`/base/path/api/revalidate?path=${pagePath}`, pageRouterBasePathI18n.url).href,
645+
new URL(
646+
`/base/path${revalidateApiBasePath}?path=${pagePath}`,
647+
pageRouterBasePathI18n.url,
648+
).href,
626649
)
627650
expect(revalidateImplicit?.status()).toBe(200)
628651

@@ -713,8 +736,10 @@ test.describe('Page Router with basePath and i18n', () => {
713736

714737
// revalidate implicit locale path
715738
const revalidateExplicit = await page.goto(
716-
new URL(`/base/path/api/revalidate?path=/en${pagePath}`, pageRouterBasePathI18n.url)
717-
.href,
739+
new URL(
740+
`/base/path${revalidateApiBasePath}?path=/en${pagePath}`,
741+
pageRouterBasePathI18n.url,
742+
).href,
718743
)
719744
expect(revalidateExplicit?.status()).toBe(200)
720745

@@ -934,8 +959,10 @@ test.describe('Page Router with basePath and i18n', () => {
934959
expect(data2?.pageProps?.time).toBe(date1)
935960

936961
const revalidate = await page.goto(
937-
new URL(`/base/path/api/revalidate?path=/de${pagePath}`, pageRouterBasePathI18n.url)
938-
.href,
962+
new URL(
963+
`/base/path${revalidateApiBasePath}?path=/de${pagePath}`,
964+
pageRouterBasePathI18n.url,
965+
).href,
939966
)
940967
expect(revalidate?.status()).toBe(200)
941968

0 commit comments

Comments
 (0)