-
Notifications
You must be signed in to change notification settings - Fork 28.2k
/
Copy pathdynamic-data.test.ts
317 lines (290 loc) · 11.4 KB
/
dynamic-data.test.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import { nextTestSetup } from 'e2e-utils'
import { assertHasRedbox, getRedboxHeader } from 'next-test-utils'
process.env.__TEST_SENTINEL = 'at buildtime'
describe('dynamic-data', () => {
const { next, isNextDev, skipped } = nextTestSetup({
files: __dirname + '/fixtures/main',
skipDeployment: true,
})
if (skipped) {
return
}
it('should render the dynamic apis dynamically when used in a top-level scope', async () => {
const $ = await next.render$(
'/top-level?foo=foosearch',
{},
{
headers: {
fooheader: 'foo header value',
cookie: 'foocookie=foo cookie value',
},
}
)
if (isNextDev) {
// in dev we expect the entire page to be rendered at runtime
expect($('#layout').text()).toBe('at runtime')
expect($('#page').text()).toBe('at runtime')
} else if (process.env.__NEXT_EXPERIMENTAL_PPR) {
// in PPR we expect the shell to be rendered at build and the page to be rendered at runtime
expect($('#layout').text()).toBe('at buildtime')
expect($('#page').text()).toBe('at runtime')
} else {
// in static generation we expect the entire page to be rendered at runtime
expect($('#layout').text()).toBe('at runtime')
expect($('#page').text()).toBe('at runtime')
}
expect($('#headers .fooheader').text()).toBe('foo header value')
expect($('#cookies .foocookie').text()).toBe('foo cookie value')
expect($('#searchparams .foo').text()).toBe('foosearch')
})
it('should render the dynamic apis dynamically when used in a top-level scope with force dynamic', async () => {
const $ = await next.render$(
'/force-dynamic?foo=foosearch',
{},
{
headers: {
fooheader: 'foo header value',
cookie: 'foocookie=foo cookie value',
},
}
)
if (isNextDev) {
// in dev we expect the entire page to be rendered at runtime
expect($('#layout').text()).toBe('at runtime')
expect($('#page').text()).toBe('at runtime')
} else if (process.env.__NEXT_EXPERIMENTAL_PPR) {
// @TODO this should actually be build but there is a bug in how we do segment level dynamic in PPR at the moment
// see note in create-component-tree
expect($('#layout').text()).toBe('at runtime')
expect($('#page').text()).toBe('at runtime')
} else {
// in static generation we expect the entire page to be rendered at runtime
expect($('#layout').text()).toBe('at runtime')
expect($('#page').text()).toBe('at runtime')
}
expect($('#headers .fooheader').text()).toBe('foo header value')
expect($('#cookies .foocookie').text()).toBe('foo cookie value')
expect($('#searchparams .foo').text()).toBe('foosearch')
})
it('should render empty objects for dynamic APIs when rendering with force-static', async () => {
const $ = await next.render$(
'/force-static?foo=foosearch',
{},
{
headers: {
fooheader: 'foo header value',
cookie: 'foocookie=foo cookie value',
},
}
)
if (isNextDev) {
// in dev we expect the entire page to be rendered at runtime
expect($('#layout').text()).toBe('at runtime')
expect($('#page').text()).toBe('at runtime')
} else if (process.env.__NEXT_EXPERIMENTAL_PPR) {
// in PPR we expect the shell to be rendered at build and the page to be rendered at runtime
expect($('#layout').text()).toBe('at buildtime')
expect($('#page').text()).toBe('at buildtime')
// we expect there to be a suspense boundary in fallback state
expect($('#boundary').html()).toBeNull()
} else {
// in static generation we expect the entire page to be rendered at runtime
expect($('#layout').text()).toBe('at buildtime')
expect($('#page').text()).toBe('at buildtime')
// we expect there to be no suspense boundary in fallback state
expect($('#boundary').html()).toBeNull()
}
expect($('#headers .fooheader').html()).toBeNull()
expect($('#cookies .foocookie').html()).toBeNull()
expect($('#searchparams .foo').html()).toBeNull()
})
it('should track searchParams access as dynamic when the Page is a client component', async () => {
const $ = await next.render$(
'/client-page?foo=foosearch',
{},
{
headers: {
fooheader: 'foo header value',
cookie: 'foocookie=foo cookie value',
},
}
)
if (isNextDev) {
// in dev we expect the entire page to be rendered at runtime
expect($('#layout').text()).toBe('at runtime')
expect($('#page').text()).toBe('at runtime')
// we don't assert the state of the fallback because it can depend on the timing
// of when streaming starts and how fast the client references resolve
} else if (process.env.__NEXT_EXPERIMENTAL_PPR) {
// in PPR we expect the shell to be rendered at build and the page to be rendered at runtime
expect($('#layout').text()).toBe('at buildtime')
expect($('#page').text()).toBe('at runtime')
// we expect there to be a suspense boundary in fallback state
expect($('#boundary').html()).not.toBeNull()
} else {
// in static generation we expect the entire page to be rendered at runtime
expect($('#layout').text()).toBe('at runtime')
expect($('#page').text()).toBe('at runtime')
// we don't assert the state of the fallback because it can depend on the timing
// of when streaming starts and how fast the client references resolve
}
expect($('#searchparams .foo').text()).toBe('foosearch')
})
if (!isNextDev) {
it('should track dynamic apis when rendering app routes', async () => {
expect(next.cliOutput).toContain(
`Caught Error: Dynamic server usage: Route /routes/url couldn't be rendered statically because it used \`request.url\`.`
)
expect(next.cliOutput).toContain(
`Caught Error: Dynamic server usage: Route /routes/next-url couldn't be rendered statically because it used \`nextUrl.toString\`.`
)
})
}
})
describe('dynamic-data with dynamic = "error"', () => {
const { next, isNextDev, isNextDeploy, skipped } = nextTestSetup({
files: __dirname + '/fixtures/require-static',
skipStart: true,
})
if (skipped) {
return
}
if (isNextDeploy) {
it.skip('should not run in next deploy.', () => {})
return
}
if (isNextDev) {
beforeAll(async () => {
await next.start()
})
it('displays redbox when `dynamic = "error"` and dynamic data is read in dev', async () => {
let browser = await next.browser('/cookies?foo=foosearch')
try {
await assertHasRedbox(browser)
expect(await getRedboxHeader(browser)).toMatch(
'Error: Route /cookies with `dynamic = "error"` couldn\'t be rendered statically because it used `cookies`'
)
} finally {
await browser.close()
}
browser = await next.browser('/connection')
try {
await assertHasRedbox(browser)
expect(await getRedboxHeader(browser)).toMatch(
'Error: Route /connection with `dynamic = "error"` couldn\'t be rendered statically because it used `connection`'
)
} finally {
await browser.close()
}
browser = await next.browser('/headers?foo=foosearch')
try {
await assertHasRedbox(browser)
expect(await getRedboxHeader(browser)).toMatch(
'Error: Route /headers with `dynamic = "error"` couldn\'t be rendered statically because it used `headers`'
)
} finally {
await browser.close()
}
browser = await next.browser('/search?foo=foosearch')
try {
await assertHasRedbox(browser)
expect(await getRedboxHeader(browser)).toMatch(
'Error: Route /search with `dynamic = "error"` couldn\'t be rendered statically because it used `searchParams.then`'
)
} finally {
await browser.close()
}
})
} else {
it('error when the build when `dynamic = "error"` and dynamic data is read', async () => {
try {
await next.start()
} catch (err) {
// We expect this to fail
}
// Error: Page with `dynamic = "error"` couldn't be rendered statically because it used `headers`
expect(next.cliOutput).toMatch(
'Error: Route /cookies with `dynamic = "error"` couldn\'t be rendered statically because it used `cookies`'
)
expect(next.cliOutput).toMatch(
'Error: Route /connection with `dynamic = "error"` couldn\'t be rendered statically because it used `connection`'
)
expect(next.cliOutput).toMatch(
'Error: Route /headers with `dynamic = "error"` couldn\'t be rendered statically because it used `headers`'
)
expect(next.cliOutput).toMatch(
'Error: Route /search with `dynamic = "error"` couldn\'t be rendered statically because it used `await searchParams`, `searchParams.then`, or similar'
)
expect(next.cliOutput).toMatch(
'Error: Route /routes/form-data/error with `dynamic = "error"` couldn\'t be rendered statically because it used `request.formData`'
)
expect(next.cliOutput).toMatch(
'Error: Route /routes/next-url/error with `dynamic = "error"` couldn\'t be rendered statically because it used `nextUrl.toString`'
)
})
}
})
describe('dynamic-data inside cache scope', () => {
const { next, isNextDev, isNextDeploy, skipped } = nextTestSetup({
files: __dirname + '/fixtures/cache-scoped',
skipStart: true,
})
if (skipped) {
return
}
if (isNextDeploy) {
it.skip('should not run in next deploy..', () => {})
return
}
if (isNextDev) {
beforeAll(async () => {
await next.start()
})
it('displays redbox when accessing dynamic data inside a cache scope', async () => {
let browser = await next.browser('/cookies')
try {
await assertHasRedbox(browser)
expect(await getRedboxHeader(browser)).toMatch(
'Error: Route /cookies used "cookies" inside a function cached with "unstable_cache(...)".'
)
} finally {
await browser.close()
}
browser = await next.browser('/connection')
try {
await assertHasRedbox(browser)
expect(await getRedboxHeader(browser)).toMatch(
'Error: Route /connection used "connection" inside a function cached with "unstable_cache(...)".'
)
} finally {
await browser.close()
}
browser = await next.browser('/headers')
try {
await assertHasRedbox(browser)
expect(await getRedboxHeader(browser)).toMatch(
'Error: Route /headers used "headers" inside a function cached with "unstable_cache(...)".'
)
} finally {
await browser.close()
}
})
} else {
it('error when the build when accessing dynamic data inside a cache scope', async () => {
try {
await next.start()
} catch (err) {
// We expect this to fail
}
expect(next.cliOutput).toMatch(
'Error: Route /cookies used "cookies" inside a function cached with "unstable_cache(...)".'
)
expect(next.cliOutput).toMatch(
'Error: Route /connection used "connection" inside a function cached with "unstable_cache(...)".'
)
expect(next.cliOutput).toMatch(
'Error: Route /headers used "headers" inside a function cached with "unstable_cache(...)".'
)
})
}
})