Skip to content

Commit 2a5464f

Browse files
committed
Fixed instrumentation kind mapping
1 parent 2bb7c0b commit 2a5464f

File tree

6 files changed

+108
-46
lines changed

6 files changed

+108
-46
lines changed

README.md

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,43 @@
44

55
This package extends the functionality of [`@neshca/cache-handler`](https://www.npmjs.com/package/@neshca/cache-handler) by providing additional cache handlers for specialized use cases, specifically for Redis-based caching solutions. The original `@neshca/cache-handler` offers a robust caching API for Next.js applications, and this package introduces two new handlers for managing Redis cache with different expiration strategies and tag-based revalidation.
66

7+
8+
## Migration
9+
10+
### 1.2.x -> 1.3.x
11+
12+
#### cache-handler
13+
1.2.x
14+
```
15+
const { Next15CacheHandler } = require("@fortedigital/nextjs-cache-handler/next-15-cache-handler");
16+
module.exports = new Next15CacheHandler();
17+
```
18+
19+
1.3.x
20+
```
21+
const { Next15CacheHandler } = require("@fortedigital/nextjs-cache-handler");
22+
module.exports = Next15CacheHandler;
23+
```
24+
25+
#### instrumentation
26+
1.2.x
27+
```
28+
if (process.env.NEXT_RUNTIME === "nodejs") {
29+
const { registerInitialCache } = await import('@neshca/cache-handler/instrumentation')
30+
const CacheHandler = (await import("./cache-handler.js")).default;
31+
await registerInitialCache(CacheHandler);
32+
}
33+
```
34+
35+
1.3.x
36+
```
37+
if (process.env.NEXT_RUNTIME === "nodejs") {
38+
const { registerInitialCache } = await import("@fortedigital/nextjs-cache-handler/instrumentation");
39+
const CacheHandler = (await import("./cache-handler.js")).default;
40+
await registerInitialCache(CacheHandler);
41+
}
42+
```
43+
744
## Installation
845

946
To install this package along with its dependencies:
@@ -53,15 +90,13 @@ Use this:
5390

5491
```js
5592
const { CacheHandler } = require("@neshca/cache-handler");
56-
const {
57-
Next15CacheHandler,
58-
} = require("@fortedigital/nextjs-cache-handler/next-15-cache-handler");
93+
const { Next15CacheHandler } = require("@fortedigital/nextjs-cache-handler");
5994

6095
CacheHandler.onCreation(() => {
6196
// your usual setup
6297
});
6398

64-
module.exports = new Next15CacheHandler();
99+
module.exports = Next15CacheHandler;
65100
```
66101

67102
### Instrumentation
@@ -270,7 +305,7 @@ CacheHandler.onCreation(() => {
270305
return global.cacheHandlerConfigPromise;
271306
});
272307

273-
module.exports = new Next15CacheHandler();
308+
module.exports = Next15CacheHandler;
274309
```
275310
276311
## Reference to Original Package

packages/nextjs-cache-handler/package.json

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,20 @@
3030
},
3131
"typesVersions": {
3232
"*": {
33-
"Next15CacheHandler": [
33+
"*": [
3434
"dist/handlers/next-15-cache-handler.d.ts"
3535
],
36-
"composite": [
37-
"dist/handlers/composite.d.ts"
38-
],
39-
"redis-strings": [
40-
"dist/handlers/redis-strings.d.ts"
36+
"instrumentation": [
37+
"dist/instrumentation/instrumentation.d.ts"
4138
],
4239
"buffer-string-decorator": [
4340
"dist/handlers/buffer-string-decorator.d.ts"
4441
],
45-
"instrumentation/instrumentation": [
46-
"dist/instrumentation/instrumentation.d.ts"
42+
"composite": [
43+
"dist/handlers/composite.d.ts"
4744
],
48-
"instrumentation/register-initial-cache": [
49-
"dist/instrumentation/register-initial-cache.d.ts"
45+
"redis-strings": [
46+
"dist/handlers/redis-strings.d.ts"
5047
]
5148
}
5249
},
Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,64 @@
11
import { CacheHandler } from "@neshca/cache-handler";
2+
import { OutgoingHttpHeaders } from "http";
23
import {
4+
CachedRedirectValue,
5+
CachedImageValue,
36
CachedFetchValue,
47
IncrementalCacheValue,
58
} from "next/dist/server/response-cache";
69

10+
export interface CachedRouteValue {
11+
kind: "APP_ROUTE";
12+
body: Buffer;
13+
status: number;
14+
headers: OutgoingHttpHeaders;
15+
}
16+
17+
interface IncrementalCachedPageValue {
18+
kind: "APP_PAGE";
19+
html: string;
20+
pageData: Object;
21+
postponed: string | undefined;
22+
headers: OutgoingHttpHeaders | undefined;
23+
status: number | undefined;
24+
}
25+
26+
export type Next15IncrementalCacheValue =
27+
| CachedRedirectValue
28+
| IncrementalCachedPageValue
29+
| CachedImageValue
30+
| CachedFetchValue
31+
| CachedRouteValue;
32+
733
/*
834
* Use this handler in Next 15.2.1 and higher after `revalidate` property had been removed from context object.
935
* https://github.com/caching-tools/next-shared-cache/issues/1027
1036
* https://github.com/vercel/next.js/commit/6a1b6336a3062dfdb1b9dddcee6b836da94fd198
1137
*/
12-
export function Next15CacheHandler() {
13-
return class extends CacheHandler {
14-
async set(
15-
cacheKey: string,
16-
incrementalCacheValue: IncrementalCacheValue | null,
17-
ctx: {
18-
revalidate?: number | false;
19-
fetchCache?: boolean;
20-
fetchUrl?: string;
21-
fetchIdx?: number;
22-
tags?: string[];
23-
} & { neshca_lastModified?: number },
24-
) {
25-
await super.set(cacheKey, incrementalCacheValue, {
38+
export class Next15CacheHandler extends CacheHandler {
39+
async set(
40+
cacheKey: string,
41+
incrementalCacheValue:
42+
| IncrementalCacheValue
43+
| Next15IncrementalCacheValue
44+
| null,
45+
ctx: {
46+
revalidate?: number | false;
47+
fetchCache?: boolean;
48+
fetchUrl?: string;
49+
fetchIdx?: number;
50+
tags?: string[];
51+
} & { neshca_lastModified?: number },
52+
) {
53+
await super.set(
54+
cacheKey,
55+
incrementalCacheValue as unknown as IncrementalCacheValue,
56+
{
2657
...ctx,
2758
revalidate:
2859
ctx.revalidate ||
2960
(incrementalCacheValue as CachedFetchValue)?.revalidate,
30-
});
31-
}
32-
};
61+
},
62+
);
63+
}
3364
}

packages/nextjs-cache-handler/src/instrumentation/helpers/getTagsFromHeaders.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ import type { OutgoingHttpHeaders } from "http";
99
*/
1010

1111
export function getTagsFromHeaders(headers: OutgoingHttpHeaders): string[] {
12-
const tagsHeader = headers["x-next-cache-tags"];
12+
const tagsHeader = headers["x-next-cache-tags"];
1313

14-
if (Array.isArray(tagsHeader)) {
15-
return tagsHeader;
16-
}
14+
if (Array.isArray(tagsHeader)) {
15+
return tagsHeader;
16+
}
1717

18-
if (typeof tagsHeader === "string") {
19-
return tagsHeader.split(",");
20-
}
18+
if (typeof tagsHeader === "string") {
19+
return tagsHeader.split(",");
20+
}
2121

22-
return [];
22+
return [];
2323
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export {
22
registerInitialCache,
33
type RegisterInitialCacheOptions,
4-
} from './register-initial-cache';
4+
} from "./register-initial-cache";

packages/nextjs-cache-handler/src/instrumentation/register-initial-cache.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ import path from "node:path";
33
import { PRERENDER_MANIFEST, SERVER_DIRECTORY } from "next/constants";
44
import type { PrerenderManifest } from "next/dist/build";
55
import { CACHE_ONE_YEAR } from "next/dist/lib/constants";
6-
import { CacheHandler } from "@neshca/cache-handler";
7-
import { Next15CacheHandler } from "../handlers/next-15-cache-handler";
86
import { Revalidate } from "next/dist/server/lib/revalidate";
97
import { CachedFetchValue } from "next/dist/server/response-cache";
108
import type { OutgoingHttpHeaders } from "http";
119
import { getTagsFromHeaders } from "./helpers/getTagsFromHeaders";
1210

13-
type CacheHandlerType = typeof import('../handlers/next-15-cache-handler').Next15CacheHandler;
11+
type CacheHandlerType =
12+
typeof import("../handlers/next-15-cache-handler").Next15CacheHandler;
1413

1514
type NextRouteMetadata = {
1615
status: number | undefined;
@@ -202,7 +201,7 @@ export async function registerInitialCache(
202201
await cacheHandler.set(
203202
cachePath,
204203
{
205-
kind: "ROUTE",
204+
kind: "APP_ROUTE",
206205
body,
207206
headers: meta.headers,
208207
status: meta.status,
@@ -289,7 +288,7 @@ export async function registerInitialCache(
289288
await cacheHandler.set(
290289
cachePath,
291290
{
292-
kind: "PAGE",
291+
kind: "APP_PAGE",
293292
html,
294293
pageData,
295294
postponed: meta?.postponed,

0 commit comments

Comments
 (0)