Skip to content

[Bug]: Next.js middlewares are currently broken on Netlify #1141

Closed
@ivorpad

Description

@ivorpad

Summary

Next.js throws an error when hostname and port are not provided in the custom server since v12.0.5-canary.18 — as a result it breaks middleware in Netlify:

[email protected]#L725-L729

// For middleware to "fetch" we must always provide an absolute URL
const url = getRequestMeta(params.request, '__NEXT_INIT_URL')!
 if (!url.startsWith('http')) {
   throw new Error(
     'To use middleware you must provide a `hostname` and `port` to the Next.js Server'
   )
}

Screenshot 2022-01-18 at 23 47 04

Ref: https://github.com/netlify/netlify-plugin-nextjs/blob/main/src/templates/getHandler.ts#L46-L50

Steps to reproduce

  1. Fork this simple Next.js app (npx create-next-app with one middleware) https://github.com/ivorpad/repro-nextjs-middleware
  2. Deploy to Netlify
  3. Go to /api/users
  4. Watch the ___netlify-handler function logs

A link to a reproduction repository

https://github.com/ivorpad/repro-nextjs-middleware

Plugin version

4.2.0

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?

No response

Your netlify.toml file

`netlify.toml`
[build]
command = "npm run build"
functions = "out_functions"
publish = ".next"

[[plugins]]
package = "@netlify/plugin-nextjs"

Your public/_redirects file

`_redirects`
# Paste content of your `_redirects` file here

Your next.config.js file

`next.config.js`
# Paste content of your `next.config.js` file here. Check there is no private info in there.

Builds logs (or link to your logs)

Build logs
# Paste logs here

Function logs

Function logs
# Paste logs here

.next JSON files

generated .next JSON files
# Paste file contents here. Please check there isn't any private info in them
# You can either build locally, or download the deploy from Netlify by clicking the arrow next to the deploy time.

Metadata

Metadata

Assignees

Labels

type: bugcode to address defects in shipped code

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions