Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix external check for non-local next import #25518

Merged
merged 2 commits into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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