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.

`src/pages/_middleware` running for requests outside of `pages` folder

See original GitHub issue

What version of Next.js are you using?

12.0.3

What version of Node.js are you using?

14.16.0

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

Vercel

Describe the Bug

Our project utilizes a src folder to hold our pages directory. Inside we added a _middleware.js. If I log the req object inside the middleware, it seems to be running for not only our pages, but for our assets as well.

Screen Shot 2021-11-12 at 5 01 29 PM

This has had the effect of breakout out site.webmanifest failing requests on URLs inside our next.config.js rewrites array, and causing certain edge functions to time out.

Screen Shot 2021-11-12 at 5 15 08 PM This is our middleware which ignores a rewrite to `/system` and only attempts to run on req objects containing a page name. This works locally however fails when deployed to Vercel.
import { NextResponse } from "next/server";

const middleware = (req) => {
  const { page, nextUrl } = req;

  const isActualPage = !!page.name;

  const hasCookies =
    req.cookies.refresh && req.cookies.payload && req.cookies.access;

  const publicPage = /^\/(signin|signup).*$/.test(nextUrl.pathname);

  const companyOrInvestorPage = /^\/(company|investor)\/.*$/.test(
    nextUrl.pathname
  );

  const systemUrl = /^\/(system).*$/.test(nextUrl.pathname);

  if (systemUrl || !isActualPage) {
    return NextResponse.next();
  }

  if (isActualPage && !publicPage && !companyOrInvestorPage && !hasCookies) {
    return NextResponse.redirect(`/signin?redirect=${nextUrl.pathname}`);
  }

  if (isActualPage && publicPage && hasCookies) {
    return NextResponse.redirect(`/`);
  }

  return NextResponse.next();
};

export default middleware;


Expected Behavior

We expected this to only run on the request to the page, so we would not have to ignore certain path names

To Reproduce

Create a project with a rewrite, a src/pages folder with a _middleware.js inside.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
edelfradecommented, Nov 14, 2021

Possible duplicate of #31324

0reactions
github-actions[bot]commented, Jul 20, 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

middlewares - ReferenceError: self is not defined · Issue #38149
PS: I'm using a custom pageExtensions . FIXED Just moved the file from /src/pages/middleware.page.ts to /src/middleware.page.
Read more >
Using python Requests with javascript pages - Stack Overflow
No, Requests is an http library. It cannot run javascript. – sberry. Oct 16, 2014 at 20:48. 2.
Read more >
Folder Approval Double Request - Power Platform Community
Hello,. I am trying to create a folder approval flow so that when a sub-folder is added to my Pre-Approved folder, a request...
Read more >
requests-HTML v0.3.4 documentation
Note, the first time you ever run the render() method, it will download Chromium into your home directory (e.g. ~/.pyppeteer/ ). This only...
Read more >
Adding and Managing Documents and Links - NextRequest
If you would like to move a document/link out of all folders select the option “no folder.” Click the SAVE button. New Request...
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