Skip to content

Commit

Permalink
Handle source map requests for Turbopack client chunks
Browse files Browse the repository at this point in the history
Since Turbopack is generating actual source map files, and not inlined
source maps as Webpack does, React might request a source map for a
client chunk using the chunk's URL pathname, e.g.
`/_next/static/chunks/[project]__edc466._.js`.

In the source map dev middleware, we need to convert those URL pathnames
to filenames before requesting the source map for the file from
Turbopack.
  • Loading branch information
unstubbable committed Oct 11, 2024
1 parent eaf467e commit a3daa07
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export function getOverlayMiddleware(project: Project) {
}
}

export function getSourceMapMiddleware(project: Project) {
export function getSourceMapMiddleware(project: Project, distDir: string) {
return async function (
req: IncomingMessage,
res: ServerResponse,
Expand All @@ -163,13 +163,17 @@ export function getSourceMapMiddleware(project: Project) {
return next()
}

const filename = searchParams.get('filename')
let filename = searchParams.get('filename')

if (!filename) {
return badRequest(res)
}

try {
if (filename.startsWith('/_next/static')) {
filename = path.join(distDir, filename.replace(/^\/_next\//, ''))
}

const sourceMapString = await project.getSourceMap(filename)

if (sourceMapString) {
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/dev/hot-reloader-turbopack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ export async function createHotReloaderTurbopack(

const middlewares = [
getOverlayMiddleware(project),
getSourceMapMiddleware(project),
getSourceMapMiddleware(project, distDir),
]

const versionInfoPromise = getVersionInfo(
Expand Down

0 comments on commit a3daa07

Please sign in to comment.