Skip to content

Commit

Permalink
Fix SWC loader options for files outside of RSC layers (#50341)
Browse files Browse the repository at this point in the history
The React Server Component transform of SWC shouldn't be enabled for
files that are not part of "app layers", for example middleware.
  • Loading branch information
shuding committed May 25, 2023
1 parent 3870abf commit 592f592
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -847,8 +847,8 @@ export default async function getBaseWebpackConfig(
rootDir: dir,
pagesDir,
appDir,
hasServerComponents,
hasReactRefresh: dev && isClient,
hasServerComponents: true,
fileReading: config.experimental.swcFileReading,
nextConfig: config,
jsConfig,
Expand All @@ -874,13 +874,16 @@ export default async function getBaseWebpackConfig(
: []
const swcLoaderForClientLayer = hasServerComponents
? useSWCLoader
? [getSwcLoader({ isServerLayer: false })]
? [getSwcLoader({ hasServerComponents, isServerLayer: false })]
: // When using Babel, we will have to add the SWC loader
// as an additional pass to handle RSC correctly.
// This will cause some performance overhead but
// acceptable as Babel will not be recommended.
[getSwcLoader({ isServerLayer: false }), getBabelLoader()]
: []
const swcLoaderForMiddlewareLayer = useSWCLoader
? getSwcLoader({ hasServerComponents: false })
: getBabelLoader()

// Loader for API routes needs to be differently configured as it shouldn't
// have RSC transpiler enabled, so syntax checks such as invalid imports won't
Expand Down Expand Up @@ -2022,7 +2025,7 @@ export default async function getBaseWebpackConfig(
{
test: codeCondition.test,
issuerLayer: WEBPACK_LAYERS.middleware,
use: defaultLoaders.babel,
use: swcLoaderForMiddlewareLayer,
},
...(hasServerComponents
? [
Expand Down
4 changes: 4 additions & 0 deletions test/e2e/app-dir/app-middleware/middleware.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { NextResponse } from 'next/server'

// It should be able to import `headers` inside middleware
import { headers } from 'next/headers'
console.log(!!headers)

/**
* @param {import('next/server').NextRequest} request
*/
Expand Down

0 comments on commit 592f592

Please sign in to comment.