-
Notifications
You must be signed in to change notification settings - Fork 73
fix res.revalidate #481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
fix res.revalidate #481
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c987630
fix res.revalidate
conico974 d004241
lint
conico974 56feb35
fix rebase and move to CodePatcher
conico974 23633a0
add unit test
conico974 6dae82b
added e2e test as well
conico974 9e88ac2
add comment and changeset
conico974 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
--- | ||
"@opennextjs/cloudflare": patch | ||
--- | ||
|
||
fix `res.revalidate` not working in page router api route |
This file contains hidden or 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,19 @@ | ||
import { expect, test } from "@playwright/test"; | ||
|
||
test("`res.revalidate` should revalidate the ssg page", async ({ page, request }) => { | ||
await page.goto("/ssg/"); | ||
const initialTime = await page.getByTestId("time").textContent(); | ||
|
||
await page.reload(); | ||
const newTime = await page.getByTestId("time").textContent(); | ||
|
||
expect(initialTime).toBe(newTime); | ||
|
||
const revalidateResult = await request.post("/api/revalidate"); | ||
expect(revalidateResult.status()).toBe(200); | ||
expect(await revalidateResult.json()).toEqual({ hello: "OpenNext rocks!" }); | ||
|
||
await page.reload(); | ||
const revalidatedTime = await page.getByTestId("time").textContent(); | ||
expect(initialTime).not.toBe(revalidatedTime); | ||
}); |
This file contains hidden or 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,11 @@ | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
try { | ||
await res.revalidate("/ssg/"); | ||
return res.json({ hello: "OpenNext rocks!" }); | ||
} catch (e) { | ||
console.error(e); | ||
return res.status(500).json({ error: "An error occurred" }); | ||
} | ||
} |
This file contains hidden or 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,21 @@ | ||
import type { InferGetStaticPropsType } from "next"; | ||
import Link from "next/link"; | ||
|
||
export async function getStaticProps() { | ||
return { | ||
props: { | ||
time: new Date().toISOString(), | ||
}, | ||
}; | ||
} | ||
|
||
export default function Page({ time }: InferGetStaticPropsType<typeof getStaticProps>) { | ||
return ( | ||
<div> | ||
<div className="flex" data-testid="time"> | ||
Time: {time} | ||
</div> | ||
<Link href="/">Home</Link> | ||
</div> | ||
); | ||
} |
This file contains hidden or 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
148 changes: 148 additions & 0 deletions
148
packages/cloudflare/src/cli/build/patches/plugins/res-revalidate.spec.ts
Large diffs are not rendered by default.
Oops, something went wrong.
83 changes: 83 additions & 0 deletions
83
packages/cloudflare/src/cli/build/patches/plugins/res-revalidate.ts
This file contains hidden or 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,83 @@ | ||
/** | ||
* This patch will replace code used for `res.revalidate` in page router | ||
* Without the patch it uses `fetch` to make a call to itself, which doesn't work once deployed in cloudflare workers | ||
* This patch will replace this fetch by a call to `NEXT_CACHE_REVALIDATION_WORKER` service binding | ||
*/ | ||
import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js"; | ||
import type { CodePatcher } from "@opennextjs/aws/build/patch/codePatcher.js"; | ||
import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js"; | ||
|
||
export const rule = ` | ||
rule: | ||
kind: await_expression | ||
inside: | ||
kind: if_statement | ||
stopBy: end | ||
has: | ||
kind: parenthesized_expression | ||
has: { kind: property_identifier , stopBy: end, regex: trustHostHeader} | ||
has: | ||
kind: call_expression | ||
all: | ||
- has: {kind: identifier, pattern: fetch} | ||
- has: | ||
kind: arguments | ||
all: | ||
- has: | ||
kind: object | ||
all: | ||
- has: | ||
kind: pair | ||
all: | ||
- has: {kind: property_identifier, regex: method } | ||
- has: {kind: string, regex: 'HEAD'} | ||
- has: | ||
kind: pair | ||
all: | ||
- has: {kind: property_identifier, regex: headers} | ||
- has: {kind: identifier, pattern: $HEADERS} | ||
- has: | ||
kind: template_string | ||
all: | ||
- has: | ||
kind: string_fragment | ||
regex: https:// | ||
- has: | ||
kind: template_substitution | ||
all: | ||
- has: { kind: identifier, stopBy: end, pattern: $REQ } | ||
- has: | ||
kind: property_identifier | ||
regex: headers | ||
stopBy: end | ||
- has: | ||
kind: property_identifier | ||
regex: host | ||
stopBy: end | ||
- has: | ||
kind: template_substitution | ||
pattern: $URL_PATH | ||
has: | ||
kind: identifier | ||
|
||
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}) | ||
`; | ||
|
||
export const patchResRevalidate: CodePatcher = { | ||
name: "patch-res-revalidate", | ||
patches: [ | ||
{ | ||
versions: ">=14.2.0", | ||
field: { | ||
pathFilter: getCrossPlatformPathRegex( | ||
String.raw`(pages-api\.runtime\.prod\.js|node/api-resolver\.js)$`, | ||
{ | ||
escape: false, | ||
} | ||
), | ||
contentFilter: /\.trustHostHeader/, | ||
patchCode: async ({ code }) => patchCode(code, rule), | ||
}, | ||
}, | ||
], | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.