@opennextjs/cloudflare 0.5.9 → 0.5.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -4,6 +4,8 @@ export declare const DEFAULT_REVALIDATION_TIMEOUT_MS = 10000;
|
|
|
4
4
|
* The Memory Queue offers basic ISR revalidation by directly requesting a revalidation of a route.
|
|
5
5
|
*
|
|
6
6
|
* It offers basic support for in-memory de-duping per isolate.
|
|
7
|
+
*
|
|
8
|
+
* A service binding called `NEXT_CACHE_REVALIDATION_WORKER` that points to your worker is required.
|
|
7
9
|
*/
|
|
8
10
|
export declare class MemoryQueue implements Queue {
|
|
9
11
|
private opts;
|
package/dist/api/memory-queue.js
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import logger from "@opennextjs/aws/logger.js";
|
|
2
|
+
import { IgnorableError } from "@opennextjs/aws/utils/error.js";
|
|
3
|
+
import { getCloudflareContext } from "./cloudflare-context";
|
|
2
4
|
export const DEFAULT_REVALIDATION_TIMEOUT_MS = 10_000;
|
|
3
5
|
/**
|
|
4
6
|
* The Memory Queue offers basic ISR revalidation by directly requesting a revalidation of a route.
|
|
5
7
|
*
|
|
6
8
|
* It offers basic support for in-memory de-duping per isolate.
|
|
9
|
+
*
|
|
10
|
+
* A service binding called `NEXT_CACHE_REVALIDATION_WORKER` that points to your worker is required.
|
|
7
11
|
*/
|
|
8
12
|
export class MemoryQueue {
|
|
9
13
|
opts;
|
|
@@ -13,6 +17,9 @@ export class MemoryQueue {
|
|
|
13
17
|
this.opts = opts;
|
|
14
18
|
}
|
|
15
19
|
async send({ MessageBody: { host, url }, MessageGroupId }) {
|
|
20
|
+
const service = getCloudflareContext().env.NEXT_CACHE_REVALIDATION_WORKER;
|
|
21
|
+
if (!service)
|
|
22
|
+
throw new IgnorableError("No service binding for cache revalidation worker");
|
|
16
23
|
if (this.revalidatedPaths.has(MessageGroupId))
|
|
17
24
|
return;
|
|
18
25
|
this.revalidatedPaths.set(MessageGroupId,
|
|
@@ -23,7 +30,7 @@ export class MemoryQueue {
|
|
|
23
30
|
// TODO: Drop the import - https://github.com/opennextjs/opennextjs-cloudflare/issues/361
|
|
24
31
|
// @ts-ignore
|
|
25
32
|
const manifest = await import("./.next/prerender-manifest.json");
|
|
26
|
-
await
|
|
33
|
+
await service.fetch(`${protocol}://${host}${url}`, {
|
|
27
34
|
method: "HEAD",
|
|
28
35
|
headers: {
|
|
29
36
|
"x-prerender-revalidate": manifest.preview.previewModeId,
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import { generateMessageGroupId } from "@opennextjs/aws/core/routing/queue.js";
|
|
2
2
|
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
|
|
3
|
-
import cache, { DEFAULT_REVALIDATION_TIMEOUT_MS } from "./memory-queue";
|
|
3
|
+
import cache, { DEFAULT_REVALIDATION_TIMEOUT_MS } from "./memory-queue.js";
|
|
4
4
|
vi.mock("./.next/prerender-manifest.json", () => Promise.resolve({ preview: { previewModeId: "id" } }));
|
|
5
|
+
const mockServiceWorkerFetch = vi.fn();
|
|
6
|
+
vi.mock("./cloudflare-context", () => ({
|
|
7
|
+
getCloudflareContext: () => ({
|
|
8
|
+
env: { NEXT_CACHE_REVALIDATION_WORKER: { fetch: mockServiceWorkerFetch } },
|
|
9
|
+
}),
|
|
10
|
+
}));
|
|
5
11
|
describe("MemoryQueue", () => {
|
|
6
12
|
beforeAll(() => {
|
|
7
13
|
vi.useFakeTimers();
|
|
@@ -16,7 +22,7 @@ describe("MemoryQueue", () => {
|
|
|
16
22
|
});
|
|
17
23
|
vi.advanceTimersByTime(DEFAULT_REVALIDATION_TIMEOUT_MS);
|
|
18
24
|
await firstRequest;
|
|
19
|
-
expect(
|
|
25
|
+
expect(mockServiceWorkerFetch).toHaveBeenCalledTimes(1);
|
|
20
26
|
const secondRequest = cache.send({
|
|
21
27
|
MessageBody: { host: "test.local", url: "/test" },
|
|
22
28
|
MessageGroupId: generateMessageGroupId("/test"),
|
|
@@ -24,7 +30,7 @@ describe("MemoryQueue", () => {
|
|
|
24
30
|
});
|
|
25
31
|
vi.advanceTimersByTime(1);
|
|
26
32
|
await secondRequest;
|
|
27
|
-
expect(
|
|
33
|
+
expect(mockServiceWorkerFetch).toHaveBeenCalledTimes(2);
|
|
28
34
|
});
|
|
29
35
|
it("should process revalidations for multiple paths", async () => {
|
|
30
36
|
const firstRequest = cache.send({
|
|
@@ -34,7 +40,7 @@ describe("MemoryQueue", () => {
|
|
|
34
40
|
});
|
|
35
41
|
vi.advanceTimersByTime(1);
|
|
36
42
|
await firstRequest;
|
|
37
|
-
expect(
|
|
43
|
+
expect(mockServiceWorkerFetch).toHaveBeenCalledTimes(1);
|
|
38
44
|
const secondRequest = cache.send({
|
|
39
45
|
MessageBody: { host: "test.local", url: "/test" },
|
|
40
46
|
MessageGroupId: generateMessageGroupId("/other"),
|
|
@@ -42,7 +48,7 @@ describe("MemoryQueue", () => {
|
|
|
42
48
|
});
|
|
43
49
|
vi.advanceTimersByTime(1);
|
|
44
50
|
await secondRequest;
|
|
45
|
-
expect(
|
|
51
|
+
expect(mockServiceWorkerFetch).toHaveBeenCalledTimes(2);
|
|
46
52
|
});
|
|
47
53
|
it("should de-dupe revalidations", async () => {
|
|
48
54
|
const requests = [
|
|
@@ -59,6 +65,6 @@ describe("MemoryQueue", () => {
|
|
|
59
65
|
];
|
|
60
66
|
vi.advanceTimersByTime(1);
|
|
61
67
|
await Promise.all(requests);
|
|
62
|
-
expect(
|
|
68
|
+
expect(mockServiceWorkerFetch).toHaveBeenCalledTimes(1);
|
|
63
69
|
});
|
|
64
70
|
});
|