最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - NextJS Middleware Matcher For Dynamic Routes - Stack Overflow

matteradmin5PV0评论

I'm building a website with subdomains. Each subdomain is mapped to a file-based page using middleware. Below is an example of how I'm mapping subdomains to pages:

  • app maps to /home
  • app/pricing maps to /home/pricing
  • subdomain1.app/dashboard maps to /_subdomains/subdomain1/dashboard
  • subdomain2.app/dashboard/home maps to /_subdomains/subdomain2/dashboard/home

app, subdomain1.app and subdomain1.app/dashboard/ and everything else are working fine, but when I try to access subdomain1.app/dashboard/home I'm getting 404 Not Found.

Here's my folder structure:

pages/
├── _subdomains/
│   └── [subdomain]/
│       ├── dashboard/
│       │   ├── home.tsx
│       │   └── index.tsx
│       └── index.tsx
├── home/
│   └── ...
└── _app.tsx
import { NextRequest, NextResponse } from 'next/server';

export const config = {
  // I have a feeling this isn't matching /_subdomains/subdomain/dashboard/home
  matcher: ['/', '/([^/.]*)', '/auth/([^/.]*)', '/_subdomains/:path*'], 
};

export default async function middleware(req: NextRequest) {
  const url = req.nextUrl;
  const hostname = req.headers.get('host') || process.env.ROOT_DOMAIN;

  if (!hostname) {
    throw Error('Middleware -> No hostname');
  }

  const currentHost = hostname.replace(`.${process.env.ROOT_DOMAIN}`, '');

  if (currentHost === process.env.ROOT_DOMAIN) {
    url.pathname = `/home${url.pathname}`;
  } else {
    console.log(`/_subdomains/${currentHost}${url.pathname}`)
    url.pathname = `/_subdomains/${currentHost}${url.pathname}`;
  }

  return NextResponse.rewrite(url);
}

I'm fairly certain it's the matcher that isn't working but I don't know why.

Matching /_subdomains/:path* should match /_subdomains/a/b/c according to the docs but it isn't working in this case. Or it could be another issue I'm not sure

I'm building a website with subdomains. Each subdomain is mapped to a file-based page using middleware. Below is an example of how I'm mapping subdomains to pages:

  • app. maps to /home
  • app./pricing maps to /home/pricing
  • subdomain1.app./dashboard maps to /_subdomains/subdomain1/dashboard
  • subdomain2.app./dashboard/home maps to /_subdomains/subdomain2/dashboard/home

app., subdomain1.app. and subdomain1.app./dashboard/ and everything else are working fine, but when I try to access subdomain1.app./dashboard/home I'm getting 404 Not Found.

Here's my folder structure:

pages/
├── _subdomains/
│   └── [subdomain]/
│       ├── dashboard/
│       │   ├── home.tsx
│       │   └── index.tsx
│       └── index.tsx
├── home/
│   └── ...
└── _app.tsx
import { NextRequest, NextResponse } from 'next/server';

export const config = {
  // I have a feeling this isn't matching /_subdomains/subdomain/dashboard/home
  matcher: ['/', '/([^/.]*)', '/auth/([^/.]*)', '/_subdomains/:path*'], 
};

export default async function middleware(req: NextRequest) {
  const url = req.nextUrl;
  const hostname = req.headers.get('host') || process.env.ROOT_DOMAIN;

  if (!hostname) {
    throw Error('Middleware -> No hostname');
  }

  const currentHost = hostname.replace(`.${process.env.ROOT_DOMAIN}`, '');

  if (currentHost === process.env.ROOT_DOMAIN) {
    url.pathname = `/home${url.pathname}`;
  } else {
    console.log(`/_subdomains/${currentHost}${url.pathname}`)
    url.pathname = `/_subdomains/${currentHost}${url.pathname}`;
  }

  return NextResponse.rewrite(url);
}

I'm fairly certain it's the matcher that isn't working but I don't know why.

Matching /_subdomains/:path* should match /_subdomains/a/b/c according to the docs but it isn't working in this case. Or it could be another issue I'm not sure

Share Improve this question asked Sep 16, 2022 at 4:04 SamiSami 3053 silver badges16 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Fixed by changing the matcher to

matcher: [
     /*
      * Match all paths except for:
      * 1. /api routes
      * 2. /_next (Next.js internals)
      * 3. /fonts (inside /public)
      * 4. /examples (inside /public)
      * 5. all root files inside /public (e.g. /favicon.ico)
      */
     "/((?!api|_next|fonts|examples|[\\w-]+\\.\\w+).*)",
   ],

Thanks to this

Post a comment

comment list (0)

  1. No comments so far