Skip to content

Commit

Permalink
Drop browser field for edge runtime (#35335)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
huozhi committed Mar 16, 2022
1 parent 98d7ee3 commit efdb87d
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/next/build/webpack-config.ts
Expand Up @@ -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: [],
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -1,5 +1,11 @@
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>
)
}
Expand Up @@ -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()
Expand Down

0 comments on commit efdb87d

Please sign in to comment.