Skip to content

Commit 312cd28

Browse files
committed
typehell
1 parent 868d8d8 commit 312cd28

File tree

4 files changed

+127
-42
lines changed

4 files changed

+127
-42
lines changed

src/build/content/prerendered.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { satisfies } from 'semver'
1010

1111
import { encodeBlobKey } from '../../shared/blobkey.js'
1212
import type {
13-
CachedFetchValue,
13+
CachedFetchValueForMultipleVersions,
1414
NetlifyCachedAppPageValue,
1515
NetlifyCachedPageValue,
1616
NetlifyCachedRouteValue,
@@ -107,7 +107,9 @@ const buildRouteCacheValue = async (
107107
revalidate: initialRevalidateSeconds,
108108
})
109109

110-
const buildFetchCacheValue = async (path: string): Promise<CachedFetchValue> => ({
110+
const buildFetchCacheValue = async (
111+
path: string,
112+
): Promise<CachedFetchValueForMultipleVersions> => ({
111113
kind: 'FETCH',
112114
...JSON.parse(await readFile(path, 'utf-8')),
113115
})

src/run/handlers/cache.cts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ import { type Span } from '@opentelemetry/api'
1111
import type { PrerenderManifest } from 'next/dist/build/index.js'
1212
import { NEXT_CACHE_TAGS_HEADER } from 'next/dist/lib/constants.js'
1313

14-
import type {
15-
CacheHandler,
16-
CacheHandlerContext,
17-
IncrementalCache,
18-
NetlifyCachedPageValue,
19-
NetlifyCachedRouteValue,
20-
NetlifyCacheHandlerValue,
21-
NetlifyIncrementalCacheValue,
14+
import {
15+
type CacheHandlerContext,
16+
type CacheHandlerForMultipleVersions,
17+
isCachedPageValue,
18+
isCachedRouteValue,
19+
type NetlifyCachedPageValue,
20+
type NetlifyCachedRouteValue,
21+
type NetlifyCacheHandlerValue,
22+
type NetlifyIncrementalCacheValue,
2223
} from '../../shared/cache-types.cjs'
2324
import { getRegionalBlobStore } from '../regional-blob-store.cjs'
2425

@@ -29,7 +30,7 @@ type TagManifest = { revalidatedAt: number }
2930

3031
type TagManifestBlobCache = Record<string, Promise<TagManifest>>
3132

32-
export class NetlifyCacheHandler implements CacheHandler {
33+
export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
3334
options: CacheHandlerContext
3435
revalidatedTags: string[]
3536
blobStore: Store
@@ -190,7 +191,9 @@ export class NetlifyCacheHandler implements CacheHandler {
190191
}
191192
}
192193

193-
async get(...args: Parameters<CacheHandler['get']>): ReturnType<CacheHandler['get']> {
194+
async get(
195+
...args: Parameters<CacheHandlerForMultipleVersions['get']>
196+
): ReturnType<CacheHandlerForMultipleVersions['get']> {
194197
return this.tracer.withActiveSpan('get cache key', async (span) => {
195198
const [key, ctx = {}] = args
196199
getLogger().debug(`[NetlifyCacheHandler.get]: ${key}`)
@@ -282,18 +285,22 @@ export class NetlifyCacheHandler implements CacheHandler {
282285
}
283286

284287
private transformToStorableObject(
285-
data: Parameters<IncrementalCache['set']>[1],
286-
context: Parameters<IncrementalCache['set']>[2],
288+
data: Parameters<CacheHandlerForMultipleVersions['set']>[1],
289+
context: Parameters<CacheHandlerForMultipleVersions['set']>[2],
287290
): NetlifyIncrementalCacheValue | null {
288-
if (data?.kind === 'ROUTE' || data?.kind === 'APP_ROUTE') {
291+
if (!data) {
292+
return null
293+
}
294+
295+
if (isCachedRouteValue(data)) {
289296
return {
290297
...data,
291298
revalidate: context.revalidate,
292299
body: data.body.toString('base64'),
293300
}
294301
}
295302

296-
if (data?.kind === 'PAGE' || data?.kind === 'PAGES') {
303+
if (isCachedPageValue(data)) {
297304
return {
298305
...data,
299306
revalidate: context.revalidate,
@@ -311,7 +318,7 @@ export class NetlifyCacheHandler implements CacheHandler {
311318
return data
312319
}
313320

314-
async set(...args: Parameters<IncrementalCache['set']>) {
321+
async set(...args: Parameters<CacheHandlerForMultipleVersions['set']>) {
315322
return this.tracer.withActiveSpan('set cache key', async (span) => {
316323
const [key, data, context] = args
317324
const blobKey = await this.encodeBlobKey(key)

src/run/handlers/request-context.cts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { LogLevel, systemLogger } from '@netlify/functions/internal'
44

55
import type { NetlifyCachedRouteValue } from '../../shared/cache-types.cjs'
66

7-
// hacks?
7+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
8+
// @ts-ignore - last remaining bit to fix
89
process.env.NODE_ENV = 'production'
910

1011
type SystemLogger = typeof systemLogger

src/shared/cache-types.cts

Lines changed: 99 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,133 @@
11
import type {
2+
CacheHandler,
23
CacheHandlerValue,
3-
IncrementalCache,
44
} from 'next/dist/server/lib/incremental-cache/index.js'
55
import type {
6+
CachedFetchValue,
67
CachedRouteValue,
78
IncrementalCachedAppPageValue,
9+
IncrementalCachedPageValue,
810
IncrementalCacheValue,
911
} from 'next/dist/server/response-cache/types.js'
1012

11-
export type {
12-
CacheHandler,
13-
CacheHandlerContext,
14-
IncrementalCache,
15-
} from 'next/dist/server/lib/incremental-cache/index.js'
13+
export type { CacheHandlerContext } from 'next/dist/server/lib/incremental-cache/index.js'
1614

17-
export type NetlifyCachedRouteValue = Omit<CachedRouteValue, 'body'> & {
15+
/**
16+
* Shape of the cache value that is returned from CacheHandler.get or passed to CacheHandler.set
17+
*/
18+
type CachedRouteValueForMultipleVersions = Omit<CachedRouteValue, 'kind'> & {
19+
kind: 'ROUTE' | 'APP_ROUTE'
20+
}
21+
22+
/**
23+
* Used for storing in blobs and reading from blobs
24+
*/
25+
export type NetlifyCachedRouteValue = Omit<CachedRouteValueForMultipleVersions, 'body'> & {
1826
// Next.js stores body as buffer, while we store it as base64 encoded string
1927
body: string
2028
// Next.js doesn't produce cache-control tag we use to generate cdn cache control
2129
// so store needed values as part of cached response data
22-
revalidate: Parameters<IncrementalCache['set']>[2]['revalidate']
30+
revalidate?: Parameters<CacheHandler['set']>[2]['revalidate']
2331
}
2432

25-
export type NetlifyCachedAppPageValue = Omit<IncrementalCachedAppPageValue, 'rscData'> & {
33+
/**
34+
* Shape of the cache value that is returned from CacheHandler.get or passed to CacheHandler.set
35+
*/
36+
type IncrementalCachedAppPageValueForMultipleVersions = Omit<
37+
IncrementalCachedAppPageValue,
38+
'kind'
39+
> & {
40+
kind: 'APP_PAGE'
41+
}
42+
43+
/**
44+
* Used for storing in blobs and reading from blobs
45+
*/
46+
export type NetlifyCachedAppPageValue = Omit<
47+
IncrementalCachedAppPageValueForMultipleVersions,
48+
'rscData'
49+
> & {
2650
// Next.js stores rscData as buffer, while we store it as base64 encoded string
2751
rscData: string | undefined
28-
revalidate?: Parameters<IncrementalCache['set']>[2]['revalidate']
52+
revalidate?: Parameters<CacheHandler['set']>[2]['revalidate']
2953
}
3054

31-
type CachedPageValue = Extract<IncrementalCacheValue, { kind: 'PAGES' }>
32-
33-
export type NetlifyCachedPageValue = CachedPageValue & {
34-
revalidate?: Parameters<IncrementalCache['set']>[2]['revalidate']
55+
/**
56+
* Shape of the cache value that is returned from CacheHandler.get or passed to CacheHandler.set
57+
*/
58+
type IncrementalCachedPageValueForMultipleVersions = Omit<IncrementalCachedPageValue, 'kind'> & {
59+
kind: 'PAGE' | 'PAGES'
3560
}
3661

37-
export type CachedFetchValue = Extract<IncrementalCacheValue, { kind: 'FETCH' }>
62+
/**
63+
* Used for storing in blobs and reading from blobs
64+
*/
65+
export type NetlifyCachedPageValue = IncrementalCachedPageValueForMultipleVersions & {
66+
revalidate?: Parameters<CacheHandler['set']>[2]['revalidate']
67+
}
3868

39-
export type NetlifyIncrementalCacheValue =
40-
| Exclude<
41-
IncrementalCacheValue,
42-
CachedRouteValue | CachedPageValue | IncrementalCachedAppPageValue
43-
>
44-
| NetlifyCachedRouteValue
45-
| NetlifyCachedPageValue
46-
| NetlifyCachedAppPageValue
69+
export type CachedFetchValueForMultipleVersions = Omit<CachedFetchValue, 'kind'> & {
70+
kind: 'FETCH'
71+
}
4772

4873
type CachedRouteValueToNetlify<T> = T extends CachedRouteValue
4974
? NetlifyCachedRouteValue
50-
: T extends CachedPageValue
75+
: T extends IncrementalCachedPageValue
5176
? NetlifyCachedPageValue
5277
: T extends IncrementalCachedAppPageValue
5378
? NetlifyCachedAppPageValue
5479
: T
5580

5681
type MapCachedRouteValueToNetlify<T> = { [K in keyof T]: CachedRouteValueToNetlify<T[K]> }
5782

83+
/**
84+
* Used for storing in blobs and reading from blobs
85+
*/
5886
export type NetlifyCacheHandlerValue = MapCachedRouteValueToNetlify<CacheHandlerValue>
87+
88+
/**
89+
* Used for storing in blobs and reading from blobs
90+
*/
91+
export type NetlifyIncrementalCacheValue = NetlifyCacheHandlerValue['value']
92+
93+
// type IncrementalCacheValueToMultipleVersions<T> = T extends CachedRouteValue ?
94+
type IncrementalCacheValueToMultipleVersions<T> = T extends CachedRouteValue
95+
? CachedRouteValueForMultipleVersions
96+
: T extends IncrementalCachedPageValue
97+
? IncrementalCachedPageValueForMultipleVersions
98+
: T extends IncrementalCachedAppPageValue
99+
? IncrementalCachedAppPageValueForMultipleVersions
100+
: T extends CachedFetchValue
101+
? CachedFetchValueForMultipleVersions
102+
: T extends CacheHandlerValue
103+
? {
104+
[K in keyof T]: IncrementalCacheValueToMultipleVersions<T[K]>
105+
}
106+
: T
107+
108+
type IncrementalCacheValueForMultipleVersions =
109+
IncrementalCacheValueToMultipleVersions<IncrementalCacheValue>
110+
111+
export const isCachedPageValue = (
112+
value: IncrementalCacheValueForMultipleVersions,
113+
): value is IncrementalCachedPageValueForMultipleVersions =>
114+
value.kind === 'PAGE' || value.kind === 'PAGES'
115+
116+
export const isCachedRouteValue = (
117+
value: IncrementalCacheValueForMultipleVersions,
118+
): value is CachedRouteValueForMultipleVersions =>
119+
value.kind === 'ROUTE' || value.kind === 'APP_ROUTE'
120+
121+
type MapArgsOrReturn<T> = T extends readonly any[]
122+
? { [K in keyof T]: MapArgsOrReturn<T[K]> }
123+
: T extends Promise<infer P>
124+
? Promise<MapArgsOrReturn<P>>
125+
: IncrementalCacheValueToMultipleVersions<T>
126+
127+
type MapCacheHandlerClassMethod<T> = T extends (...args: infer Args) => infer Ret
128+
? (...args: MapArgsOrReturn<Args>) => MapArgsOrReturn<Ret>
129+
: T
130+
131+
type MapCacheHandlerClass<T> = { [K in keyof T]: MapCacheHandlerClassMethod<T[K]> }
132+
133+
export type CacheHandlerForMultipleVersions = MapCacheHandlerClass<CacheHandler>

0 commit comments

Comments
 (0)