Skip to content

Commit b11d441

Browse files
Add additional layer for server components case (#36921)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 359d03f commit b11d441

File tree

5 files changed

+51
-34
lines changed

5 files changed

+51
-34
lines changed

packages/next/build/entries.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,10 @@ export function getViewsEntry(opts: {
313313
viewsDir: string
314314
pageExtensions: string[]
315315
}) {
316-
return `next-view-loader?${stringify(opts)}!`
316+
return {
317+
import: `next-view-loader?${stringify(opts)}!`,
318+
layer: 'sc_server',
319+
}
317320
}
318321

319322
export function getServerlessEntry(opts: {

packages/next/build/webpack/loaders/next-view-loader.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ const nextViewLoader: webpack.LoaderDefinitionFunction<{
119119
export const components = {
120120
${componentsCode.join(',\n')}
121121
};
122+
123+
export const __next_view_webpack_require__ = __webpack_require__
122124
`
123125
return result
124126
}

packages/next/server/load-components.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ export async function loadComponents(
137137
await requirePage(
138138
normalizePagePath(pathname) + '.__sc_client__',
139139
distDir,
140-
serverless
140+
serverless,
141+
rootEnabled
141142
)
142143
} catch (_) {
143144
// This page might not be a server component page, so there is no __sc_client__

packages/next/server/view-render.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,11 @@ function createServerComponentRenderer(
165165
) {
166166
// We need to expose the `__webpack_require__` API globally for
167167
// react-server-dom-webpack. This is a hack until we find a better way.
168-
if (ComponentMod.__next_rsc__) {
168+
if (ComponentMod.__next_view_webpack_require__ || ComponentMod.__next_rsc__) {
169169
// @ts-ignore
170-
globalThis.__webpack_require__ =
171-
ComponentMod.__next_rsc__.__webpack_require__
170+
globalThis.__webpack_require__ = ComponentMod.__next_view_webpack_require__
171+
? ComponentMod.__next_view_webpack_require__
172+
: ComponentMod.__next_rsc__.__webpack_require__
172173

173174
// @ts-ignore
174175
globalThis.__webpack_chunk_load__ = () => Promise.resolve()

test/e2e/views-dir/index.test.ts

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -226,37 +226,47 @@ describe('views dir', () => {
226226
expect(html).toContain('hello from root/shared-component-route')
227227
})
228228

229-
// TODO: implement
230-
it.skip('should serve client component', async () => {
231-
const html = await renderViaHTTP(next.url, '/client-component-route')
232-
expect(html).toContain('hello from root/client-component-route. count: 0')
233-
234-
const browser = await webdriver(next.url, '/client-component-route')
235-
// After hydration count should be 1
236-
expect(await browser.elementByCss('p').text()).toBe(
237-
'hello from root/client-component-route. count: 1'
238-
)
229+
describe('should serve client component', () => {
230+
it('should serve server-side', async () => {
231+
const html = await renderViaHTTP(next.url, '/client-component-route')
232+
const $ = cheerio.load(html)
233+
expect($('p').text()).toBe(
234+
'hello from root/client-component-route. count: 0'
235+
)
236+
})
237+
238+
// TODO: Implement hydration
239+
it.skip('should serve client-side', async () => {
240+
const browser = await webdriver(next.url, '/client-component-route')
241+
// After hydration count should be 1
242+
expect(await browser.elementByCss('p').text()).toBe(
243+
'hello from root/client-component-route. count: 1'
244+
)
245+
})
239246
})
240247

241-
// TODO: implement
242-
it.skip('should include client component layout with server component route', async () => {
243-
const html = await renderViaHTTP(next.url, '/client-nested')
244-
const $ = cheerio.load(html)
245-
// Should not be nested in dashboard
246-
expect($('h1').text()).toBe('Client Nested. Count: 0')
247-
// Should include the page text
248-
expect($('p').text()).toBe('hello from root/client-nested')
249-
250-
const browser = await webdriver(next.url, '/client-nested')
251-
// After hydration count should be 1
252-
expect(await browser.elementByCss('h1').text()).toBe(
253-
'Client Nested. Count: 0'
254-
)
255-
256-
// After hydration count should be 1
257-
expect(await browser.elementByCss('h1').text()).toBe(
258-
'hello from root/client-nested'
259-
)
248+
describe('should include client component layout with server component route', () => {
249+
it('should include it server-side', async () => {
250+
const html = await renderViaHTTP(next.url, '/client-nested')
251+
const $ = cheerio.load(html)
252+
// Should not be nested in dashboard
253+
expect($('h1').text()).toBe('Client Nested. Count: 0')
254+
// Should include the page text
255+
expect($('p').text()).toBe('hello from root/client-nested')
256+
})
257+
258+
// TODO: Implement hydration
259+
it.skip('should include it client-side', async () => {
260+
const browser = await webdriver(next.url, '/client-nested')
261+
// After hydration count should be 1
262+
expect(await browser.elementByCss('h1').text()).toBe(
263+
'Client Nested. Count: 0'
264+
)
265+
// After hydration count should be 1
266+
expect(await browser.elementByCss('h1').text()).toBe(
267+
'hello from root/client-nested'
268+
)
269+
})
260270
})
261271
})
262272
})

0 commit comments

Comments
 (0)