question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Middleware redirection not working after upgrading to v12.2.5

See original GitHub issue

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System: Platform: win32 Arch: x64 Version: Windows 11 Enterprise Binaries: Node: 16.14.0 npm: 8.5.2 Yarn: N/A pnpm: N/A Relevant packages: next: 12.2.6-canary.0 eslint-config-next: 12.2.5 react: 18.2.0 react-dom: 18.2.0

What browser are you using? (if relevant)

Chrome and Edge, all up to date

How are you deploying your application? (if relevant)

npx nx serve ui

Describe the Bug

When executing the application, the root ‘/’ is to be redirected to ‘/landing’. Earlier we were using next v12.0.7 and had placed the _middleware.js inside the /pages folder. The middleware was working fine before I upgrade my Next version to the 12.2.x.

After upgrading next to v12.2.5, we placed the middleware.js file in the root next to pages folder. Now we are not able to get the paths and it gives syntaxErrors regarding the same.

//-- middleware code start // the console.log is here just as a test, more info on the print below

console.log('request-->', request);
const { pathname } = request.nextUrl
    if (pathname == '/') {
        return NextResponse.rewrite('/landing')
    }
    return NextResponse.next()
}

//-- middleware code end

If I comment out or exclude the middleware, the error stop, but the route doesnot change accordingly. It should redirect the page everytime root ‘/’ is hit.

Also, this is what is the console.log(‘request–>’, request) gives: //— request console.log start

{ cookies: {},
  geo: {},
  ip: undefined,
  nextUrl:
   { href: 'http://undefined:4200/_next/static/development/_devMiddlewareManifest.json',
     origin: 'http://undefined:4200',
     protocol: 'http:',
     username: '',
     password: '',
     host: 'undefined:4200',
     hostname: 'undefined',
     port: '4200',
     pathname: '/_next/static/development/_devMiddlewareManifest.json',
     search: '',
     searchParams: { [Symbol(impl)]: { _list: [], _url: [Object], [Symbol(wrapper)]: [Circular] } },
     hash: '' },
  url: 'http://undefined:4200/_next/static/development/_devMiddlewareManifest.json',
  bodyUsed: false,
  cache: 'default',
  credentials: 'same-origin',
  destination: '',
  headers:
   { accept: '*/*',
     accept-encoding: 'gzip, deflate, br',
     accept-language: 'en-US,en;q=0.9',
     connection: 'keep-alive',
     host: 'localhost:4200',
     referer: 'http://localhost:4200/',
   } }

//— request console.log end

Expected Behavior

I need the middleware to redirect to page /landing whenever ‘/’ is called.

Link to reproduction

Can’t publicly share the repo as it is a company project.

To Reproduce

Can’t publicly share the repo as it is a company project. Will share any other information if needed.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
PodStuartcommented, Aug 24, 2022

I’m seeing the same problem using NextAuth.js middleware. I tried to recreate a minimal example but couldn’t reproduce the same problem. Eventually realised this was only failing when I was running my dev server (using Nx). I found that adding “hostname”: “localhost” to my Nx project.json file fixed the problem.

0reactions
github-actions[bot]commented, Oct 26, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

asp.net core middleware not redirecting - Stack Overflow
I created a login middleware that will redirect to login page if the user is not authenticated. I get this error : var...
Read more >
Release Notes — Airflow Documentation
Airflow 2.5.0 (2022-12-02) ... Fix trigger dag redirect from task instance log view (#21239) ... Upgrade Azure blob to v12 (#12188).
Read more >
@next/bundle-analyzer 12.2.5 on Node.js NPM - NewReleases.io
New release @next/bundle-analyzer version 12.2.5 v12.2.5 on Node.js NPM. ... strict Redirect type: #38277; fix(next): dev server starting when importing a ...
Read more >
5 Oracle Access Management
Section 5.1.3.1, "Federation Metadata is not Accessible after Upgrade" · Section 5.1.3.2, "Federation Redirect URLs May be Overwritten in Concurrency Mode".
Read more >
Node.js and Express Tutorial: Authentication Using Passport
We tested this tutorial using Node.js v12.16.0 and npm v6.13.4 . ... Since Passport.js is a middleware function, the verify callback also ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found