Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 of 2 tasks
ivorpad opened this issue Jan 19, 2022 · 2 comments · Fixed by #1149
Closed
1 of 2 tasks

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

ivorpad opened this issue Jan 19, 2022 · 2 comments · Fixed by #1149
Assignees
Labels
type: bug code to address defects in shipped code

Comments

@ivorpad
Copy link

ivorpad commented Jan 19, 2022

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.
@ivorpad ivorpad added the type: bug code to address defects in shipped code label Jan 19, 2022
@ascorbic ascorbic self-assigned this Jan 19, 2022
@guscsales
Copy link

I have the same issue here but running the server locally. I did an update to version 12.0.8 yesterday and this error started happening. I rollback to 12.0.7 and worked. Maybe there is some bug in the latest version.

@ascorbic
Copy link
Contributor

Thank you both for the detailed report and information. I have a fix ready and should be able to put out a release on Monday.

@kodiakhq kodiakhq bot closed this as completed in #1149 Jan 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug code to address defects in shipped code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants