You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
How are you deploying your application? (if relevant)
in futher i will use Vercel
Describe the Bug
I'm creating a multi-tenant app, and when I create a main middleware to redirect the host to _sites/[site] path, the _middleware.ts file inside _sites/[site] is not firing.
Making some tests, the problem is when I use a rewirte method.
Here's my code:
file: pages/_middleware.ts
import{NextRequest,NextResponse}from'next/server'exportdefaultfunctionmiddleware(req: NextRequest){consturl=req.nextUrl.clone()// clone the request urlconst{ pathname }=req.nextUrl// Get hostname (e.g. vercel.com, test.vercel.app, etc.)consthostname=req.headers.get('host')// If localhost, assign the host value manually// If prod, get the custom domain/subdomain value by removing the root URL// (in the case of "test.vercel.app", "vercel.app" is the root URL)constcurrentHost=process.env.NODE_ENV=='production'
? hostname?.replace(`.telecodeapp.com`,'').replace(`.telecode.app`,'')// PUT YOUR DOMAIN HERE
: hostname?.replace(`.localhost:3000`,'')// Prevent security issues – users should not be able to canonically access// the pages/sites folder and its respective contents. This can also be done// via rewrites to a custom 404 pageif(pathname.startsWith(`/_sites`)){returnnewResponse(null,{status: 404})}if(!pathname.includes('.')&&// exclude all files in the public folder!pathname.startsWith('/api')// exclude all API routes){// rewrite to the current hostname under the pages/sites folder// the main logic component will happen in pages/sites/[site]/index.tsxurl.pathname=`/_sites/${currentHost}${pathname}`returnNextResponse.rewrite(url)}}
import{NextRequest,NextResponse}from'next/server'exportdefaultfunctionmiddleware(req: NextRequest){consturl=req.nextUrl.clone()// clone the request urlconst{ pathname }=req.nextUrl// Get hostname (e.g. vercel.com, test.vercel.app, etc.)consthostname=req.headers.get('host')// If localhost, assign the host value manually// If prod, get the custom domain/subdomain value by removing the root URL// (in the case of "test.vercel.app", "vercel.app" is the root URL)constcurrentHost=process.env.NODE_ENV=='production'
? hostname?.replace(`.telecodeapp.com`,'').replace(`.telecode.app`,'')// PUT YOUR DOMAIN HERE
: hostname?.replace(`.localhost:3000`,'')// Prevent security issues – users should not be able to canonically access// the pages/sites folder and its respective contents. This can also be done// via rewrites to a custom 404 pageif(pathname.startsWith(`/_sites`)){returnnewResponse(null,{status: 404})}if(!pathname.includes('.')&&// exclude all files in the public folder!pathname.startsWith('/api')// exclude all API routes){// rewrite to the current hostname under the pages/sites folder// the main logic component will happen in pages/sites/[site]/index.tsxurl.pathname=`/_sites/${currentHost}${pathname}`returnNextResponse.rewrite(url)}}
drafenous
changed the title
Nextjs 12 no firing _middleware.ts file when NextResponse.rewrite()
Nextjs 12 not firing _middleware.ts file when NextResponse.rewrite()
May 13, 2022
Yeah, this is a bit odd. There was an open discussion here, without much conversation going on really, #36325.
In my case, I ended up using redirects and rewrites. I had a sub section of pages that didn't need rewrites, so I redirect to them, to trigger the middleware, at the price of unmasking the URL and triggering a page load on the client.
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.
Verify canary release
Provide environment information
What browser are you using? (if relevant)
Chrome 101.0.4951.54
How are you deploying your application? (if relevant)
in futher i will use Vercel
Describe the Bug
I'm creating a multi-tenant app, and when I create a main middleware to redirect the host to _sites/[site] path, the _middleware.ts file inside _sites/[site] is not firing.
Making some tests, the problem is when I use a rewirte method.
Here's my code:
file: pages/_middleware.ts
file: /pages/_sites/[site]/_middleware.ts
This second file is not firing.
Expected Behavior
Run middlewares inside _sites/[site] folder.
To Reproduce
Create these files:
file: pages/_middleware.ts
file: /pages/_sites/[site]/_middleware.ts
And try to access any url inside /pages/_sites/[site] folder.
The text was updated successfully, but these errors were encountered: