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

Support server-only inside pages/api #46328

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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 } } : {}),
Expand Down Expand Up @@ -1723,17 +1738,7 @@ export default async function getBaseWebpackConfig(
return true
},
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
Expand Down Expand Up @@ -1869,14 +1874,6 @@ export default async function getBaseWebpackConfig(
},
]
: []),
{
test: /\.(js|cjs|mjs)$/,
issuerLayer: WEBPACK_LAYERS.api,
parser: {
// Switch back to normal URL handling
url: true,
},
},
{
oneOf: [
{
Expand All @@ -1886,7 +1883,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,
Expand Down
6 changes: 6 additions & 0 deletions test/e2e/app-dir/app/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ createNextDescribe(
react: 'latest',
'react-dom': 'latest',
sass: 'latest',
'server-only': 'latest',
},
},
({ next, isNextDev: isDev, isNextStart, isNextDeploy }) => {
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions test/e2e/app-dir/app/pages/api/server-only.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'server-only'

export default function (_, res) {
res.end('Hello from server-only.js')
}