next-runtime icon indicating copy to clipboard operation
next-runtime copied to clipboard

[Bug]: Middleware rewrites cause `TypeError: Invalid URL` when pointing to a directory in `public`

Open cprussin opened this issue 2 years ago • 2 comments

Summary

With a middleware like this:

export default async (request) => new MiddlewareRequest(request).rewrite('/foo');

If there's a folder /foo in public, edge functions throw an error like this:

Oct 13, 08:10:08 PM: error  [next_src_middleware] TypeError: Invalid URL: '/foo/'
    at getSerialization (ext:deno_url/00_url.js:89:11)
    at new URL (ext:deno_url/00_url.js:380:27)
    at parseURL (file:///root/.netlify/edge-functions/next_src_middleware/bundle.js:2706:12)
    at new NextURL (file:///root/.netlify/edge-functions/next_src_middleware/bundle.js:2720:18)
    at adapter (file:///root/.netlify/edge-functions/next_src_middleware/bundle.js:1599:29)
    at eventLoopTick (ext:core/01_core.js:183:11)
    at async handler (file:///root/.netlify/edge-functions/next_src_middleware/index.ts:50:20)
    at async FunctionChain.runFunction (file:///root/src/bootstrap/function_chain.ts:396:22)
    at async FunctionChain.run (file:///root/src/bootstrap/function_chain.ts:316:20)
    at async handleRequest (file:///root/src/bootstrap/handler.ts:80:22)

A link to a reproduction repository

https://github.com/cprussin/netlify-issue-repro

Expected Result

Render the file /public/foo/index.html if it exists, and 404 if it does not.

Actual Result

500 with the message Invalid URL: '/foo/' and the edge function log:

Oct 13, 08:10:08 PM: error  [next_src_middleware] TypeError: Invalid URL: '/foo/'
    at getSerialization (ext:deno_url/00_url.js:89:11)
    at new URL (ext:deno_url/00_url.js:380:27)
    at parseURL (file:///root/.netlify/edge-functions/next_src_middleware/bundle.js:2706:12)
    at new NextURL (file:///root/.netlify/edge-functions/next_src_middleware/bundle.js:2720:18)
    at adapter (file:///root/.netlify/edge-functions/next_src_middleware/bundle.js:1599:29)
    at eventLoopTick (ext:core/01_core.js:183:11)
    at async handler (file:///root/.netlify/edge-functions/next_src_middleware/index.ts:50:20)
    at async FunctionChain.runFunction (file:///root/src/bootstrap/function_chain.ts:396:22)
    at async FunctionChain.run (file:///root/src/bootstrap/function_chain.ts:316:20)
    at async handleRequest (file:///root/src/bootstrap/handler.ts:80:22)

Steps to reproduce

  1. Deploy repo
  2. Navigate to site

Next Runtime version

4.40.2

Is your issue related to the app directory?

  • [X] Yes, I am using the app directory

More information about your build

  • [ ] I am building using the CLI
  • [ ] I am building using file-based configuration (netlify.toml)

What OS are you using?

None

Your netlify.toml file

No response

Your public/_redirects file

No response

Your next.config.js file

`next.config.js`
module.exports = {
  reactStrictMode: true
};

Builds logs (or link to your logs)

Build logs
8:09:05 PM: build-image version: 2a79f2b6b97edab3f858983ec4cea00280a2836a (focal)
8:09:05 PM: buildbot version: c7db8131ac2d97256c1f537cc5bbdf08464b8daa
8:09:05 PM: Fetching cached dependencies
8:09:05 PM: Starting to download cache of 274.0MB
8:09:06 PM: Finished downloading cache in 743ms
8:09:06 PM: Starting to extract cache
8:09:08 PM: Finished extracting cache in 2.036s
8:09:08 PM: Finished fetching cache in 2.849s
8:09:08 PM: Starting to prepare the repo for build
8:09:08 PM: Preparing Git Reference refs/heads/main
8:09:10 PM: Starting to install dependencies
8:09:10 PM: Python version set to 3.8
8:09:10 PM: Attempting Ruby version 2.7.2, read from environment
8:09:11 PM: Using Ruby version 2.7.2
8:09:11 PM: Started restoring cached go cache
8:09:11 PM: Finished restoring cached go cache
8:09:11 PM: go version go1.19.13 linux/amd64
8:09:11 PM: Using PHP version 8.0
8:09:12 PM: Started restoring cached Node.js version
8:09:13 PM: Finished restoring cached Node.js version
8:09:14 PM: v18.18.2 is already installed.
8:09:14 PM: Now using node v18.18.2 (npm v9.8.1)
8:09:14 PM: Enabling Node.js Corepack
8:09:14 PM: Started restoring cached build plugins
8:09:14 PM: Finished restoring cached build plugins
8:09:14 PM: Started restoring cached corepack dependencies
8:09:14 PM: Finished restoring cached corepack dependencies
8:09:14 PM: No pnpm workspaces detected
8:09:14 PM: Started restoring cached node modules
8:09:14 PM: Finished restoring cached node modules
8:09:14 PM: Installing npm packages using pnpm version 8.7.6
8:09:15 PM: Lockfile is up to date, resolution step is skipped
8:09:15 PM: Already up to date
8:09:15 PM: Done in 421ms
8:09:15 PM: npm packages installed using pnpm
8:09:15 PM: Install dependencies script success
8:09:15 PM: Starting build script
8:09:16 PM: Detected 1 framework(s)
8:09:16 PM: "next" at version "13.5.4"
8:09:16 PM: Section completed: initializing
8:09:18 PM: ​
8:09:18 PM: Netlify Build                                                 
8:09:18 PM: ────────────────────────────────────────────────────────────────
8:09:18 PM: ​
8:09:18 PM: ❯ Version
8:09:18 PM:   @netlify/build 29.22.5
8:09:18 PM: ​
8:09:18 PM: ❯ Flags
8:09:18 PM:   baseRelDir: true
8:09:18 PM:   buildId: 652a065085dce700083fb0d8
8:09:18 PM:   deployId: 652a065085dce700083fb0da
8:09:18 PM: ​
8:09:18 PM: ❯ Current directory
8:09:18 PM:   /opt/build/repo
8:09:18 PM: ​
8:09:18 PM: ❯ Config file
8:09:18 PM:   No config file was defined: using default values.
8:09:18 PM: ​
8:09:18 PM: ❯ Context
8:09:18 PM:   production
8:09:18 PM: ​
8:09:18 PM: ❯ Using Next.js Runtime - v4.40.2
8:09:19 PM: ​
8:09:19 PM: @netlify/plugin-nextjs (onPreBuild event)                     
8:09:19 PM: ────────────────────────────────────────────────────────────────
8:09:19 PM: ​
8:09:19 PM: Next.js cache restored.
8:09:19 PM: Netlify configuration property "build.environment.NEXT_PRIVATE_TARGET" value changed.
8:09:19 PM: ​
8:09:19 PM: (@netlify/plugin-nextjs onPreBuild completed in 126ms)
8:09:19 PM: ​
8:09:19 PM: Build command from Netlify app                                
8:09:19 PM: ────────────────────────────────────────────────────────────────
8:09:19 PM: ​
8:09:19 PM: $ pnpm run build
8:09:19 PM: > [email protected] build /opt/build/repo
8:09:19 PM: > next build
8:09:20 PM:    Creating an optimized production build ...
8:09:21 PM:  ✓ Compiled successfully
8:09:21 PM:    Linting and checking validity of types ...
8:09:21 PM:    Collecting page data ...
8:09:22 PM:    Generating static pages (0/4) ...
8:09:23 PM:    Generating static pages (1/4)
8:09:23 PM:    Generating static pages (2/4)
8:09:23 PM:    Generating static pages (3/4)
8:09:23 PM:  ✓ Generating static pages (4/4)
8:09:25 PM:    Finalizing page optimization ...
8:09:25 PM:    Collecting build traces ...
8:09:26 PM: Route (app)                              Size     First Load JS
8:09:26 PM: ┌ ○ /                                    140 B          80.5 kB
8:09:26 PM: └ ○ /_not-found                          883 B          81.2 kB
8:09:26 PM: + First Load JS shared by all            80.3 kB
8:09:26 PM:   ├ chunks/95-667055ea6e31680b.js        27.5 kB
8:09:26 PM:   ├ chunks/b97a3a71-357a375a396d8622.js  51 kB
8:09:26 PM:   ├ chunks/main-app-968e29e1596532ac.js  229 B
8:09:26 PM:   └ chunks/webpack-bf1a64d1eafd2816.js   1.66 kB
8:09:26 PM: ƒ Middleware                             27.9 kB
8:09:26 PM: ○  (Static)  automatically rendered as static HTML (uses no initial props)
8:09:26 PM: ​
8:09:26 PM: (build.command completed in 6.6s)
8:09:26 PM: ​
8:09:26 PM: @netlify/plugin-nextjs (onBuild event)                        
8:09:26 PM: ────────────────────────────────────────────────────────────────
8:09:26 PM: ​
8:09:26 PM: Moving static page files to serve from CDN...
8:09:26 PM: Moved 6 files
8:09:26 PM: You are not using Netlify Edge Functions for image format detection. Set env var "NEXT_FORCE_EDGE_IMAGES=true" to enable.
8:09:26 PM: ✨ Deploying middleware and functions to Netlify Edge Functions ✨
8:09:26 PM: Netlify configuration property "redirects" value changed to [
8:09:26 PM:   { from: "/_next/static/*", to: "/static/:splat", status: 200 },
8:09:26 PM:   {
8:09:26 PM:     from: "/_next/image*",
8:09:26 PM:     query: { url: ":url", w: ":width", q: ":quality" },
8:09:26 PM:     to: "/_ipx/w_:width,q_:quality/:url",
8:09:26 PM:     status: 301
8:09:26 PM:   },
8:09:26 PM:   { from: "/_ipx/*", to: "/.netlify/builders/_ipx", status: 200 },
8:09:26 PM:   {
8:09:26 PM:     from: "/api/*",
8:09:26 PM:     to: "/.netlify/functions/___netlify-handler",
8:09:26 PM:     status: 200
8:09:26 PM:   },
8:09:26 PM:   {
8:09:26 PM:     from: "/foo/index.html",
8:09:26 PM:     to: "/foo/index.html",
8:09:26 PM:     conditions: { Cookie: [Array] },
8:09:26 PM:     status: 200
8:09:26 PM:   },
8:09:26 PM:   {
8:09:26 PM:     from: "/*",
8:09:26 PM:     to: "/.netlify/functions/___netlify-handler",
8:09:26 PM:     status: 200,
8:09:26 PM:     conditions: { Cookie: [Array] },
8:09:26 PM:     force: true
8:09:26 PM:   },
8:09:26 PM:   {
8:09:26 PM:     from: "/_next/data/tep2MhD4Z2WLvbHajTyX-/_not-found.json",
8:09:26 PM:     to: "/.netlify/functions/___netlify-handler",
8:09:26 PM:     status: 200,
8:09:26 PM:     force: false
8:09:26 PM:   },
8:09:26 PM:   {
8:09:26 PM:     from: "/_not-found",
8:09:26 PM:     to: "/.netlify/functions/___netlify-handler",
8:09:26 PM:     status: 200,
8:09:26 PM:     force: false
8:09:26 PM:   },
8:09:26 PM:   {
8:09:26 PM:     from: "/*",
8:09:26 PM:     to: "/.netlify/functions/___netlify-handler",
8:09:26 PM:     status: 200
8:09:26 PM:   }
8:09:26 PM: ].
8:09:26 PM: ​
8:09:26 PM: (@netlify/plugin-nextjs onBuild completed in 121ms)
8:09:26 PM: ​
8:09:26 PM: Functions bundling                                            
8:09:26 PM: ────────────────────────────────────────────────────────────────
8:09:26 PM: ​
8:09:26 PM: Packaging Functions from .netlify/functions-internal directory:
8:09:26 PM:  - ___netlify-handler/___netlify-handler.js
8:09:26 PM:  - ___netlify-odb-handler/___netlify-odb-handler.js
8:09:26 PM:  - _ipx/_ipx.js
8:09:26 PM: ​
8:09:57 PM: ​
8:09:57 PM: (Functions bundling completed in 31.1s)
8:09:57 PM: ​
8:09:57 PM: Edge Functions bundling                                       
8:09:57 PM: ────────────────────────────────────────────────────────────────
8:09:57 PM: ​
8:09:57 PM: Packaging Edge Functions from .netlify/edge-functions directory:
8:09:57 PM:  - next_src_middleware
8:09:57 PM:  - rsc-data
8:09:58 PM: ​
8:09:58 PM: (Edge Functions bundling completed in 1.3s)
8:09:58 PM: ​
8:09:58 PM: @netlify/plugin-nextjs (onPostBuild event)                    
8:09:58 PM: ────────────────────────────────────────────────────────────────
8:09:58 PM: ​
8:09:58 PM: Next.js cache saved.
8:09:58 PM: ​
8:09:58 PM: (@netlify/plugin-nextjs onPostBuild completed in 92ms)
8:09:58 PM: ​
8:09:58 PM: Deploy site                                                   
8:09:58 PM: ────────────────────────────────────────────────────────────────
8:09:58 PM: ​
8:09:58 PM: Starting to deploy site from ".next"
8:09:58 PM: Calculating files to upload
8:09:59 PM: 8 new files to upload
8:09:59 PM: 2 new functions to upload
8:10:07 PM: Post processing - redirect rules
8:10:07 PM: Post processing done
8:10:07 PM: Section completed: postprocessing
8:10:07 PM: Starting post processing
8:10:07 PM: Skipping HTML post processing
8:10:07 PM: Post processing - header rules
8:10:07 PM: Site is live ✨
8:10:07 PM: Section completed: deploying
8:10:07 PM: Site deploy was successfully initiated
8:10:07 PM: ​
8:10:07 PM: (Deploy site completed in 8.2s)
8:10:07 PM: ​
8:10:07 PM: Netlify Build Complete                                        
8:10:07 PM: ────────────────────────────────────────────────────────────────
8:10:07 PM: ​
8:10:07 PM: (Netlify Build completed in 49s)
8:10:07 PM: Caching artifacts
8:10:07 PM: Started saving node modules
8:10:07 PM: Finished saving node modules
8:10:07 PM: Started saving build plugins
8:10:07 PM: Finished saving build plugins
8:10:07 PM: Started saving corepack cache
8:10:07 PM: Finished saving corepack cache
8:10:07 PM: Started saving pip cache
8:10:07 PM: Finished saving pip cache
8:10:07 PM: Started saving emacs cask dependencies
8:10:07 PM: Finished saving emacs cask dependencies
8:10:07 PM: Started saving maven dependencies
8:10:07 PM: Finished saving maven dependencies
8:10:07 PM: Started saving boot dependencies
8:10:07 PM: Finished saving boot dependencies
8:10:07 PM: Started saving rust rustup cache
8:10:07 PM: Finished saving rust rustup cache
8:10:07 PM: Started saving go dependencies
8:10:07 PM: Finished saving go dependencies
8:10:07 PM: Build script success
8:10:07 PM: Section completed: building
8:10:08 PM: Uploading Cache of size 273.9MB
8:10:11 PM: Section completed: cleanup
8:10:11 PM: Finished processing build request in 1m5.883s

Function logs

Function logs
Oct 13, 08:10:08 PM: error  [next_src_middleware] TypeError: Invalid URL: '/foo/'
    at getSerialization (ext:deno_url/00_url.js:89:11)
    at new URL (ext:deno_url/00_url.js:380:27)
    at parseURL (file:///root/.netlify/edge-functions/next_src_middleware/bundle.js:2706:12)
    at new NextURL (file:///root/.netlify/edge-functions/next_src_middleware/bundle.js:2720:18)
    at adapter (file:///root/.netlify/edge-functions/next_src_middleware/bundle.js:1599:29)
    at eventLoopTick (ext:core/01_core.js:183:11)
    at async handler (file:///root/.netlify/edge-functions/next_src_middleware/index.ts:50:20)
    at async FunctionChain.runFunction (file:///root/src/bootstrap/function_chain.ts:396:22)
    at async FunctionChain.run (file:///root/src/bootstrap/function_chain.ts:316:20)
    at async handleRequest (file:///root/src/bootstrap/handler.ts:80:22)

.next JSON files

No response

cprussin avatar Oct 14 '23 03:10 cprussin

Netlify-only internal link to conversation with customer on this topic

nolessafool avatar Oct 16 '23 18:10 nolessafool

See comment here on the related issue #2332, the same comment applies to this ticket as well.

cprussin avatar Oct 23 '23 15:10 cprussin

Hi @cprussin

The team at Netlify have now released the new v5 Next.js runtime which has support for both pages and app router, on-demand and time-based revalidation, automatic fine-grained cache control, and automatic image optimization using Netlify's image CDN.

You can find the documentation and additional information on the new runtime here: Next.js on Netlify

The v4 runtime is now in the maintenance support phase with no new features being added. Occasional bug fixes and security patches will be applied when needed.

Thanks!

serhalp avatar Jun 04 '24 16:06 serhalp