Skip to content

Commit a8207c8

Browse files
committed
Handle for Webpack
1 parent b1976f3 commit a8207c8

File tree

6 files changed

+39
-34
lines changed

6 files changed

+39
-34
lines changed

packages/next/src/build/analysis/get-page-static-info.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
} from '../segment-config/middleware/middleware-config'
4141
import { normalizeAppPath } from '../../shared/lib/router/utils/app-paths'
4242
import { normalizePagePath } from '../../shared/lib/page-path/normalize-page-path'
43+
import { isProxyFile } from '../utils'
4344

4445
const PARSE_PATTERN =
4546
/(?<!(_jsx|jsx-))runtime|preferredRegion|getStaticProps|getServerSideProps|generateStaticParams|export const|generateImageMetadata|generateSitemaps|middleware|proxy/
@@ -756,14 +757,15 @@ export async function getPagesPageStaticInfo({
756757
warnAboutExperimentalEdge(isAnAPIRoute ? page! : null)
757758
}
758759

759-
if (
760-
(page === `/${PROXY_FILENAME}` || page === `/src/${PROXY_FILENAME}`) &&
761-
isEdgeRuntime(resolvedRuntime)
762-
) {
760+
if (isProxyFile(page) && isEdgeRuntime(resolvedRuntime)) {
763761
resolvedRuntime = SERVER_RUNTIME.nodejs
764-
const message = `Proxy does not support Edge runtime.`
762+
const relativePath = relative(process.cwd(), pageFilePath)
763+
const resolvedPath = relativePath.startsWith('.')
764+
? relativePath
765+
: `./${relativePath}`
766+
const message = `Route segment config is not allowed in Proxy file at "${resolvedPath}". Proxy always runs on Node.js runtime. Learn more: https://nextjs.org/docs/messages/middleware-to-proxy`
765767
if (isDev) {
766-
Log.error(message)
768+
Log.errorOnce(message)
767769
} else {
768770
throw new Error(message)
769771
}

packages/next/src/build/entries.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ import type { __ApiPreviewProps } from '../server/api-utils'
4343
import {
4444
isMiddlewareFile,
4545
isMiddlewareFilename,
46+
isProxyFile,
4647
isInstrumentationHookFile,
4748
isInstrumentationHookFilename,
48-
isProxyFilename,
4949
} from './utils'
5050
import { getPageStaticInfo } from './analysis/get-page-static-info'
5151
import { normalizePathSep } from '../shared/lib/page-path/normalize-path-sep'
@@ -767,12 +767,12 @@ export function runDependingOnPageType<T>(params: {
767767
return
768768
}
769769

770-
if (isProxyFilename(params.page)) {
770+
if (isProxyFile(params.page)) {
771771
params.onServer()
772772
return
773773
}
774774

775-
if (isMiddlewareFile(params.page)) {
775+
if (isMiddlewareFile(params.page) && !isProxyFile(params.page)) {
776776
if (params.pageRuntime === 'nodejs') {
777777
params.onServer()
778778
return
@@ -1036,11 +1036,12 @@ export async function createEntrypoints(
10361036
: undefined,
10371037
}).import
10381038
}
1039-
const edgeServerBundlePath = isMiddlewareFile(page)
1040-
? serverBundlePath
1041-
.replace(PROXY_FILENAME, MIDDLEWARE_FILENAME)
1042-
.replace('src/', '')
1043-
: serverBundlePath
1039+
const edgeServerBundlePath =
1040+
isMiddlewareFile(page) && !isProxyFile(page)
1041+
? serverBundlePath
1042+
.replace(PROXY_FILENAME, MIDDLEWARE_FILENAME)
1043+
.replace('src/', '')
1044+
: serverBundlePath
10441045
edgeServer[edgeServerBundlePath] = getEdgeServerEntry({
10451046
...params,
10461047
rootDir,

packages/next/src/build/index.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,13 +1214,7 @@ export default async function build(
12141214

12151215
for (const rootPath of rootPaths) {
12161216
const { name: fileBaseName, dir: fileDir } = path.parse(rootPath)
1217-
const isAtConventionLevel =
1218-
fileDir === '/' ||
1219-
fileDir === '/src' ||
1220-
// rootPaths are currently relative paths from the root directory.
1221-
// Add safety check here for unexpected future changes.
1222-
fileDir === dir ||
1223-
fileDir === path.join(dir, 'src')
1217+
const isAtConventionLevel = fileDir === '/' || fileDir === '/src'
12241218

12251219
if (isAtConventionLevel && fileBaseName === MIDDLEWARE_FILENAME) {
12261220
middlewareFilePath = rootPath
@@ -2610,9 +2604,7 @@ export default async function build(
26102604
return serverFilesManifest
26112605
})
26122606

2613-
const middlewareFile = rootPaths.find(
2614-
(p) => p.includes(MIDDLEWARE_FILENAME) || p.includes(PROXY_FILENAME)
2615-
)
2607+
const middlewareFile = proxyFilePath || middlewareFilePath
26162608
let hasNodeMiddleware = false
26172609

26182610
if (middlewareFile) {
@@ -2623,7 +2615,10 @@ export default async function build(
26232615
appDir,
26242616
pageExtensions: config.pageExtensions,
26252617
isDev: false,
2626-
page: 'middleware',
2618+
// Is format of `(/src)/(proxy|middleware).<ext>`, so split by
2619+
// "." and get the first part, regard rest of the extensions
2620+
// to match the `page` value format.
2621+
page: middlewareFile.split('.')[0],
26272622
})
26282623

26292624
if (staticInfo.hadUnsupportedValue) {

packages/next/src/build/output/log.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,12 @@ export function warnOnce(...message: any[]) {
8585
warn(...message)
8686
}
8787
}
88+
89+
const errorOnceCache = new LRUCache<string>(10_000, (value) => value.length)
90+
export function errorOnce(...message: any[]) {
91+
const key = message.join(' ')
92+
if (!errorOnceCache.has(key)) {
93+
errorOnceCache.set(key, key)
94+
error(...message)
95+
}
96+
}

packages/next/src/build/utils.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,6 @@ export function isMiddlewareFilename(file?: string | null) {
105105
)
106106
}
107107

108-
export function isMiddlewareOnlyFilename(file?: string | null) {
109-
return file === MIDDLEWARE_FILENAME || file === `src/${MIDDLEWARE_FILENAME}`
110-
}
111-
112-
export function isProxyFilename(file?: string | null) {
113-
return file === PROXY_FILENAME || file === `src/${PROXY_FILENAME}`
114-
}
115-
116108
export function isInstrumentationHookFilename(file?: string | null) {
117109
return (
118110
file === INSTRUMENTATION_HOOK_FILENAME ||
@@ -1429,6 +1421,10 @@ export function isMiddlewareFile(file: string) {
14291421
)
14301422
}
14311423

1424+
export function isProxyFile(file: string) {
1425+
return file === `/${PROXY_FILENAME}` || file === `/src/${PROXY_FILENAME}`
1426+
}
1427+
14321428
export function isInstrumentationHookFile(file: string) {
14331429
return (
14341430
file === `/${INSTRUMENTATION_HOOK_FILENAME}` ||

test/e2e/app-dir/proxy-runtime/proxy-runtime.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ Next.js can't recognize the exported \`config\` field in route. Proxy does not s
3535
3636
The exported configuration object in a source file needs to have a very specific format from which some properties can be statically parsed at compiled-time.`)
3737
} else {
38-
expect(cliOutput).toContain(`Proxy does not support Edge runtime.`)
38+
expect(cliOutput).toContain(
39+
`Route segment config is not allowed in Proxy file at "./proxy.ts". Proxy always runs on Node.js runtime. Learn more: https://nextjs.org/docs/messages/middleware-to-proxy`
40+
)
3941
}
4042

4143
await next.stop()

0 commit comments

Comments
 (0)