Skip to content

Commit

Permalink
Fix the runtime for rsc layer (vercel#65850)
Browse files Browse the repository at this point in the history
### What

Fix a bug introduced in vercel#65694 , use app-page runtime for app router
layers

### Why

This is basically reverted the route context picking up logic we had
before.

During the test we found the error thrown
> Module not found: shared-runtime module router-context cannot be used
in rsc layer

Which is caused by a `next/router` imports in rsc page. Decided to
revert to what it was before as the most safe way to load share module
contexts.

It's caused by `next-contentlayer` usage that they're using
`next/router` in server component MDX, but we cannot lint error that
from node_modules. (We actually can, but disabled that due to various
mis-usage of server/client hooks we had before)
  • Loading branch information
huozhi authored and panteliselef committed May 20, 2024
1 parent 8418852 commit f959eb6
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 21 deletions.
17 changes: 9 additions & 8 deletions packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1710,14 +1710,15 @@ export default async function getBaseWebpackConfig(
const layer = resource.contextInfo.issuerLayer
let runtime

if (layer === WEBPACK_LAYERS.serverSideRendering) {
runtime = 'app-page'
} else if (!layer || layer === WEBPACK_LAYERS.api) {
runtime = 'pages'
} else {
throw new Error(
`shared-runtime module ${moduleName} cannot be used in ${layer} layer`
)
switch (layer) {
case WEBPACK_LAYERS.serverSideRendering:
case WEBPACK_LAYERS.reactServerComponents:
case WEBPACK_LAYERS.appPagesBrowser:
case WEBPACK_LAYERS.actionBrowser:
runtime = 'app-page'
break
default:
runtime = 'pages'
}
resource.request = `next/dist/server/future/route-modules/${runtime}/vendored/contexts/${moduleName}`
}
Expand Down
7 changes: 0 additions & 7 deletions test/e2e/app-dir/navigation/navigation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -909,11 +909,4 @@ describe('app dir - navigation', () => {
})
})
})

describe('pages api', () => {
it('should not error if just import the navigation api in pages/api', async () => {
const res = await next.fetch('/api/navigation')
expect(res.status).toBe(200)
})
})
})
5 changes: 0 additions & 5 deletions test/e2e/app-dir/navigation/pages/api/navigation.js

This file was deleted.

5 changes: 5 additions & 0 deletions test/e2e/app-dir/rsc-basic/app/shared-context/server/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require('next/router')

export default function Page() {
return <p>just work</p>
}
6 changes: 6 additions & 0 deletions test/e2e/app-dir/rsc-basic/pages/api/navigation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Use `require` to skip the api check
require('next/navigation')

export default function handle(_, res) {
res.send('just work')
}
14 changes: 14 additions & 0 deletions test/e2e/app-dir/rsc-basic/rsc-basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ describe('app dir - rsc basics', () => {
})
}

describe('next internal shared context', () => {
it('should not error if just load next/navigation module in pages/api', async () => {
const res = await next.fetch('/api/navigation')
expect(res.status).toBe(200)
expect(await res.text()).toBe('just work')
})

it('should not error if just load next/router module in app page', async () => {
const res = await next.fetch('/shared-context/server')
expect(res.status).toBe(200)
expect(await res.text()).toContain('just work')
})
})

it('should correctly render page returning null', async () => {
const homeHTML = await next.render('/return-null/page')
const $ = cheerio.load(homeHTML)
Expand Down
4 changes: 3 additions & 1 deletion test/turbopack-build-tests-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2889,7 +2889,9 @@
"app dir - rsc basics should suspense next/image in server components",
"app dir - rsc basics should suspense next/legacy/image in server components",
"app dir - rsc basics should track client components in dynamic imports",
"app dir - rsc basics should use canary react for app"
"app dir - rsc basics should use canary react for app",
"app dir - rsc basics next internal shared context should not error if just load next/navigation module in pages/api",
"app dir - rsc basics next internal shared context should not error if just load next/router module in app page"
],
"pending": [
"app dir - rsc basics should support partial hydration with inlined server data in browser",
Expand Down

0 comments on commit f959eb6

Please sign in to comment.