Skip to content

Commit

Permalink
Fix external check for non-local next import (#25518)
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed May 28, 2021
1 parent 39181c4 commit 5e92ae0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
9 changes: 9 additions & 0 deletions packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -739,6 +740,14 @@ export default async function getBaseWebpackConfig(
return
}

if (
res.match(
/next[/\\]dist[/\\]next-server[/\\](?!lib[/\\]router[/\\]router)/
)
) {
return `commonjs ${request}`
}

// Default pages have to be transpiled
if (
res.match(/[/\\]next[/\\]dist[/\\]/) ||
Expand Down
37 changes: 36 additions & 1 deletion test/integration/getserversideprops/pages/index.js
Original file line number Diff line number Diff line change
@@ -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 (
<>
<p>props {JSON.stringify(props)}</p>
<p>router: {JSON.stringify(router)}</p>
</>
)
}

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(
<RouterContext.Provider
value={{
query,
pathname: '/',
asPath: req.url,
isPreview: preview,
}}
>
<p>hello world</p>
<RouterComp hello={'world'} />
</RouterContext.Provider>
)
)
return {
props: {
url: req.url,
Expand Down

0 comments on commit 5e92ae0

Please sign in to comment.