豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit 77fe518

Browse files
committed
Fix hydration fallback test typing
1 parent af36c16 commit 77fe518

File tree

3 files changed

+72
-26
lines changed

3 files changed

+72
-26
lines changed

packages/next/src/client/components/router-reducer/create-initial-router-state.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ describe('createInitialRouterState output export fallback', () => {
8787
createInitialRouterState({
8888
navigatedAt: Date.now(),
8989
initialRSCPayload,
90-
location: new URL('https://example.com/hydrated/first/') as Location,
90+
location: new URL(
91+
'https://example.com/hydrated/first/'
92+
) as unknown as Location,
9193
outputExportFallbackBasePath: '/hydrated/__fallback',
9294
})
9395

@@ -122,7 +124,9 @@ describe('createInitialRouterState output export fallback', () => {
122124
...initialRSCPayload,
123125
p: new ReadableStream<Uint8Array>(),
124126
},
125-
location: new URL('https://example.com/hydrated/first/') as Location,
127+
location: new URL(
128+
'https://example.com/hydrated/first/'
129+
) as unknown as Location,
126130
outputExportFallbackBasePath: '/hydrated/__fallback',
127131
})
128132

packages/next/src/client/components/segment-cache/cache.test.ts

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import {
22
HEAD_REQUEST_KEY,
33
type SegmentRequestKey,
44
} from '../../../shared/lib/segment-cache/segment-value-encoding'
5+
import type { VaryParamsThenable } from '../../../shared/lib/segment-cache/vary-params-decoding'
56
import { FetchStrategy } from './types'
67
import { Fallback } from './cache-map'
78
import {
9+
EntryStatus,
810
getOutputExportSegmentRequestUrl,
911
invalidateEntirePrefetchCache,
1012
readSegmentCacheEntry,
@@ -104,13 +106,13 @@ describe('readOrCreateSegmentCacheEntry output export fallback', () => {
104106
)
105107

106108
expect(firstEntry).toBe(secondEntry)
107-
expect(secondTree.varyPath.parent.parent.value).toBe('second')
108-
expect(
109-
(
110-
secondTree.varyPath.parent
111-
.parent as typeof secondTree.varyPath.parent.parent
112-
).value
113-
).not.toBe(Fallback)
109+
const secondThreadVaryPath = secondTree.varyPath.parent?.parent
110+
expect(secondThreadVaryPath).not.toBeNull()
111+
if (secondThreadVaryPath === null) {
112+
throw new Error('expected metadata vary path to include thread params')
113+
}
114+
expect(secondThreadVaryPath.value).toBe('second')
115+
expect(secondThreadVaryPath.value).not.toBe(Fallback)
114116
})
115117

116118
it('rekeys runtime-prefetched fallback segments under generic params', () => {
@@ -137,15 +139,38 @@ describe('readOrCreateSegmentCacheEntry output export fallback', () => {
137139

138140
const firstTree = makeTree('first')
139141
const secondTree = makeTree('second')
142+
const emptyEntry = readOrCreateSegmentCacheEntry(
143+
now,
144+
FetchStrategy.PPRRuntime,
145+
firstTree,
146+
'/t/__fallback'
147+
)
148+
expect(emptyEntry.status).toBe(EntryStatus.Empty)
149+
if (emptyEntry.status !== EntryStatus.Empty) {
150+
throw new Error('expected a new empty segment cache entry')
151+
}
140152
const ownedEntry = upgradeToPendingSegment(
141-
readOrCreateSegmentCacheEntry(
142-
now,
143-
FetchStrategy.PPRRuntime,
144-
firstTree,
145-
'/t/__fallback'
146-
),
153+
emptyEntry,
147154
FetchStrategy.PPRRuntime
148155
)
156+
const fulfilledVaryParams = new Set(['threadId'])
157+
const fulfilledVaryParamsThenable: VaryParamsThenable = {
158+
status: 'fulfilled',
159+
value: fulfilledVaryParams,
160+
then<TResult1 = Set<string>, TResult2 = never>(
161+
onfulfilled?:
162+
| ((value: Set<string>) => TResult1 | PromiseLike<TResult1>)
163+
| null,
164+
_onrejected?:
165+
| ((reason: unknown) => TResult2 | PromiseLike<TResult2>)
166+
| null
167+
): PromiseLike<TResult1 | TResult2> {
168+
if (onfulfilled) {
169+
return Promise.resolve(onfulfilled(fulfilledVaryParams))
170+
}
171+
return Promise.resolve(fulfilledVaryParams as TResult1)
172+
},
173+
}
149174

150175
writeDynamicRenderResponseIntoCache(
151176
now,
@@ -161,7 +186,7 @@ describe('readOrCreateSegmentCacheEntry output export fallback', () => {
161186
{},
162187
null,
163188
false,
164-
Promise.resolve(new Set(['threadId'])),
189+
fulfilledVaryParamsThenable,
165190
],
166191
head: null,
167192
isHeadPartial: false,
@@ -187,6 +212,6 @@ describe('readOrCreateSegmentCacheEntry output export fallback', () => {
187212
const secondEntry = readSegmentCacheEntry(now, secondTree.varyPath)
188213

189214
expect(secondEntry).toBe(ownedEntry)
190-
expect(secondEntry?.status).toBe(2)
215+
expect(secondEntry?.status).toBe(EntryStatus.Fulfilled)
191216
})
192217
})

packages/next/src/client/components/segment-cache/vary-path.test.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
import { Fallback } from './cache-map'
22
import { FetchStrategy } from './types'
3+
import {
4+
HEAD_REQUEST_KEY,
5+
type SegmentRequestKey,
6+
} from '../../../shared/lib/segment-cache/segment-value-encoding'
37
import {
48
finalizeMetadataVaryPath,
59
finalizePageVaryPath,
610
getFulfilledSegmentVaryPath,
711
getSegmentVaryPathForRequest,
812
} from './vary-path'
913

14+
function getRequiredParent<T extends { parent: unknown | null }>(
15+
path: T
16+
): Exclude<T['parent'], null> {
17+
expect(path.parent).not.toBeNull()
18+
return path.parent as Exclude<T['parent'], null>
19+
}
20+
1021
describe('getSegmentVaryPathForRequest output export fallback', () => {
1122
it('reuses path params for normal static prefetches', () => {
1223
const varyPath = finalizeMetadataVaryPath(
@@ -22,8 +33,8 @@ describe('getSegmentVaryPathForRequest output export fallback', () => {
2233
const requestVaryPath = getSegmentVaryPathForRequest(
2334
FetchStrategy.PPR,
2435
{
25-
requestKey: '/_head',
26-
segment: '/_head',
36+
requestKey: HEAD_REQUEST_KEY,
37+
segment: HEAD_REQUEST_KEY,
2738
refreshState: null,
2839
varyPath,
2940
isPage: true,
@@ -33,7 +44,9 @@ describe('getSegmentVaryPathForRequest output export fallback', () => {
3344
null
3445
)
3546

36-
expect(requestVaryPath.parent.parent.value).toBe('j97358')
47+
expect(getRequiredParent(getRequiredParent(requestVaryPath)).value).toBe(
48+
'j97358'
49+
)
3750
})
3851

3952
it('treats path params as reusable for learned output export fallback routes', () => {
@@ -50,8 +63,8 @@ describe('getSegmentVaryPathForRequest output export fallback', () => {
5063
const requestVaryPath = getSegmentVaryPathForRequest(
5164
FetchStrategy.PPR,
5265
{
53-
requestKey: '/t/$d$threadId/__PAGE__',
54-
segment: '/t/$d$threadId',
66+
requestKey: '/t/$d$threadId/__PAGE__' as SegmentRequestKey,
67+
segment: '/t/$d$threadId' as SegmentRequestKey,
5568
refreshState: null,
5669
varyPath,
5770
isPage: true,
@@ -61,8 +74,10 @@ describe('getSegmentVaryPathForRequest output export fallback', () => {
6174
'/t/__fallback'
6275
)
6376

64-
expect(requestVaryPath.parent.value).toBe(Fallback)
65-
expect(requestVaryPath.parent.parent.value).toBe(Fallback)
77+
expect(getRequiredParent(requestVaryPath).value).toBe(Fallback)
78+
expect(getRequiredParent(getRequiredParent(requestVaryPath)).value).toBe(
79+
Fallback
80+
)
6681
})
6782

6883
it('keeps fulfilled fallback segment path params generic even when vary params include them', () => {
@@ -82,7 +97,9 @@ describe('getSegmentVaryPathForRequest output export fallback', () => {
8297
true
8398
)
8499

85-
expect(fulfilledVaryPath.parent.value).toBe('')
86-
expect(fulfilledVaryPath.parent.parent.value).toBe(Fallback)
100+
expect(getRequiredParent(fulfilledVaryPath).value).toBe('')
101+
expect(getRequiredParent(getRequiredParent(fulfilledVaryPath)).value).toBe(
102+
Fallback
103+
)
87104
})
88105
})

0 commit comments

Comments
 (0)