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

Apply react-server transform and valication to middleware #57448

Merged
merged 4 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,17 +461,17 @@ export default async function getBaseWebpackConfig(

const swcLoaderForMiddlewareLayer = useSWCLoader
? getSwcLoader({
serverComponents: false,
isReactServerLayer: false,
serverComponents: true,
isReactServerLayer: true,
})
: // 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".
// This will cause some performance overhead but
// acceptable as Babel will not be recommended.
[
getSwcLoader({
serverComponents: false,
isReactServerLayer: false,
serverComponents: true,
isReactServerLayer: true,
}),
getBabelLoader(),
]
Expand Down
5 changes: 1 addition & 4 deletions packages/next/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,9 @@ const WEBPACK_LAYERS = {
WEBPACK_LAYERS_NAMES.actionBrowser,
WEBPACK_LAYERS_NAMES.appMetadataRoute,
WEBPACK_LAYERS_NAMES.appRouteHandler,
],
nonClientServerTarget: [
// plus middleware and pages api
WEBPACK_LAYERS_NAMES.middleware,
WEBPACK_LAYERS_NAMES.api,
],
nonClientServerTarget: [WEBPACK_LAYERS_NAMES.api],
app: [
WEBPACK_LAYERS_NAMES.reactServerComponents,
WEBPACK_LAYERS_NAMES.actionBrowser,
Expand Down
86 changes: 51 additions & 35 deletions test/e2e/module-layer/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { createNextDescribe } from 'e2e-utils'
import { getRedboxSource, hasRedbox } from 'next-test-utils'

createNextDescribe(
'module layer',
{
files: __dirname,
},
({ next, isNextStart }) => {
({ next, isNextStart, isNextDev }) => {
function runTests() {
it('should render routes marked with restriction marks without errors', async () => {
const routes = [
Expand Down Expand Up @@ -60,45 +61,60 @@ createNextDescribe(
}
}

describe('no server-only in server targets', () => {
const middlewareFile = 'middleware.js'
// const pagesApiFile = 'pages/api/hello.js'
let middlewareContent = ''
// let pagesApiContent = ''
describe('with server-only in server targets', () => {
runTests()
})

beforeAll(async () => {
await next.stop()
// Should error for using mixed (with client-only) in server targets
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)
// pagesApiContent = await next.readFile(pagesApiFile)
beforeAll(async () => {
await next.stop()

await next.patchFile(
middlewareFile,
middlewareContent
.replace("import 'server-only'", "// import 'server-only'")
.replace("// import './lib/mixed-lib'", "import './lib/mixed-lib'")
)
middlewareContent = await next.readFile(middlewareFile)
// pagesApiContent = await next.readFile(pagesApiFile)

// await next.patchFile(
// pagesApiFile,
// pagesApiContent
// .replace("import 'server-only'", "// import 'server-only'")
// .replace(
// "// import '../../lib/mixed-lib'",
// "import '../../lib/mixed-lib'"
// )
// )
await next.patchFile(
middlewareFile,
middlewareContent
// .replace("import 'server-only'", "// import 'server-only'")
.replace(
"// import './lib/mixed-lib'",
"import './lib/mixed-lib'"
)
)

await next.start()
})
afterAll(async () => {
await next.patchFile(middlewareFile, middlewareContent)
// await next.patchFile(pagesApiFile, pagesApiContent)
// await next.patchFile(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leave these comments here, which could be enabled later once we apply the same rule to pages/api

// pagesApiFile,
// pagesApiContent
// .replace("import 'server-only'", "// import 'server-only'")
// .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()
})
}
}
)
Loading