Unverified Commit f74b59c4 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Reogranize the client component detection utils (#36135)

Refactor:

- group client components detction util
- add next script as rsc client component
parent 1e451ab7
...@@ -8,9 +8,7 @@ ...@@ -8,9 +8,7 @@
import { promisify } from 'util' import { promisify } from 'util'
import { parse } from '../../swc' import { parse } from '../../swc'
import { buildExports } from './utils' import { buildExports, isNextBuiltinClientComponent } from './utils'
const IS_NEXT_CLIENT_BUILT_IN = /[\\/]next[\\/](link|image)\.js$/
function addExportNames(names: string[], node: any) { function addExportNames(names: string[], node: any) {
if (!node) return if (!node) return
...@@ -59,7 +57,7 @@ async function collectExports( ...@@ -59,7 +57,7 @@ async function collectExports(
const names: string[] = [] const names: string[] = []
// Next.js built-in client components // Next.js built-in client components
if (IS_NEXT_CLIENT_BUILT_IN.test(resourcePath)) { if (isNextBuiltinClientComponent(resourcePath)) {
names.push('default') names.push('default')
} }
...@@ -160,7 +158,7 @@ export default async function transformSource( ...@@ -160,7 +158,7 @@ export default async function transformSource(
const moduleRefDef = const moduleRefDef =
"const MODULE_REFERENCE = Symbol.for('react.module.reference');\n" "const MODULE_REFERENCE = Symbol.for('react.module.reference');\n"
const isNextClientBuiltIn = IS_NEXT_CLIENT_BUILT_IN.test(resourcePath) const isNextClientBuiltIn = isNextBuiltinClientComponent(resourcePath)
const clientRefsExports = names.reduce((res: any, name) => { const clientRefsExports = names.reduce((res: any, name) => {
const moduleRef = const moduleRef =
......
import { builtinModules } from 'module' import { builtinModules } from 'module'
import { parse } from '../../swc' import { parse } from '../../swc'
import { buildExports } from './utils' import {
buildExports,
const imageExtensions = ['jpg', 'jpeg', 'png', 'webp', 'avif'] createClientComponentFilter,
createServerComponentFilter,
export const createClientComponentFilter = (extensions: string[]) => { } from './utils'
// Special cases for Next.js APIs that are considered as client components:
// - .client.[ext]
// - next built-in client components
// - .[imageExt]
const regex = new RegExp(
'(' +
`\\.client(\\.(${extensions.join('|')}))?|` +
`next/(link|image)(\\.js)?|` +
`\\.(${imageExtensions.join('|')})` +
')$'
)
return (importSource: string) => regex.test(importSource)
}
export const createServerComponentFilter = (extensions: string[]) => {
const regex = new RegExp(`\\.server(\\.(${extensions.join('|')}))?$`)
return (importSource: string) => regex.test(importSource)
}
function createFlightServerRequest(request: string, options: object) { function createFlightServerRequest(request: string, options: object) {
return `next-flight-server-loader?${JSON.stringify(options)}!${request}` return `next-flight-server-loader?${JSON.stringify(options)}!${request}`
......
const defaultJsFileExtensions = ['js', 'mjs', 'jsx', 'ts', 'tsx', 'json']
const imageExtensions = ['jpg', 'jpeg', 'png', 'webp', 'avif']
const nextClientComponents = ['link', 'image', 'script']
const NEXT_BUILT_IN_CLIENT_RSC_REGEX = new RegExp(
`[\\\\/]next[\\\\/](${nextClientComponents.join('|')})\\.js$`
)
export function isNextBuiltinClientComponent(resourcePath: string) {
return NEXT_BUILT_IN_CLIENT_RSC_REGEX.test(resourcePath)
}
export function buildExports(moduleExports: any, isESM: boolean) { export function buildExports(moduleExports: any, isESM: boolean) {
let ret = '' let ret = ''
Object.keys(moduleExports).forEach((key) => { Object.keys(moduleExports).forEach((key) => {
...@@ -11,3 +23,26 @@ export function buildExports(moduleExports: any, isESM: boolean) { ...@@ -11,3 +23,26 @@ export function buildExports(moduleExports: any, isESM: boolean) {
}) })
return ret return ret
} }
export const createClientComponentFilter = (
extensions: string[] = defaultJsFileExtensions
) => {
// Special cases for Next.js APIs that are considered as client components:
// - .client.[ext]
// - next built-in client components
// - .[imageExt]
const regex = new RegExp(
'(' +
`\\.client(\\.(${extensions.join('|')}))?|` +
`next/(${nextClientComponents.join('|')})(\\.js)?|` +
`\\.(${imageExtensions.join('|')})` +
')$'
)
return (importSource: string) => regex.test(importSource)
}
export const createServerComponentFilter = (extensions: string[]) => {
const regex = new RegExp(`\\.server(\\.(${extensions.join('|')}))?$`)
return (importSource: string) => regex.test(importSource)
}
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
import { webpack, sources } from 'next/dist/compiled/webpack/webpack' import { webpack, sources } from 'next/dist/compiled/webpack/webpack'
import { MIDDLEWARE_FLIGHT_MANIFEST } from '../../../shared/lib/constants' import { MIDDLEWARE_FLIGHT_MANIFEST } from '../../../shared/lib/constants'
import { createClientComponentFilter } from '../loaders/next-flight-server-loader' import { createClientComponentFilter } from '../loaders/utils'
// This is the module that will be used to anchor all client references to. // This is the module that will be used to anchor all client references to.
// I.e. it will have all the client files as async deps from this point on. // I.e. it will have all the client files as async deps from this point on.
......
import Nav from '../components/nav' import Nav from '../components/nav'
import Script from 'next/script'
const envVar = process.env.ENV_VAR_TEST const envVar = process.env.ENV_VAR_TEST
const headerKey = 'x-next-test-client' const headerKey = 'x-next-test-client'
...@@ -10,6 +11,7 @@ export default function Index({ header }) { ...@@ -10,6 +11,7 @@ export default function Index({ header }) {
<div>{'env:' + envVar}</div> <div>{'env:' + envVar}</div>
<div>{'header:' + header}</div> <div>{'header:' + header}</div>
<Nav /> <Nav />
<Script id="client-script">{`;`}</Script>
</div> </div>
) )
} }
......
...@@ -14,11 +14,14 @@ export default function (context, { runtime, env }) { ...@@ -14,11 +14,14 @@ export default function (context, { runtime, env }) {
}, },
}) })
const browser = await webdriver(context.appPort, '/')
const scriptTagContent = await browser.elementById('client-script').text()
// should have only 1 DOCTYPE // should have only 1 DOCTYPE
expect(homeHTML).toMatch(/^<!DOCTYPE html><html/) expect(homeHTML).toMatch(/^<!DOCTYPE html><html/)
expect(homeHTML).toContain('component:index.server') expect(homeHTML).toContain('component:index.server')
expect(homeHTML).toContain('env:env_var_test') expect(homeHTML).toContain('env:env_var_test')
expect(homeHTML).toContain('header:test-util') expect(homeHTML).toContain('header:test-util')
expect(scriptTagContent).toBe(';')
}) })
it('should reuse the inline flight response without sending extra requests', async () => { it('should reuse the inline flight response without sending extra requests', async () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment