Skip to content

Commit 3f5d774

Browse files
authored
fix: node.js module import error when using middleware (#77945)
The previous [backport](#77794) caused an issue due to how imports are resolved on canary vs v14, resulting in a message about using node.js APIs in middleware. This moves the import to the `web` directory and adds a test case for the warning. Closes NDX-1014
1 parent 43f10b8 commit 3f5d774

File tree

5 files changed

+29
-21
lines changed

5 files changed

+29
-21
lines changed

packages/next/src/server/base-server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ import { removeTrailingSlash } from '../shared/lib/router/utils/remove-trailing-
7272
import { denormalizePagePath } from '../shared/lib/page-path/denormalize-page-path'
7373
import * as Log from '../build/output/log'
7474
import escapePathDelimiters from '../shared/lib/router/utils/escape-path-delimiters'
75-
import { getUtils, normalizeNextQueryParam } from './server-utils'
75+
import { getUtils } from './server-utils'
7676
import isError, { getProperError } from '../lib/is-error'
7777
import {
7878
addRequestMeta,
@@ -111,6 +111,7 @@ import { sendResponse } from './send-response'
111111
import { handleInternalServerErrorResponse } from './future/route-modules/helpers/response-handlers'
112112
import {
113113
fromNodeOutgoingHttpHeaders,
114+
normalizeNextQueryParam,
114115
toNodeOutgoingHttpHeaders,
115116
} from './web/utils'
116117
import { CACHE_ONE_YEAR, NEXT_CACHE_TAGS_HEADER } from '../lib/constants'

packages/next/src/server/server-utils.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,6 @@ export function normalizeVercelUrl(
5555
}
5656
}
5757

58-
/**
59-
* Normalizes `nxtP` and `nxtI` query param values to remove the prefix.
60-
* This function does not mutate the input key; it calls the provided function
61-
* with the normalized key.
62-
*/
63-
export function normalizeNextQueryParam(
64-
key: string,
65-
onKeyNormalized: (normalizedKey: string) => void
66-
) {
67-
const prefixes = [NEXT_QUERY_PARAM_PREFIX, NEXT_INTERCEPTION_MARKER_PREFIX]
68-
for (const prefix of prefixes) {
69-
if (key !== prefix && key.startsWith(prefix)) {
70-
const normalizedKey = key.substring(prefix.length)
71-
onKeyNormalized(normalizedKey)
72-
}
73-
}
74-
}
75-
7658
export function interpolateDynamicPath(
7759
pathname: string,
7860
params: ParsedUrlQuery,

packages/next/src/server/web/adapter.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { RequestData, FetchEventResult } from './types'
22
import type { RequestInit } from './spec-extension/request'
33
import { PageSignatureError } from './error'
4-
import { fromNodeOutgoingHttpHeaders } from './utils'
4+
import { fromNodeOutgoingHttpHeaders, normalizeNextQueryParam } from './utils'
55
import { NextFetchEvent } from './spec-extension/fetch-event'
66
import { NextRequest } from './spec-extension/request'
77
import { NextResponse } from './spec-extension/response'
@@ -18,7 +18,6 @@ import { getTracer } from '../lib/trace/tracer'
1818
import type { TextMapGetter } from 'next/dist/compiled/@opentelemetry/api'
1919
import { MiddlewareSpan } from '../lib/trace/constants'
2020
import { getEdgePreviewProps } from './get-edge-preview-props'
21-
import { normalizeNextQueryParam } from '../server-utils'
2221

2322
export class NextRequestHint extends NextRequest {
2423
sourcePage: string

packages/next/src/server/web/utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import type { OutgoingHttpHeaders } from 'http'
2+
import {
3+
NEXT_INTERCEPTION_MARKER_PREFIX,
4+
NEXT_QUERY_PARAM_PREFIX,
5+
} from '../../lib/constants'
26

37
/**
48
* Converts a Node.js IncomingHttpHeaders object to a Headers object. Any
@@ -146,3 +150,21 @@ export function validateURL(url: string | URL): string {
146150
)
147151
}
148152
}
153+
154+
/**
155+
* Normalizes `nxtP` and `nxtI` query param values to remove the prefix.
156+
* This function does not mutate the input key; it calls the provided function
157+
* with the normalized key.
158+
*/
159+
export function normalizeNextQueryParam(
160+
key: string,
161+
onKeyNormalized: (normalizedKey: string) => void
162+
) {
163+
const prefixes = [NEXT_QUERY_PARAM_PREFIX, NEXT_INTERCEPTION_MARKER_PREFIX]
164+
for (const prefix of prefixes) {
165+
if (key !== prefix && key.startsWith(prefix)) {
166+
const normalizedKey = key.substring(prefix.length)
167+
onKeyNormalized(normalizedKey)
168+
}
169+
}
170+
}

test/e2e/app-dir/app-middleware/app-middleware.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ createNextDescribe(
2323
}, /app-dir/)
2424
})
2525

26+
it('should not include any warnings about using Node.js APIs', async () => {
27+
expect(next.cliOutput).not.toContain('A Node.js module is loaded')
28+
})
29+
2630
describe.each([
2731
{
2832
title: 'Serverless Functions',

0 commit comments

Comments
 (0)