Skip to content

Commit 3e7f96e

Browse files
authored
revert "Apply react-server condition for pages api (#57459)" (#57500)
This reverts commit 6b18f39. <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Adding or Updating Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Closes NEXT- Fixes # -->
1 parent c7d02f4 commit 3e7f96e

File tree

4 files changed

+59
-45
lines changed

4 files changed

+59
-45
lines changed

packages/next/src/build/webpack-config.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -454,12 +454,13 @@ export default async function getBaseWebpackConfig(
454454
].filter(Boolean)
455455
: []
456456

457-
const swcLoaderForMiddlewareLayer =
458-
// When using Babel, we will have to use SWC to do the optimization
459-
// for middleware to tree shake the unused default optimized imports like "next/server".
460-
// This will cause some performance overhead but
461-
// acceptable as Babel will not be recommended.
462-
[swcServerLayerLoader, babelLoader].filter(Boolean)
457+
const swcLoaderForMiddlewareLayer = useSWCLoader
458+
? swcServerLayerLoader
459+
: // When using Babel, we will have to use SWC to do the optimization
460+
// for middleware to tree shake the unused default optimized imports like "next/server".
461+
// This will cause some performance overhead but
462+
// acceptable as Babel will not be recommended.
463+
[swcServerLayerLoader, babelLoader]
463464

464465
// client components layers: SSR + browser
465466
const swcLoaderForClientLayer = [
@@ -487,9 +488,16 @@ export default async function getBaseWebpackConfig(
487488
: []),
488489
]
489490

490-
// Loader for API routes will also apply react-server export condition for bundling,
491-
// and the API checks for server side only APIs.
492-
const loaderForAPIRoutes = [swcServerLayerLoader, babelLoader].filter(Boolean)
491+
// Loader for API routes needs to be differently configured as it shouldn't
492+
// have RSC transpiler enabled, so syntax checks such as invalid imports won't
493+
// be performed.
494+
const loaderForAPIRoutes =
495+
hasAppDir && useSWCLoader
496+
? getSwcLoader({
497+
serverComponents: false,
498+
isReactServerLayer: false,
499+
})
500+
: defaultLoaders.babel
493501

494502
const pageExtensions = config.pageExtensions
495503

test/e2e/app-dir/rsc-basic/pages/api/import-test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// You can still import React and Next's client component APIs from the server
2+
// they won't be poisoned by the environment.
3+
// eslint-disable-next-line no-unused-vars
4+
import { useState } from 'react'
15
import 'next/headers'
26

37
export default function (_, res) {

test/e2e/module-layer/index.test.ts

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createNextDescribe } from 'e2e-utils'
2-
import { check, getRedboxSource, hasRedbox } from 'next-test-utils'
2+
import { getRedboxSource, hasRedbox } from 'next-test-utils'
33

44
createNextDescribe(
55
'module layer',
@@ -68,49 +68,51 @@ createNextDescribe(
6868
// Should error for using mixed (with client-only) in server targets
6969
if (isNextDev) {
7070
describe('no server-only in server targets', () => {
71-
it('should error when import client-only in middleware', async () => {
72-
const middlewareFile = 'middleware.js'
73-
const middlewareContent = await next.readFile(middlewareFile)
71+
const middlewareFile = 'middleware.js'
72+
// const pagesApiFile = 'pages/api/hello.js'
73+
let middlewareContent = ''
74+
// let pagesApiContent = ''
75+
76+
beforeAll(async () => {
77+
await next.stop()
78+
79+
middlewareContent = await next.readFile(middlewareFile)
80+
// pagesApiContent = await next.readFile(pagesApiFile)
81+
7482
await next.patchFile(
7583
middlewareFile,
76-
middlewareContent.replace(
77-
"// import './lib/mixed-lib'",
78-
"import './lib/mixed-lib'"
79-
)
84+
middlewareContent
85+
// .replace("import 'server-only'", "// import 'server-only'")
86+
.replace(
87+
"// import './lib/mixed-lib'",
88+
"import './lib/mixed-lib'"
89+
)
8090
)
81-
const browser = await next.browser('/')
8291

83-
expect(await hasRedbox(browser, true)).toBe(true)
84-
expect(await getRedboxSource(browser)).toContain(
85-
`You're importing a component that imports client-only. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components by default.`
86-
)
92+
// await next.patchFile(
93+
// pagesApiFile,
94+
// pagesApiContent
95+
// .replace("import 'server-only'", "// import 'server-only'")
96+
// .replace(
97+
// "// import '../../lib/mixed-lib'",
98+
// "import '../../lib/mixed-lib'"
99+
// )
100+
// )
87101

102+
await next.start()
103+
})
104+
afterAll(async () => {
88105
await next.patchFile(middlewareFile, middlewareContent)
106+
// await next.patchFile(pagesApiFile, pagesApiContent)
89107
})
90108

91-
it('should error when import client-only in pages/api', async () => {
92-
const pagesApiFile = 'pages/api/mixed.js'
93-
const pagesApiContent = await next.readFile(pagesApiFile)
94-
await next.patchFile(
95-
pagesApiFile,
96-
pagesApiContent.replace(
97-
"// import 'client-only'",
98-
"import 'client-only'"
99-
)
100-
)
101-
102-
const existingCliOutputLength = next.cliOutput.length
103-
await check(async () => {
104-
await next.fetch('/api/mixed')
105-
const newCliOutput = next.cliOutput.slice(existingCliOutputLength)
106-
expect(newCliOutput).toContain('./pages/api/mixed.js')
107-
expect(newCliOutput).toContain(
108-
`You're importing a component that imports client-only. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components by default.`
109-
)
110-
return 'success'
111-
}, 'success')
109+
it('should error when import client-only in middleware', async () => {
110+
const browser = await next.browser('/')
112111

113-
await next.patchFile(pagesApiFile, pagesApiContent)
112+
expect(await hasRedbox(browser, true)).toBe(true)
113+
expect(await getRedboxSource(browser)).toContain(
114+
`You're importing a component that imports client-only. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components by default.`
115+
)
114116
})
115117
})
116118
}

test/e2e/module-layer/pages/api/mixed.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// import 'client-only'
1+
import '../../lib/mixed-lib'
22

33
export default function handler(req, res) {
44
return res.send('pages/api/mixed.js:')

0 commit comments

Comments
 (0)