Skip to content

Commit

Permalink
Create unique source URLs between Client and Server in Pages dir
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Feb 12, 2025
1 parent 319a338 commit c1265c5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
14 changes: 11 additions & 3 deletions packages/next/src/build/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,9 @@ export function finalizeEntrypoint({
? WEBPACK_LAYERS.instrument
: isServerComponent
? WEBPACK_LAYERS.reactServerComponents
: undefined
: name.startsWith('pages/')
? WEBPACK_LAYERS.pagesDirNode
: undefined

return {
publicPath: isApi ? '' : undefined,
Expand All @@ -884,7 +886,9 @@ export function finalizeEntrypoint({
? WEBPACK_LAYERS.api
: isMiddlewareFilename(name) || isInstrumentation
? WEBPACK_LAYERS.middleware
: undefined,
: name.startsWith('pages/')
? WEBPACK_LAYERS.pagesDirEdge
: undefined,
library: { name: ['_ENTRIES', `middleware_[name]`], type: 'assign' },
runtime: EDGE_RUNTIME_WEBPACK,
asyncChunks: false,
Expand Down Expand Up @@ -919,6 +923,7 @@ export function finalizeEntrypoint({
name.startsWith('pages/') && name !== 'pages/_app'
? 'pages/_app'
: CLIENT_STATIC_FILES_RUNTIME_MAIN,
layer: WEBPACK_LAYERS.pagesDirBrowser,
...entry,
}
}
Expand All @@ -930,7 +935,10 @@ export function finalizeEntrypoint({
}
}

return entry
return {
layer: WEBPACK_LAYERS.pagesDirBrowser,
...entry,
}
}
default: {
// Should never happen.
Expand Down
8 changes: 7 additions & 1 deletion packages/next/src/build/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1782,7 +1782,13 @@ export function isWebpackClientOnlyLayer(
export function isWebpackDefaultLayer(
layer: WebpackLayerName | null | undefined
): boolean {
return layer === null || layer === undefined
return (
layer === null ||
layer === undefined ||
layer === WEBPACK_LAYERS.pagesDirBrowser ||
layer === WEBPACK_LAYERS.pagesDirEdge ||
layer === WEBPACK_LAYERS.pagesDirNode
)
}

export function isWebpackBundledLayer(
Expand Down
12 changes: 12 additions & 0 deletions packages/next/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ const WEBPACK_LAYERS_NAMES = {
* The browser client bundle layer for App directory.
*/
appPagesBrowser: 'app-pages-browser',
/**
* The browser client bundle layer for Pages directory.
*/
pagesDirBrowser: 'pages-dir-browser',
/**
* The Edge Lite bundle layer for Pages directory.
*/
pagesDirEdge: 'pages-dir-edge',
/**
* The Node.js bundle layer for Pages directory.
*/
pagesDirNode: 'pages-dir-node',
} as const

export type WebpackLayerName =
Expand Down
13 changes: 11 additions & 2 deletions packages/next/src/server/dev/hot-reloader-webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,8 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
stackTrace
)?.[1]
if (file) {
// `file` is filepath in `pages/` but it can be weird long webpack url in `app/`.
// If it's a webpack loader URL, it will start with '(app-pages)/./'
// `file` is filepath in `pages/` but it can be a webpack url.
// If it's a webpack loader URL, it will include the app-pages layer
if (
file.startsWith(`(${WEBPACK_LAYERS.appPagesBrowser})/./`)
) {
Expand All @@ -539,6 +539,15 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
if (modules.length > 0) {
fileMessage = ` when ${modules.join(', ')} changed`
}
} else if (
// Handle known webpack layers
file.startsWith(`(${WEBPACK_LAYERS.pagesDirBrowser})/./`)
) {
const cleanedFilePath = file.slice(
`(${WEBPACK_LAYERS.pagesDirBrowser})/`.length
)

fileMessage = ` when ${cleanedFilePath} changed`
} else {
fileMessage = ` when ${file} changed`
}
Expand Down

0 comments on commit c1265c5

Please sign in to comment.