Unverified Commit 130ab594 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Support `server-only` inside pages/api (#46328)

This can be a special case for the API layer.

## Bug

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ]
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)

---------
parent 232dfbe8
......@@ -786,6 +786,11 @@ export default async function getBaseWebpackConfig(
[getSwcLoader({ isServerLayer: true }), getBabelLoader()]
: []
const loaderForAPI =
hasServerComponents && useSWCLoader
? getSwcLoader({ isServerLayer: true })
: defaultLoaders.babel
const pageExtensions = config.pageExtensions
const outputPath =
......@@ -1409,6 +1414,16 @@ export default async function getBaseWebpackConfig(
},
}
const serverCondition = [
'react-server',
...mainFieldsPerCompiler[
isEdgeServer ? COMPILER_NAMES.edgeServer : COMPILER_NAMES.server
],
'node',
'import',
'require',
]
let webpackConfig: webpack.Configuration = {
parallelism: Number(process.env.NEXT_WEBPACK_PARALLELISM) || undefined,
...(isNodeServer ? { externalsPresets: { node: true } } : {}),
......@@ -1721,17 +1736,7 @@ export default async function getBaseWebpackConfig(
],
},
resolve: {
conditionNames: [
'react-server',
...mainFieldsPerCompiler[
isEdgeServer
? COMPILER_NAMES.edgeServer
: COMPILER_NAMES.server
],
'node',
'import',
'require',
],
conditionNames: serverCondition,
alias: {
// If missing the alias override here, the default alias will be used which aliases
// react to the direct file path, not the package name. In that case the condition
......@@ -1857,14 +1862,6 @@ export default async function getBaseWebpackConfig(
},
]
: []),
{
test: /\.(js|cjs|mjs)$/,
issuerLayer: WEBPACK_LAYERS.api,
parser: {
// Switch back to normal URL handling
url: true,
},
},
{
oneOf: [
{
......@@ -1874,7 +1871,12 @@ export default async function getBaseWebpackConfig(
// Switch back to normal URL handling
url: true,
},
use: defaultLoaders.babel,
resolve: {
alias: {
'server-only': 'next/dist/compiled/server-only/empty',
},
},
use: loaderForAPI,
},
{
...codeCondition,
......
......@@ -13,6 +13,7 @@ createNextDescribe(
react: 'latest',
'react-dom': 'latest',
sass: 'latest',
'server-only': 'latest',
},
},
({ next, isNextDev: isDev, isNextStart, isNextDeploy }) => {
......@@ -67,6 +68,11 @@ createNextDescribe(
})
}
it('should support server-only in pages/api', async () => {
const res = await next.fetch('/api/server-only')
expect(await res.text()).toBe('Hello from server-only.js')
})
if (!isNextDeploy) {
it('should not share edge workers', async () => {
const controller1 = new AbortController()
......
import 'server-only'
export default function (_, res) {
res.end('Hello from server-only.js')
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment