From 71e987a6a6089c909545a336f094f83ae09f457a Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 27 May 2021 10:33:01 -0500 Subject: [PATCH 1/2] Fix external check for non-local next import --- packages/next/build/webpack-config.ts | 11 ++++-- .../getserversideprops/pages/index.js | 37 ++++++++++++++++++- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/packages/next/build/webpack-config.ts b/packages/next/build/webpack-config.ts index bbcce90a111b1..01e56abbb708d 100644 --- a/packages/next/build/webpack-config.ts +++ b/packages/next/build/webpack-config.ts @@ -176,6 +176,7 @@ const WEBPACK_RESOLVE_OPTIONS = { // Otherwise combined ESM+CJS packages will never be external // as resolving mismatch would lead to opt-out from being external. dependencyType: 'commonjs', + symlinks: true, } const NODE_RESOLVE_OPTIONS = { @@ -738,12 +739,14 @@ export default async function getBaseWebpackConfig( if (baseRes !== res) { return } + const isNextExternal = res.match(/next[/\\]dist[/\\]next-server[/\\]/) // Default pages have to be transpiled if ( - res.match(/[/\\]next[/\\]dist[/\\]/) || - // This is the @babel/plugin-transform-runtime "helpers: true" option - res.match(/node_modules[/\\]@babel[/\\]runtime[/\\]/) + !isNextExternal && + (res.match(/[/\\]next[/\\]dist[/\\]/) || + // This is the @babel/plugin-transform-runtime "helpers: true" option + res.match(/node_modules[/\\]@babel[/\\]runtime[/\\]/)) ) { return } @@ -758,7 +761,7 @@ export default async function getBaseWebpackConfig( // Anything else that is standard JavaScript within `node_modules` // can be externalized. - if (/node_modules[/\\].*\.c?js$/.test(res)) { + if (isNextExternal || /node_modules[/\\].*\.c?js$/.test(res)) { return `commonjs ${request}` } diff --git a/test/integration/getserversideprops/pages/index.js b/test/integration/getserversideprops/pages/index.js index f940f57fa4b72..8bbf910a94dcd 100644 --- a/test/integration/getserversideprops/pages/index.js +++ b/test/integration/getserversideprops/pages/index.js @@ -1,6 +1,41 @@ import Link from 'next/link' +import ReactDOM from 'react-dom/server' +import { RouterContext } from 'next/dist/next-server/lib/router-context' +import { useRouter } from 'next/router' -export async function getServerSideProps({ req }) { +function RouterComp(props) { + const router = useRouter() + + if (!router) { + throw new Error('router is missing!') + } + + return ( + <> +

props {JSON.stringify(props)}

+

router: {JSON.stringify(router)}

+ + ) +} + +export async function getServerSideProps({ req, query, preview }) { + // this ensures the same router context is used by the useRouter hook + // no matter where it is imported + console.log( + ReactDOM.renderToString( + +

hello world

+ +
+ ) + ) return { props: { url: req.url, From 0a9d7e3d6bace585a722a5cc60ed8a83bbc3a92e Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 27 May 2021 13:42:56 -0500 Subject: [PATCH 2/2] Update to test/return earlier --- packages/next/build/webpack-config.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/next/build/webpack-config.ts b/packages/next/build/webpack-config.ts index 01e56abbb708d..c48c8cb31887b 100644 --- a/packages/next/build/webpack-config.ts +++ b/packages/next/build/webpack-config.ts @@ -739,14 +739,20 @@ export default async function getBaseWebpackConfig( if (baseRes !== res) { return } - const isNextExternal = res.match(/next[/\\]dist[/\\]next-server[/\\]/) + + if ( + res.match( + /next[/\\]dist[/\\]next-server[/\\](?!lib[/\\]router[/\\]router)/ + ) + ) { + return `commonjs ${request}` + } // Default pages have to be transpiled if ( - !isNextExternal && - (res.match(/[/\\]next[/\\]dist[/\\]/) || - // This is the @babel/plugin-transform-runtime "helpers: true" option - res.match(/node_modules[/\\]@babel[/\\]runtime[/\\]/)) + res.match(/[/\\]next[/\\]dist[/\\]/) || + // This is the @babel/plugin-transform-runtime "helpers: true" option + res.match(/node_modules[/\\]@babel[/\\]runtime[/\\]/) ) { return } @@ -761,7 +767,7 @@ export default async function getBaseWebpackConfig( // Anything else that is standard JavaScript within `node_modules` // can be externalized. - if (isNextExternal || /node_modules[/\\].*\.c?js$/.test(res)) { + if (/node_modules[/\\].*\.c?js$/.test(res)) { return `commonjs ${request}` }