Skip to content

Commit

Permalink
Only track client entry modules in the client reference manifest (#48814
Browse files Browse the repository at this point in the history
)

This is currently an overhead, that we check a module's layer (`mod.buildInfo.rsc?.type === RSC_MODULE_TYPES.client`) and put all client modules in the client reference manifest, but the manifest is only used for accessing these entry modules. So here we change the util to check if it's an client entry instead.

With this change the client manifest of a test app decreased from 177 KB to 70 KB.

Ref: https://github.com/vercel/next.js/blob/5b609e264f56e6e7c6e230d88ad444650b5b6ba9/packages/next/src/build/analysis/get-page-static-info.ts#L50-L64
  • Loading branch information
shuding authored Apr 25, 2023
1 parent 0f6b357 commit f08cab3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
12 changes: 8 additions & 4 deletions packages/next/src/build/webpack/loaders/utils.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { createHash } from 'crypto'

import { RSC_MODULE_TYPES } from '../../../shared/lib/constants'

const imageExtensions = ['jpg', 'jpeg', 'png', 'webp', 'avif', 'ico', 'svg']
const imageRegex = new RegExp(`\\.(${imageExtensions.join('|')})$`)

export function isClientComponentModule(mod: {
export function isClientComponentEntryModule(mod: {
resource: string
buildInfo: any
}) {
const hasClientDirective = mod.buildInfo.rsc?.type === RSC_MODULE_TYPES.client
return hasClientDirective || imageRegex.test(mod.resource)
const rscInfo = mod.buildInfo.rsc
const hasClientDirective = rscInfo?.isClientRef
const isActionLayerEntry =
rscInfo?.actions && rscInfo?.type === RSC_MODULE_TYPES.client
return (
hasClientDirective || isActionLayerEntry || imageRegex.test(mod.resource)
)
}

export const regexCSS = /\.(css|scss|sass)(\?.*)?$/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
import {
generateActionId,
getActions,
isClientComponentModule,
isClientComponentEntryModule,
isCSSMod,
} from '../loaders/utils'
import { traverseModules, forEachEntryModule } from '../utils'
Expand Down Expand Up @@ -521,7 +521,7 @@ export class ClientReferenceEntryPlugin {
}
visitedBySegment[entryRequest].add(storeKey)

const isClientComponent = isClientComponentModule(mod)
const isClientComponent = isClientComponentEntryModule(mod)

const actions = getActions(mod)
if (actions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
SYSTEM_ENTRYPOINTS,
} from '../../../shared/lib/constants'
import { relative, sep } from 'path'
import { isClientComponentModule, isCSSMod } from '../loaders/utils'
import { isClientComponentEntryModule, isCSSMod } from '../loaders/utils'
import { getProxiedPluginState } from '../../build-context'

import { traverseModules } from '../utils'
Expand Down Expand Up @@ -212,7 +212,10 @@ export class ClientReferenceManifestPlugin {

// Only apply following logic to client module requests from client entry,
// or if the module is marked as client module.
if (!clientRequestsSet.has(resource) && !isClientComponentModule(mod)) {
if (
!clientRequestsSet.has(resource) &&
!isClientComponentEntryModule(mod)
) {
return
}

Expand Down

0 comments on commit f08cab3

Please sign in to comment.