Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rsc: skip next builtin module when apply loaders #36202

Merged
merged 8 commits into from Apr 17, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -5,6 +5,7 @@ import {
buildExports,
createClientComponentFilter,
createServerComponentFilter,
isNextBuiltinClientComponent,
} from './utils'

function createFlightServerRequest(request: string, options: object) {
Expand Down Expand Up @@ -57,7 +58,10 @@ async function parseModuleInfo({
const isBuiltinModule_ = builtinModules.includes(path)
const resolvedPath = isBuiltinModule_ ? path : await resolver(path)

const isNodeModuleImport_ = resolvedPath.includes('/node_modules/')
const isNodeModuleImport_ =
/[\\/]node_modules[\\/]/.test(resolvedPath) &&
// exclude next built-in modules
!isNextBuiltinClientComponent(resolvedPath)

return [isBuiltinModule_, isNodeModuleImport_] as const
}
Expand Down Expand Up @@ -208,7 +212,7 @@ export default async function transformSource(
}

const isServerComponent = createServerComponentFilter(extensions)
const isClientComponent = createClientComponentFilter(extensions)
const isClientComponent = createClientComponentFilter()
huozhi marked this conversation as resolved.
Show resolved Hide resolved
const hasAppliedFlightServerLoader = this.loaders.some((loader: any) => {
return hasFlightLoader(loader.path, 'server')
})
Expand Down
12 changes: 6 additions & 6 deletions packages/next/build/webpack/loaders/utils.ts
@@ -1,4 +1,4 @@
const defaultJsFileExtensions = ['js', 'mjs', 'jsx', 'ts', 'tsx', 'json']
const defaultJsFileExtensions = ['js', 'mjs', 'jsx', 'ts', 'tsx']
const imageExtensions = ['jpg', 'jpeg', 'png', 'webp', 'avif']
const nextClientComponents = ['link', 'image', 'head', 'script']

Expand All @@ -24,16 +24,14 @@ export function buildExports(moduleExports: any, isESM: boolean) {
return ret
}

export const createClientComponentFilter = (
extensions: string[] = defaultJsFileExtensions
) => {
export const createClientComponentFilter = () => {
// 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('|')}))?|` +
`\\.client(\\.(${defaultJsFileExtensions.join('|')}))?|` +
`next/(${nextClientComponents.join('|')})(\\.js)?|` +
`\\.(${imageExtensions.join('|')})` +
')$'
Expand All @@ -42,7 +40,9 @@ export const createClientComponentFilter = (
return (importSource: string) => regex.test(importSource)
}

export const createServerComponentFilter = (extensions: string[]) => {
export const createServerComponentFilter = (
extensions: string[] = defaultJsFileExtensions
) => {
const regex = new RegExp(`\\.server(\\.(${extensions.join('|')}))?$`)
return (importSource: string) => regex.test(importSource)
}
Expand Up @@ -23,6 +23,7 @@ type Options = {

const PLUGIN_NAME = 'FlightManifestPlugin'

const isClientComponent = createClientComponentFilter()
export class FlightManifestPlugin {
dev: boolean = false
pageExtensions: string[]
Expand Down Expand Up @@ -64,7 +65,6 @@ export class FlightManifestPlugin {

createAsset(assets: any, compilation: any) {
const manifest: any = {}
const isClientComponent = createClientComponentFilter(this.pageExtensions)
compilation.chunkGroups.forEach((chunkGroup: any) => {
function recordModule(id: string, _chunk: any, mod: any) {
const resource = mod.resource
Expand Down
12 changes: 10 additions & 2 deletions test/production/react-18-streaming-ssr/index.test.ts
Expand Up @@ -11,7 +11,7 @@ describe('react 18 streaming SSR in minimal mode', () => {
next = await createNext({
files: {
'pages/index.server.js': `
export default function Page() {
export default function Page() {
return <p>static streaming</p>
}
`,
Expand Down Expand Up @@ -65,8 +65,15 @@ describe('react 18 streaming SSR with custom next configs', () => {
}
`,
'pages/hello.js': `
import Link from 'next/link'

export default function Page() {
return <p>hello nextjs</p>
return (
<div>
<p>hello nextjs</p>
<Link href='/'><a>home></a></Link>
</div>
)
}
`,
'pages/multi-byte.js': `
Expand Down Expand Up @@ -114,6 +121,7 @@ describe('react 18 streaming SSR with custom next configs', () => {
expect(redirectRes.status).toBe(308)
expect(res.status).toBe(200)
expect(html).toContain('hello nextjs')
expect(html).toContain('home')
})

it('should render multi-byte characters correctly in streaming', async () => {
Expand Down