Unverified Commit f4e6dea1 authored by Jimmy Lai's avatar Jimmy Lai Committed by GitHub

build: fix externals resolution when importing from next/dist (#55269)

This fixes a "bug" where Sentry was trying to access some of the internal async storages and it would fail because their imports would be compiled as-is instead of using the special rules for Next externals as we are short-cutting it for `next/dist` requests for some reason.

Not a bug per se because this is technically an undocumented API but I think it's a reasonable fix since we don't provide an API for that. The alternative for them would be to try to play around with the bundling, which is not a good use of anyone's time.
parent 3afba0d1
...@@ -1430,7 +1430,7 @@ export default async function getBaseWebpackConfig( ...@@ -1430,7 +1430,7 @@ export default async function getBaseWebpackConfig(
* This is used to ensure that files used across the rendering runtime(s) and the user code are one and the same. The logic in this function * This is used to ensure that files used across the rendering runtime(s) and the user code are one and the same. The logic in this function
* will rewrite the require to the correct bundle location depending on the layer at which the file is being used. * will rewrite the require to the correct bundle location depending on the layer at which the file is being used.
*/ */
const isLocalCallback = (localRes: string) => { const resolveNextExternal = (localRes: string) => {
const isSharedRuntime = sharedRuntimePattern.test(localRes) const isSharedRuntime = sharedRuntimePattern.test(localRes)
const isExternal = externalPattern.test(localRes) const isExternal = externalPattern.test(localRes)
...@@ -1514,8 +1514,7 @@ export default async function getBaseWebpackConfig( ...@@ -1514,8 +1514,7 @@ export default async function getBaseWebpackConfig(
return `module ${request}` return `module ${request}`
} }
// Other Next.js internals need to be transpiled. return resolveNextExternal(request)
return
} }
// Early return if the request needs to be bundled, such as in the client layer. // Early return if the request needs to be bundled, such as in the client layer.
...@@ -1535,9 +1534,7 @@ export default async function getBaseWebpackConfig( ...@@ -1535,9 +1534,7 @@ export default async function getBaseWebpackConfig(
const fullRequest = isRelative const fullRequest = isRelative
? path.join(context, request).replace(/\\/g, '/') ? path.join(context, request).replace(/\\/g, '/')
: request : request
const resolveNextExternal = isLocalCallback(fullRequest) return resolveNextExternal(fullRequest)
return resolveNextExternal
} }
// TODO-APP: Let's avoid this resolve call as much as possible, and eventually get rid of it. // TODO-APP: Let's avoid this resolve call as much as possible, and eventually get rid of it.
...@@ -1549,7 +1546,7 @@ export default async function getBaseWebpackConfig( ...@@ -1549,7 +1546,7 @@ export default async function getBaseWebpackConfig(
isEsmRequested, isEsmRequested,
hasAppDir, hasAppDir,
getResolve, getResolve,
isLocal ? isLocalCallback : undefined isLocal ? resolveNextExternal : undefined
) )
if ('localRes' in resolveResult) { if ('localRes' in resolveResult) {
...@@ -1610,7 +1607,7 @@ export default async function getBaseWebpackConfig( ...@@ -1610,7 +1607,7 @@ export default async function getBaseWebpackConfig(
hasAppDir, hasAppDir,
isEsmRequested, isEsmRequested,
getResolve, getResolve,
isLocal ? isLocalCallback : undefined isLocal ? resolveNextExternal : undefined
) )
if (pkgRes.res) { if (pkgRes.res) {
resolvedExternalPackageDirs.set(pkg, path.dirname(pkgRes.res)) resolvedExternalPackageDirs.set(pkg, path.dirname(pkgRes.res))
......
...@@ -221,5 +221,10 @@ createNextDescribe( ...@@ -221,5 +221,10 @@ createNextDescribe(
const middlewareBundle = await next.readFile('.next/server/middleware.js') const middlewareBundle = await next.readFile('.next/server/middleware.js')
expect(middlewareBundle).not.toContain('image-response') expect(middlewareBundle).not.toContain('image-response')
}) })
it('should use the same async storages if imported directly', async () => {
const html = await next.render('/async-storage')
expect(html).toContain('success')
})
} }
) )
import { requestAsyncStorage } from 'next/dist/client/components/request-async-storage.external'
export default async function Page() {
// cookies is undefined if not set
return !!requestAsyncStorage.getStore().cookies ? 'success' : 'fail'
}
export const dynamic = 'force-dynamic'
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