forked from vercel/next.js
/
rsc.js
239 lines (202 loc) · 8.83 KB
/
rsc.js
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
/* eslint-env jest */
import webdriver from 'next-webdriver'
import { renderViaHTTP, check } from 'next-test-utils'
import { join } from 'path'
import fs from 'fs-extra'
import { getNodeBySelector } from './utils'
export default function (context, { runtime, env }) {
const distDir = join(context.appDir, '.next')
it('should render server components correctly', async () => {
const homeHTML = await renderViaHTTP(context.appPort, '/', null, {
headers: {
'x-next-test-client': 'test-util',
},
})
// should have only 1 DOCTYPE
expect(homeHTML).toMatch(/^<!DOCTYPE html><html/)
expect(homeHTML).toContain('component:index.server')
expect(homeHTML).toContain('env:env_var_test')
expect(homeHTML).toContain('header:test-util')
})
it('should reuse the inline flight response without sending extra requests', async () => {
let hasFlightRequest = false
let requestsCount = 0
await webdriver(context.appPort, '/', {
beforePageLoad(page) {
page.on('request', (request) => {
requestsCount++
const url = request.url()
if (/\?__flight__=1/.test(url)) {
hasFlightRequest = true
}
})
},
})
expect(requestsCount).toBeGreaterThan(0)
expect(hasFlightRequest).toBe(false)
})
it('should support multi-level server component imports', async () => {
const html = await renderViaHTTP(context.appPort, '/multi')
expect(html).toContain('bar.server.js:')
expect(html).toContain('foo.client')
})
it('should resolve different kinds of components correctly', async () => {
const html = await renderViaHTTP(context.appPort, '/shared')
const main = getNodeBySelector(html, '#main').html()
// Should have 5 occurrences of "client_component".
expect([...main.matchAll(/client_component/g)].length).toBe(5)
// Should have 2 occurrences of "shared:server", and 2 occurrences of
// "shared:client".
const sharedServerModule = [...main.matchAll(/shared:server:(\d+)/g)]
const sharedClientModule = [...main.matchAll(/shared:client:(\d+)/g)]
expect(sharedServerModule.length).toBe(2)
expect(sharedClientModule.length).toBe(2)
// Should have 2 modules created for the shared component.
expect(sharedServerModule[0][1]).toBe(sharedServerModule[1][1])
expect(sharedClientModule[0][1]).toBe(sharedClientModule[1][1])
expect(sharedServerModule[0][1]).not.toBe(sharedClientModule[0][1])
// Should import 2 module instances for node_modules too.
const modFromClient = main.match(
/node_modules instance from \.client\.js:(\d+)/
)
const modFromServer = main.match(
/node_modules instance from \.server\.js:(\d+)/
)
expect(modFromClient[1]).not.toBe(modFromServer[1])
})
it('should support next/link in server components', async () => {
const linkHTML = await renderViaHTTP(context.appPort, '/next-api/link')
const linkText = getNodeBySelector(
linkHTML,
'#__next > div > a[href="/"]'
).text()
expect(linkText).toContain('home')
const browser = await webdriver(context.appPort, '/next-api/link')
// We need to make sure the app is fully hydrated before clicking, otherwise
// it will be a full redirection instead of being taken over by the next
// router. This timeout prevents it being flaky caused by fast refresh's
// rebuilding event.
await new Promise((res) => setTimeout(res, 1000))
await browser.eval('window.beforeNav = 1')
await browser.waitForElementByCss('#next_id').click()
await check(() => browser.elementByCss('#query').text(), 'query:1')
await browser.waitForElementByCss('#next_id').click()
await check(() => browser.elementByCss('#query').text(), 'query:2')
expect(await browser.eval('window.beforeNav')).toBe(1)
})
it('should be able to navigate between rsc pages', async () => {
let content
const browser = await webdriver(context.appPort, '/')
await browser.waitForElementByCss('#goto-next-link').click()
await new Promise((res) => setTimeout(res, 1000))
expect(await browser.url()).toBe(
`http://localhost:${context.appPort}/next-api/link`
)
await browser.waitForElementByCss('#goto-home').click()
await new Promise((res) => setTimeout(res, 1000))
expect(await browser.url()).toBe(`http://localhost:${context.appPort}/`)
content = await browser.elementByCss('#__next').text()
expect(content).toContain('component:index.server')
await browser.waitForElementByCss('#goto-streaming-rsc').click()
// Wait for navigation and streaming to finish.
await check(
() => browser.elementByCss('#content').text(),
'next_streaming_data'
)
expect(await browser.url()).toBe(
`http://localhost:${context.appPort}/streaming-rsc`
)
})
it('should handle streaming server components correctly', async () => {
const browser = await webdriver(context.appPort, '/streaming-rsc')
const content = await browser.eval(
`document.querySelector('#content').innerText`
)
expect(content).toMatchInlineSnapshot('"next_streaming_data"')
})
// Disable next/image for nodejs runtime temporarily
if (runtime === 'edge') {
it('should suspense next/image in server components', async () => {
const imageHTML = await renderViaHTTP(context.appPort, '/next-api/image')
const imageTag = getNodeBySelector(
imageHTML,
'#__next > span > span > img'
)
expect(imageTag.attr('src')).toContain('data:image')
})
}
it('should refresh correctly with next/link', async () => {
// Select the button which is not hidden but rendered
const selector = '#__next #goto-next-link'
let hasFlightRequest = false
const browser = await webdriver(context.appPort, '/', {
beforePageLoad(page) {
page.on('request', (request) => {
const url = request.url()
if (/\?__flight__=1/.test(url)) {
hasFlightRequest = true
}
})
},
})
// wait for hydration
await new Promise((res) => setTimeout(res, 1000))
if (env === 'dev') {
expect(hasFlightRequest).toBe(false)
}
await browser.elementByCss(selector).click()
// wait for re-hydration
await new Promise((res) => setTimeout(res, 1000))
if (env === 'dev') {
expect(hasFlightRequest).toBe(true)
}
const refreshText = await browser.elementByCss(selector).text()
expect(refreshText).toBe('next link')
})
if (env === 'dev') {
// For prod build, the directory contains the build ID so it's not deterministic.
// Only enable it for dev for now.
it('should not bundle external imports into client builds for RSC', async () => {
const html = await renderViaHTTP(context.appPort, '/external-imports')
expect(html).toContain('date:')
const distServerDir = join(distDir, 'static', 'chunks', 'pages')
const bundle = fs
.readFileSync(join(distServerDir, 'external-imports.js'))
.toString()
expect(bundle).not.toContain('non-isomorphic-text')
})
}
it('should not pick browser field from package.json for external libraries', async () => {
const html = await renderViaHTTP(context.appPort, '/external-imports')
expect(html).toContain('isomorphic-export')
})
it('should handle various kinds of exports correctly', async () => {
const html = await renderViaHTTP(context.appPort, '/various-exports')
const content = getNodeBySelector(html, '#__next').text()
expect(content).toContain('abcde')
expect(content).toContain('default-export-arrow.client')
expect(content).toContain('named.client')
const browser = await webdriver(context.appPort, '/various-exports')
const hydratedContent = await browser.waitForElementByCss('#__next').text()
expect(hydratedContent).toContain('abcde')
expect(hydratedContent).toContain('default-export-arrow.client')
expect(hydratedContent).toContain('named.client')
expect(hydratedContent).toContain('cjs-shared')
expect(hydratedContent).toContain('cjs-client')
expect(hydratedContent).toContain('Export All: one, two, two')
})
it('should handle 404 requests and missing routes correctly', async () => {
const id = '#text'
const content = 'custom-404-page'
const page404HTML = await renderViaHTTP(context.appPort, '/404')
const pageUnknownHTML = await renderViaHTTP(context.appPort, '/no.where')
let browser = await webdriver(context.appPort, '/404')
const hydrated404Content = await browser.waitForElementByCss(id).text()
browser = await webdriver(context.appPort, '/no.where')
const hydratedUnknownContent = await browser.waitForElementByCss(id).text()
expect(hydrated404Content).toBe(content)
expect(hydratedUnknownContent).toBe(content)
expect(getNodeBySelector(page404HTML, id).text()).toBe(content)
expect(getNodeBySelector(pageUnknownHTML, id).text()).toBe(content)
})
}