Skip to content

Commit 6a18844

Browse files
committed
fix res.revalidate
1 parent bdf74de commit 6a18844

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { NextApiRequest, NextApiResponse } from "next";
2+
3+
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
4+
try {
5+
await res.revalidate('/ssg/')
6+
return res.json({ hello: "OpenNext rocks!" });
7+
}catch(e){
8+
console.error(e)
9+
return res.status(500).json({error: "An error occurred"})
10+
}
11+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type { InferGetStaticPropsType } from "next";
2+
import Link from "next/link";
3+
4+
export async function getStaticProps() {
5+
return {
6+
props: {
7+
time: new Date().toISOString(),
8+
},
9+
};
10+
}
11+
12+
export default function Page({ time }: InferGetStaticPropsType<typeof getStaticProps>) {
13+
return (
14+
<div>
15+
<div className="flex">Time: {time}</div>
16+
<Link href="/">Home</Link>
17+
</div>
18+
);
19+
}

packages/cloudflare/src/cli/build/bundle-server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { handleOptionalDependencies } from "./patches/plugins/optional-deps.js";
2222
import { patchDepdDeprecations } from "./patches/plugins/patch-depd-deprecations.js";
2323
import { fixRequire } from "./patches/plugins/require.js";
2424
import { shimRequireHook } from "./patches/plugins/require-hook.js";
25+
import { patchResRevalidate } from "./patches/plugins/res-revalidate.js";
2526
import { setWranglerExternal } from "./patches/plugins/wrangler-external.js";
2627
import { needsExperimentalReact, normalizePath, patchCodeWithValidations } from "./utils/index.js";
2728

@@ -101,6 +102,7 @@ export async function bundleServer(buildOpts: BuildOptions): Promise<void> {
101102
inlineLoadManifest(updater, buildOpts),
102103
inlineBuildId(updater),
103104
patchDepdDeprecations(updater),
105+
patchResRevalidate(updater),
104106
patchNextMinimal(updater),
105107
// Apply updater updaters, must be the last plugin
106108
updater.plugin,
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";
2+
3+
import { patchCode } from "../ast/util.js";
4+
import { ContentUpdater } from "./content-updater.js";
5+
6+
export const rule = `
7+
rule:
8+
kind: await_expression
9+
inside:
10+
kind: if_statement
11+
stopBy: end
12+
has:
13+
kind: parenthesized_expression
14+
has: { kind: property_identifier , stopBy: end, regex: trustHostHeader}
15+
has:
16+
kind: call_expression
17+
all:
18+
- has: {kind: identifier, pattern: fetch}
19+
- has:
20+
kind: arguments
21+
all:
22+
- has:
23+
kind: object
24+
all:
25+
- has:
26+
kind: pair
27+
all:
28+
- has: {kind: property_identifier, regex: method }
29+
- has: {kind: string, regex: 'HEAD'}
30+
- has:
31+
kind: pair
32+
all:
33+
- has: {kind: property_identifier, regex: headers}
34+
- has: {kind: identifier, pattern: $HEADERS}
35+
- has:
36+
kind: template_string
37+
all:
38+
- has:
39+
kind: string_fragment
40+
regex: https://
41+
- has:
42+
kind: template_substitution
43+
all:
44+
- has: { kind: identifier, stopBy: end, pattern: $REQ }
45+
- has:
46+
kind: property_identifier
47+
regex: headers
48+
stopBy: end
49+
- has:
50+
kind: property_identifier
51+
regex: host
52+
stopBy: end
53+
- has:
54+
kind: template_substitution
55+
pattern: $URL_PATH
56+
has:
57+
kind: identifier
58+
59+
fix: await (await import("@opennextjs/cloudflare")).getCloudflareContext().env.NEXT_CACHE_REVALIDATION_WORKER.fetch(\`\${$REQ.headers.host.includes("localhost") ? "http":"https" }://\${$REQ.headers.host}$URL_PATH\`,{method:'HEAD', headers:$HEADERS})
60+
`
61+
62+
export function patchResRevalidate(updater: ContentUpdater) {
63+
return updater.updateContent(
64+
"patch-res-revalidate",
65+
{
66+
filter: getCrossPlatformPathRegex(
67+
String.raw`(pages-api\.runtime\.prod\.js|api-resolver\.js)$`,
68+
{ escape: false }
69+
),
70+
contentFilter: /\.trustHostHeader/,
71+
},
72+
({ contents }) => patchCode(contents, rule)
73+
);
74+
}

0 commit comments

Comments
 (0)