Unverified Commit 17bd232c authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Apply react-server transform and valication to middleware (#57448)

Apply `react-server` resolving and server components invalid APIs
checking to middleware. We want to limit the react usage in in
middleware as so far it's not aimed for rendering purpose.

Another invalid case could be: if you're doing react SSR with
`renderToString` in middleware it should be disallowed. Imaging it could
send the rendered html code to client and you display it in browser. But
it might require hydration so it can be broken.

This PR will only let you import `react-server` export condition
packages.
parent aa28fa52
...@@ -461,8 +461,8 @@ export default async function getBaseWebpackConfig( ...@@ -461,8 +461,8 @@ export default async function getBaseWebpackConfig(
const swcLoaderForMiddlewareLayer = useSWCLoader const swcLoaderForMiddlewareLayer = useSWCLoader
? getSwcLoader({ ? getSwcLoader({
serverComponents: false, serverComponents: true,
isReactServerLayer: false, isReactServerLayer: true,
}) })
: // When using Babel, we will have to use SWC to do the optimization : // When using Babel, we will have to use SWC to do the optimization
// for middleware to tree shake the unused default optimized imports like "next/server". // for middleware to tree shake the unused default optimized imports like "next/server".
...@@ -470,8 +470,8 @@ export default async function getBaseWebpackConfig( ...@@ -470,8 +470,8 @@ export default async function getBaseWebpackConfig(
// acceptable as Babel will not be recommended. // acceptable as Babel will not be recommended.
[ [
getSwcLoader({ getSwcLoader({
serverComponents: false, serverComponents: true,
isReactServerLayer: false, isReactServerLayer: true,
}), }),
getBabelLoader(), getBabelLoader(),
] ]
......
...@@ -154,12 +154,9 @@ const WEBPACK_LAYERS = { ...@@ -154,12 +154,9 @@ const WEBPACK_LAYERS = {
WEBPACK_LAYERS_NAMES.actionBrowser, WEBPACK_LAYERS_NAMES.actionBrowser,
WEBPACK_LAYERS_NAMES.appMetadataRoute, WEBPACK_LAYERS_NAMES.appMetadataRoute,
WEBPACK_LAYERS_NAMES.appRouteHandler, WEBPACK_LAYERS_NAMES.appRouteHandler,
],
nonClientServerTarget: [
// plus middleware and pages api
WEBPACK_LAYERS_NAMES.middleware, WEBPACK_LAYERS_NAMES.middleware,
WEBPACK_LAYERS_NAMES.api,
], ],
nonClientServerTarget: [WEBPACK_LAYERS_NAMES.api],
app: [ app: [
WEBPACK_LAYERS_NAMES.reactServerComponents, WEBPACK_LAYERS_NAMES.reactServerComponents,
WEBPACK_LAYERS_NAMES.actionBrowser, WEBPACK_LAYERS_NAMES.actionBrowser,
......
import { createNextDescribe } from 'e2e-utils' import { createNextDescribe } from 'e2e-utils'
import { getRedboxSource, hasRedbox } from 'next-test-utils'
createNextDescribe( createNextDescribe(
'module layer', 'module layer',
{ {
files: __dirname, files: __dirname,
}, },
({ next, isNextStart }) => { ({ next, isNextStart, isNextDev }) => {
function runTests() { function runTests() {
it('should render routes marked with restriction marks without errors', async () => { it('should render routes marked with restriction marks without errors', async () => {
const routes = [ const routes = [
...@@ -60,45 +61,60 @@ createNextDescribe( ...@@ -60,45 +61,60 @@ createNextDescribe(
} }
} }
describe('no server-only in server targets', () => { describe('with server-only in server targets', () => {
const middlewareFile = 'middleware.js' runTests()
// const pagesApiFile = 'pages/api/hello.js' })
let middlewareContent = ''
// let pagesApiContent = ''
beforeAll(async () => { // Should error for using mixed (with client-only) in server targets
await next.stop() if (isNextDev) {
describe('no server-only in server targets', () => {
const middlewareFile = 'middleware.js'
// const pagesApiFile = 'pages/api/hello.js'
let middlewareContent = ''
// let pagesApiContent = ''
middlewareContent = await next.readFile(middlewareFile) beforeAll(async () => {
// pagesApiContent = await next.readFile(pagesApiFile) await next.stop()
await next.patchFile( middlewareContent = await next.readFile(middlewareFile)
middlewareFile, // pagesApiContent = await next.readFile(pagesApiFile)
middlewareContent
.replace("import 'server-only'", "// import 'server-only'")
.replace("// import './lib/mixed-lib'", "import './lib/mixed-lib'")
)
// await next.patchFile( await next.patchFile(
// pagesApiFile, middlewareFile,
// pagesApiContent middlewareContent
// .replace("import 'server-only'", "// import 'server-only'") // .replace("import 'server-only'", "// import 'server-only'")
// .replace( .replace(
// "// import '../../lib/mixed-lib'", "// import './lib/mixed-lib'",
// "import '../../lib/mixed-lib'" "import './lib/mixed-lib'"
// ) )
// ) )
await next.start() // await next.patchFile(
}) // pagesApiFile,
afterAll(async () => { // pagesApiContent
await next.patchFile(middlewareFile, middlewareContent) // .replace("import 'server-only'", "// import 'server-only'")
// await next.patchFile(pagesApiFile, pagesApiContent) // .replace(
// "// import '../../lib/mixed-lib'",
// "import '../../lib/mixed-lib'"
// )
// )
await next.start()
})
afterAll(async () => {
await next.patchFile(middlewareFile, middlewareContent)
// await next.patchFile(pagesApiFile, pagesApiContent)
})
it('should error when import client-only in middleware', async () => {
const browser = await next.browser('/')
expect(await hasRedbox(browser, true)).toBe(true)
expect(await getRedboxSource(browser)).toContain(
`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.`
)
})
}) })
runTests() }
})
describe('with server-only in server targets', () => {
runTests()
})
} }
) )
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