Skip to content

Commit 6c5f9d3

Browse files
Merge pull request #69 from DBB-Software/feat/PLATFORM-next15
feat: updated to support nextjs v15
2 parents a68047e + d594c89 commit 6c5f9d3

File tree

7 files changed

+112
-17
lines changed

7 files changed

+112
-17
lines changed

package-lock.json

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cache-core/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,8 @@
5050
"cookie": "0.6.0",
5151
"ua-parser-js": "1.0.37",
5252
"path-to-regexp": "6.2.2"
53+
},
54+
"peerDependencies": {
55+
"next": "^14.1.0 || ^15.0.0"
5356
}
5457
}

packages/cache-core/src/cacheHandler.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import parser from 'ua-parser-js'
44
import crypto from 'crypto'
55
import { pathToRegexp } from 'path-to-regexp'
66
import { NEXT_CACHE_IMPLICIT_TAG_ID } from 'next/dist/lib/constants'
7-
import type {
7+
import {
88
NextCacheHandlerContext,
99
CacheHandlerContext,
1010
CacheEntry,
1111
IncrementalCacheValue,
1212
CacheConfig,
13-
CacheHandler
13+
CacheHandler,
14+
CachedRouteKind
1415
} from '../src/types'
1516
import { ConsoleLogger } from './logger'
1617
import { FileSystemCache } from './strategies/fileSystem'
@@ -184,7 +185,7 @@ export class Cache implements CacheHandler {
184185
}
185186

186187
private transformCacheData(data: CacheEntry): CacheEntry {
187-
if (data.value?.kind === 'ROUTE') {
188+
if (data.value?.kind === CachedRouteKind.ROUTE || data.value?.kind === CachedRouteKind.APP_ROUTE) {
188189
return {
189190
...data,
190191
value: {
@@ -193,12 +194,21 @@ export class Cache implements CacheHandler {
193194
}
194195
}
195196
}
197+
if (data.value?.kind === CachedRouteKind.APP_PAGE) {
198+
return {
199+
...data,
200+
value: {
201+
...data.value,
202+
rscData: Buffer.from(data.value.rscData as unknown as string, 'base64')
203+
}
204+
}
205+
}
196206
return data
197207
}
198208

199209
async set(pageKey: string, data: IncrementalCacheValue | null, ctx: CacheHandlerContext): Promise<void> {
200210
try {
201-
if (!this.checkIsPathToCache(pageKey) || ['IMAGE', 'REDIRECT', 'FETCH'].includes(data?.kind ?? '')) return
211+
if (!this.checkIsPathToCache(pageKey)) return
202212

203213
Cache.config.logger.info(`Writing cache for ${pageKey}`)
204214
const context = {

packages/cache-core/src/types/index.ts

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,81 @@
1-
import type { IncrementalCacheKindHint, IncrementalCacheValue } from 'next/dist/server/response-cache'
1+
import type { IncrementalCacheKindHint } from 'next/dist/server/response-cache'
2+
import type { OutgoingHttpHeaders } from 'http'
23
import type { CacheHandlerContext as NextCacheHandlerContext } from 'next/dist/server/lib/incremental-cache'
34
import type { BaseLogger } from '../logger'
45

5-
export { NextCacheHandlerContext, IncrementalCacheValue, IncrementalCacheKindHint }
6+
export { NextCacheHandlerContext, IncrementalCacheKindHint }
7+
8+
export type AnyObject = Record<string, any>
9+
10+
export const enum CachedRouteKind {
11+
APP_PAGE = 'APP_PAGE',
12+
APP_ROUTE = 'APP_ROUTE',
13+
ROUTE = 'ROUTE',
14+
PAGES = 'PAGES',
15+
PAGE = 'PAGE',
16+
FETCH = 'FETCH',
17+
REDIRECT = 'REDIRECT',
18+
IMAGE = 'IMAGE'
19+
}
20+
21+
export type CachedFetchData = {
22+
headers: Record<string, string>
23+
body: string
24+
url: string
25+
status?: number
26+
}
27+
28+
export interface CachedFetchValue {
29+
kind: CachedRouteKind.FETCH
30+
data: CachedFetchData
31+
tags?: string[]
32+
revalidate: number
33+
}
34+
export interface CachedRedirectValue {
35+
kind: CachedRouteKind.REDIRECT
36+
props: AnyObject
37+
}
38+
39+
export interface IncrementalCachedAppPageValue {
40+
kind: CachedRouteKind.APP_PAGE
41+
html: string
42+
rscData: Buffer | undefined
43+
headers: OutgoingHttpHeaders | undefined
44+
postponed: string | undefined
45+
status: number | undefined
46+
segmentData: Map<string, Buffer> | undefined
47+
}
48+
export interface IncrementalCachedPageValue {
49+
kind: CachedRouteKind.PAGES | CachedRouteKind.PAGE
50+
html: string
51+
pageData: AnyObject
52+
headers: OutgoingHttpHeaders | undefined
53+
status: number | undefined
54+
}
55+
56+
export interface CachedRouteValue {
57+
kind: CachedRouteKind.APP_ROUTE | CachedRouteKind.ROUTE
58+
body: Buffer
59+
status: number
60+
headers: OutgoingHttpHeaders
61+
}
62+
export interface CachedImageValue {
63+
kind: CachedRouteKind.IMAGE
64+
etag: string
65+
upstreamEtag: string
66+
buffer: Buffer
67+
extension: string
68+
isMiss?: boolean
69+
isStale?: boolean
70+
}
71+
72+
export type IncrementalCacheValue =
73+
| CachedRedirectValue
74+
| IncrementalCachedPageValue
75+
| IncrementalCachedAppPageValue
76+
| CachedImageValue
77+
| CachedFetchValue
78+
| CachedRouteValue
679

780
export interface CacheEntry {
881
value: IncrementalCacheValue | null

packages/common/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"build": "rm -rf lib && rollup -c --bundleConfigAsCjs"
99
},
1010
"peerDependencies": {
11-
"next": "^14.1.0"
11+
"next": "^14.1.0 || ^15.0.0"
1212
},
1313
"devDependencies": {
1414
"tsconfig": "*"

packages/common/src/utils/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import { IncrementalCacheValue } from 'next/dist/server/response-cache'
21
import { NEXT_CACHE_TAGS_HEADER } from 'next/dist/lib/constants'
32

43
export const chunkArray = <T>(array: T[], chunkSize: number): T[][] =>
54
Array.from({ length: Math.ceil(array.length / chunkSize) }, (v, i) =>
65
array.slice(i * chunkSize, i * chunkSize + chunkSize)
76
)
87

9-
export const checkHeaderTags = (value: IncrementalCacheValue | null, tag: string) =>
10-
(value?.kind === 'PAGE' || value?.kind === 'ROUTE') &&
11-
value.headers?.[NEXT_CACHE_TAGS_HEADER]?.toString()?.split(',').includes(tag)
8+
export const checkHeaderTags = (value: Record<string, any> | null, tag: string) =>
9+
value?.headers?.[NEXT_CACHE_TAGS_HEADER]?.toString()?.split(',').includes(tag)

packages/redis-cache/src/RedisStack.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
RedisScripts,
1010
SchemaFieldTypes
1111
} from 'redis'
12-
import type { CacheEntry } from '@dbbs/next-cache-handler-core'
12+
import { CachedRouteKind, type CacheEntry } from '@dbbs/next-cache-handler-core'
1313
import { RedisAdapter, RedisJSON } from './types'
1414
import { CHUNK_LIMIT } from './constants'
1515

@@ -54,10 +54,18 @@ export class RedisStack implements RedisAdapter {
5454
}
5555

5656
async set(pageKey: string, cacheKey: string, data: CacheEntry): Promise<void> {
57-
const headersTags =
58-
data?.value?.kind === 'PAGE' || data?.value?.kind === 'ROUTE'
59-
? data?.value?.headers?.[NEXT_CACHE_TAGS_HEADER]?.toString()
60-
: ''
57+
const kind = data?.value?.kind
58+
let headersTags = ''
59+
60+
if (
61+
kind === CachedRouteKind.PAGE ||
62+
kind === CachedRouteKind.PAGES ||
63+
kind === CachedRouteKind.ROUTE ||
64+
kind === CachedRouteKind.APP_ROUTE
65+
) {
66+
headersTags = data?.value?.headers?.[NEXT_CACHE_TAGS_HEADER]?.toString() ?? ''
67+
}
68+
6169
const generalTags = [headersTags, data?.tags?.join(SEPARATOR)].filter(Boolean).join(SEPARATOR)
6270

6371
const cacheData = { ...data, currentCacheKey: cacheKey, generalTags }

0 commit comments

Comments
 (0)