Unverified Commit efdb87d1 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Drop browser field for edge runtime (#35335)

Related to #31678 

For streaming pages,

Nodejs runtime: setting global runtime to `"nodejs"` will work with default module resolution.
Edge runtime: previously next.js will pick up browser field since it's the "similar" asset (unlike nodejs) to edge runtime but browser specific things like DOM api could breaks cause edge runtime is more like worker without dom runtime.

This PR is to revert the main field resolution behavior. And if you have a library targeting multiple runtime with different fields, ideally is to make it more isomorphic and will be easy to use in edge runtime
parent 98d7ee3a
......@@ -646,7 +646,9 @@ export default async function getBaseWebpackConfig(
},
}
: undefined),
mainFields: !targetWeb ? ['main', 'module'] : ['browser', 'module', 'main'],
mainFields: targetWeb
? (isEdgeRuntime ? [] : ['browser']).concat(['module', 'main'])
: ['main', 'module'],
plugins: [],
}
......
{
"name": "non-isomorphic-text",
"module": "./index.js",
"browser": "./browser.js"
}
import moment from 'moment'
import nonIsomorphicText from 'non-isomorphic-text'
export default function Page() {
return <div>date:{moment().toString()}</div>
return (
<div>
<div>date:{moment().toString()}</div>
<div>{nonIsomorphicText()}</div>
</div>
)
}
......@@ -157,6 +157,11 @@ export default function (context, { runtime, env }) {
})
}
it('should not pick browser field from package.json for external libraries', async () => {
const html = await renderViaHTTP(context.appPort, '/external-imports')
expect(html).toContain('isomorphic-export')
})
it('should handle various kinds of exports correctly', async () => {
const html = await renderViaHTTP(context.appPort, '/various-exports')
const content = getNodeBySelector(html, '#__next').text()
......
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