`src/pages/_middleware` running for requests outside of `pages` folder
See original GitHub issueWhat 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.
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.
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:
- Created 2 years ago
- Reactions:1
- Comments:9 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Possible duplicate of #31324
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.