Skip to content

Commit

Permalink
Reland Remove ineffective webpack rules and unused app-page context m…
Browse files Browse the repository at this point in the history
…odules (vercel#65694)

Reland vercel#65321 

Added a test to make sure this change will not fail in `pages/api` like
the error mentioned in vercel#65558
  • Loading branch information
huozhi authored and panteliselef committed May 20, 2024
1 parent 51cc908 commit 1ed59be
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 44 deletions.
30 changes: 8 additions & 22 deletions packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1275,14 +1275,6 @@ export default async function getBaseWebpackConfig(
},
...(hasAppDir
? [
{
layer: WEBPACK_LAYERS.appRouteHandler,
test: new RegExp(
`private-next-app-dir\\/.*\\/route\\.(${pageExtensions.join(
'|'
)})$`
),
},
{
// Make sure that AsyncLocalStorage module instance is shared between server and client
// layers.
Expand Down Expand Up @@ -1708,23 +1700,17 @@ export default async function getBaseWebpackConfig(
'.shared-runtime'
)
const layer = resource.contextInfo.issuerLayer

let runtime

switch (layer) {
case WEBPACK_LAYERS.appRouteHandler:
runtime = 'app-route'
break
case WEBPACK_LAYERS.serverSideRendering:
case WEBPACK_LAYERS.reactServerComponents:
case WEBPACK_LAYERS.appPagesBrowser:
case WEBPACK_LAYERS.actionBrowser:
runtime = 'app-page'
break
default:
runtime = 'pages'
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`
)
}

resource.request = `next/dist/server/future/route-modules/${runtime}/vendored/contexts/${moduleName}`
}
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -613,11 +613,7 @@ export class NextTypesPlugin {
}
return
}
if (
mod.layer !== WEBPACK_LAYERS.reactServerComponents &&
mod.layer !== WEBPACK_LAYERS.appRouteHandler
)
return
if (mod.layer !== WEBPACK_LAYERS.reactServerComponents) return

const IS_LAYOUT = /[/\\]layout\.[^./\\]+$/.test(mod.resource)
const IS_PAGE = !IS_LAYOUT && /[/\\]page\.[^.]+$/.test(mod.resource)
Expand Down
9 changes: 2 additions & 7 deletions packages/next/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ const WEBPACK_LAYERS_NAMES = {
*/
shared: 'shared',
/**
* React Server Components layer (rsc).
* The layer for server-only runtime and picking up `react-server` export conditions.
* Including app router RSC pages and app router custom routes.
*/
reactServerComponents: 'rsc',
/**
Expand Down Expand Up @@ -150,10 +151,6 @@ const WEBPACK_LAYERS_NAMES = {
* The server bundle layer for metadata routes.
*/
appMetadataRoute: 'app-metadata-route',
/**
* The layer for the server bundle for App Route handlers.
*/
appRouteHandler: 'app-route-handler',
} as const

export type WebpackLayerName =
Expand All @@ -166,7 +163,6 @@ const WEBPACK_LAYERS = {
WEBPACK_LAYERS_NAMES.reactServerComponents,
WEBPACK_LAYERS_NAMES.actionBrowser,
WEBPACK_LAYERS_NAMES.appMetadataRoute,
WEBPACK_LAYERS_NAMES.appRouteHandler,
WEBPACK_LAYERS_NAMES.instrument,
WEBPACK_LAYERS_NAMES.middleware,
],
Expand All @@ -182,7 +178,6 @@ const WEBPACK_LAYERS = {
WEBPACK_LAYERS_NAMES.reactServerComponents,
WEBPACK_LAYERS_NAMES.actionBrowser,
WEBPACK_LAYERS_NAMES.appMetadataRoute,
WEBPACK_LAYERS_NAMES.appRouteHandler,
WEBPACK_LAYERS_NAMES.serverSideRendering,
WEBPACK_LAYERS_NAMES.appPagesBrowser,
WEBPACK_LAYERS_NAMES.shared,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,5 @@ export * as ServerInsertedHtml from '../../../../../../shared/lib/server-inserte
export * as AppRouterContext from '../../../../../../shared/lib/app-router-context.shared-runtime'
export * as HooksClientContext from '../../../../../../shared/lib/hooks-client-context.shared-runtime'
export * as RouterContext from '../../../../../../shared/lib/router-context.shared-runtime'
export * as HtmlContext from '../../../../../../shared/lib/html-context.shared-runtime'
export * as AmpContext from '../../../../../../shared/lib/amp-context.shared-runtime'
export * as LoadableContext from '../../../../../../shared/lib/loadable-context.shared-runtime'
export * as ImageConfigContext from '../../../../../../shared/lib/image-config-context.shared-runtime'
export * as Loadable from '../../../../../../shared/lib/loadable.shared-runtime'

This file was deleted.

This file was deleted.

This file was deleted.

7 changes: 7 additions & 0 deletions test/e2e/app-dir/navigation/navigation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -909,4 +909,11 @@ 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: 5 additions & 0 deletions test/e2e/app-dir/navigation/pages/api/navigation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { useParams } from 'next/navigation'

export default function handle(_, res) {
res.send(`${typeof useParams}`)
}

0 comments on commit 1ed59be

Please sign in to comment.