Unverified Commit 57c8d04b authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

Create unique source URLs between Client and Server in Pages dir (#75878)

This ensures we don't accidentally return the sourcemap for the server code of a given file when a stackframe was created in the browser.

The Server vs Client case could be distinguished separately but not if we're dealing with externals in Edge and Node.js. We have to use the native `findSourceMap` for those case.

There's probably a valid factoring where we ask the correct compilation to avoid ambiguity but the bundler is really just a perf optimization. E.g. in `next start` and prerender of `next build` we don't have access to the bundler so native `findSourceMap` has to work.

It's not needed for Turbopack since source URLs are already unique it seems.

The actual test is in https://github.com/vercel/next.js/pull/75863 which wouldn't work without this change.
parent eac707f0
...@@ -869,7 +869,9 @@ export function finalizeEntrypoint({ ...@@ -869,7 +869,9 @@ export function finalizeEntrypoint({
? WEBPACK_LAYERS.instrument ? WEBPACK_LAYERS.instrument
: isServerComponent : isServerComponent
? WEBPACK_LAYERS.reactServerComponents ? WEBPACK_LAYERS.reactServerComponents
: undefined : name.startsWith('pages/')
? WEBPACK_LAYERS.pagesDirNode
: undefined
return { return {
publicPath: isApi ? '' : undefined, publicPath: isApi ? '' : undefined,
...@@ -884,7 +886,9 @@ export function finalizeEntrypoint({ ...@@ -884,7 +886,9 @@ export function finalizeEntrypoint({
? WEBPACK_LAYERS.api ? WEBPACK_LAYERS.api
: isMiddlewareFilename(name) || isInstrumentation : isMiddlewareFilename(name) || isInstrumentation
? WEBPACK_LAYERS.middleware ? WEBPACK_LAYERS.middleware
: undefined, : name.startsWith('pages/')
? WEBPACK_LAYERS.pagesDirEdge
: undefined,
library: { name: ['_ENTRIES', `middleware_[name]`], type: 'assign' }, library: { name: ['_ENTRIES', `middleware_[name]`], type: 'assign' },
runtime: EDGE_RUNTIME_WEBPACK, runtime: EDGE_RUNTIME_WEBPACK,
asyncChunks: false, asyncChunks: false,
...@@ -919,6 +923,7 @@ export function finalizeEntrypoint({ ...@@ -919,6 +923,7 @@ export function finalizeEntrypoint({
name.startsWith('pages/') && name !== 'pages/_app' name.startsWith('pages/') && name !== 'pages/_app'
? 'pages/_app' ? 'pages/_app'
: CLIENT_STATIC_FILES_RUNTIME_MAIN, : CLIENT_STATIC_FILES_RUNTIME_MAIN,
layer: WEBPACK_LAYERS.pagesDirBrowser,
...entry, ...entry,
} }
} }
...@@ -930,7 +935,10 @@ export function finalizeEntrypoint({ ...@@ -930,7 +935,10 @@ export function finalizeEntrypoint({
} }
} }
return entry return {
layer: WEBPACK_LAYERS.pagesDirBrowser,
...entry,
}
} }
default: { default: {
// Should never happen. // Should never happen.
......
...@@ -1782,7 +1782,13 @@ export function isWebpackClientOnlyLayer( ...@@ -1782,7 +1782,13 @@ export function isWebpackClientOnlyLayer(
export function isWebpackDefaultLayer( export function isWebpackDefaultLayer(
layer: WebpackLayerName | null | undefined layer: WebpackLayerName | null | undefined
): boolean { ): 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( export function isWebpackBundledLayer(
......
...@@ -137,6 +137,18 @@ const WEBPACK_LAYERS_NAMES = { ...@@ -137,6 +137,18 @@ const WEBPACK_LAYERS_NAMES = {
* The browser client bundle layer for App directory. * The browser client bundle layer for App directory.
*/ */
appPagesBrowser: 'app-pages-browser', 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 } as const
export type WebpackLayerName = export type WebpackLayerName =
......
...@@ -522,8 +522,8 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface { ...@@ -522,8 +522,8 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
stackTrace stackTrace
)?.[1] )?.[1]
if (file) { if (file) {
// `file` is filepath in `pages/` but it can be weird long webpack url in `app/`. // `file` is filepath in `pages/` but it can be a webpack url.
// If it's a webpack loader URL, it will start with '(app-pages)/./' // If it's a webpack loader URL, it will include the app-pages layer
if ( if (
file.startsWith(`(${WEBPACK_LAYERS.appPagesBrowser})/./`) file.startsWith(`(${WEBPACK_LAYERS.appPagesBrowser})/./`)
) { ) {
...@@ -539,6 +539,15 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface { ...@@ -539,6 +539,15 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
if (modules.length > 0) { if (modules.length > 0) {
fileMessage = ` when ${modules.join(', ')} changed` 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 { } else {
fileMessage = ` when ${file} changed` fileMessage = ` when ${file} changed`
} }
......
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