Skip to content

Commit

Permalink
Fix: Build compilation warning when using middleware (#57685)
Browse files Browse the repository at this point in the history
### Fixing a bug

- Related issues linked using `fixes #57533`
- Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md

### What?

If there is a `middleware.js` or `middleware.ts` file in Next.JS 14, running `next build` shows the following warning:

```
./node_modules/next/dist/esm/shared/lib/router/utils/app-paths.js
A Node.js module is loaded ('url' at line 3) which is not supported in the Edge Runtime.
Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime

Import trace for requested module:
./node_modules/next/dist/esm/shared/lib/router/utils/app-paths.js
```
This PR will remove the warning.

Closes NEXT-
Fixes #57533
  • Loading branch information
Nayeem-XTREME committed Oct 30, 2023
1 parent 24a71dc commit 3248ee7
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions packages/next/src/shared/lib/router/utils/app-paths.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ensureLeadingSlash } from '../../page-path/ensure-leading-slash'
import { isGroupSegment } from '../../segment'
import { parse, format } from 'url'

/**
* Normalizes an app route so it represents the actual request path. Essentially
Expand Down Expand Up @@ -70,12 +69,12 @@ export function normalizeRscURL(url: string) {
* @param url the url to normalize
*/
export function normalizePostponedURL(url: string) {
const parsed = parse(url)
let { pathname } = parsed
const parsed = new URL(url)
const { pathname } = parsed
if (pathname && pathname.startsWith('/_next/postponed')) {
pathname = pathname.substring('/_next/postponed'.length) || '/'
parsed.pathname = pathname.substring('/_next/postponed'.length) || '/'

return format({ ...parsed, pathname })
return parsed.toString()
}

return url
Expand Down

0 comments on commit 3248ee7

Please sign in to comment.