-
Notifications
You must be signed in to change notification settings - Fork 28k
/
Copy pathutils.ts
67 lines (62 loc) · 2.04 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import type { IncrementalCacheItem, ResponseCacheEntry } from './types'
import RenderResult from '../render-result'
export async function fromResponseCacheEntry(
cacheEntry: ResponseCacheEntry
): Promise<IncrementalCacheItem> {
return {
...cacheEntry,
value:
cacheEntry.value?.kind === 'PAGE'
? {
kind: 'PAGE',
html: await cacheEntry.value.html.toUnchunkedString(true),
pageData: cacheEntry.value.pageData,
headers: cacheEntry.value.headers,
status: cacheEntry.value.status,
}
: cacheEntry.value?.kind === 'APP_PAGE'
? {
kind: 'APP_PAGE',
html: await cacheEntry.value.html.toUnchunkedString(true),
postponed: cacheEntry.value.postponed,
rscData: cacheEntry.value.rscData,
headers: cacheEntry.value.headers,
status: cacheEntry.value.status,
}
: cacheEntry.value,
}
}
export async function toResponseCacheEntry(
response: IncrementalCacheItem
): Promise<ResponseCacheEntry | null> {
if (!response) return null
if (response.value?.kind === 'FETCH') {
throw new Error(
'Invariant: unexpected cachedResponse of kind fetch in response cache'
)
}
return {
isMiss: response.isMiss,
isStale: response.isStale,
revalidate: response.revalidate,
value:
response.value?.kind === 'PAGE'
? {
kind: 'PAGE',
html: RenderResult.fromStatic(response.value.html),
pageData: response.value.pageData,
headers: response.value.headers,
status: response.value.status,
}
: response.value?.kind === 'APP_PAGE'
? {
kind: 'APP_PAGE',
html: RenderResult.fromStatic(response.value.html),
rscData: response.value.rscData,
headers: response.value.headers,
status: response.value.status,
postponed: response.value.postponed,
}
: response.value,
}
}