Replies: 12 comments 11 replies
-
I don't mind the middleware running on public assets, however I'd love if the middleware provided a boolean flag whether the request is for a public asset. Currently I'm using the solution from this example, which basically checks if the url has a file extension. This mostly works, but if your app also serves protected files outside the public folder (e.g. generating .pdf file on demand), you'll have to add extra logic in middleware to handle those files differently. |
Beta Was this translation helpful? Give feedback.
-
I know it might not be ideal, but you could do this? function shouldExclude(request: NextRequest) {
const path = request.nextUrl.pathname;
return (
path.startsWith('/api') || // exclude all API routes
path.startsWith('/static') || // exclude static files
path.includes('.') // exclude all files in the public folder
);
} I think for Perhaps least know, but similarly, placing a |
Beta Was this translation helpful? Give feedback.
-
In case anyone else is browsing this thread now that middleware is stable (Next.js v12.2.0+), here's
|
Beta Was this translation helpful? Give feedback.
-
robots.txt should probably be ignored too: export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - robots.txt (robots file)
*/
"/((?!api|_next/static|_next/image|favicon.ico|robots.txt).*)",
],
}; https://nextjs.org/docs/app/building-your-application/routing/middleware#matcher |
Beta Was this translation helpful? Give feedback.
-
A 'proper' solution from Vercel would be appreciated, even the examples in the docs lead to this problem appearing on the starter project. At the moment, my solution is to have a My current regex is as follows: export const config = {
matcher: [
"/((?!_next/static|_next/image|public/|favicon.ico|robots.txt|sitemap.xml|manifest.json).*)",
],
}; |
Beta Was this translation helpful? Give feedback.
-
Using next-intl middleware, if I don't specifically exclude all public routes, they 404. Not easy to notice something like robots.txt not loading and can obviously have a deleterious effect on SEO. But having to list all root level public files (favicon.ico|robots.txt|sitemap.xml|manifest.json|ads.txt....) is not ideal, and there are cases (like say, auto generated sitemaps) where you may not know exactly the names of the files. Here's what I'm working with currently: |
Beta Was this translation helpful? Give feedback.
-
I agree that this is super frustrating and that there should be a flag that indicates the request will be handled by public. However... That is actually entirely impossible given the current request handling architecture in Next. If we use the Middleware documentation as an example, middleware is executed early to allow redirect / rewrites (step 3). Next does not check the file system until step 5 where it attempts to handle static assets. So if anything, supporting a "public" flag would need to be in the opposite direction, something like: "prefix this route, unless it can be handled by Alternate SolutionInstead what I did was embrace the locale. Here is the structure of the solution:
Now, you can use a simple matcher like: Further, if you did have static assets that you store under Last thought, while expanding the matcher to include more files does approach a "cleaner" solution, there is a performance tradeoff as the complexity of the RegExp grows. While maybe less "clean", I think this approach has the correct balance of organization and performance. Plus assets like Hope this helps. |
Beta Was this translation helpful? Give feedback.
-
Using this regex "/((?!api|_next/static|_next/image|favicon.ico).*)" from the documentation, I find a bad UI at addresses such as "/favicon.icoo" , "robots.txtt" or others. App using minimal i18n rouring w/o external libs. Default locale is hidden by rewrite from NextResponse. |
Beta Was this translation helpful? Give feedback.
-
Just chiming in as I'm running into this as well in 2024. For the time being, adding subdirectories to export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - images directory in /public (public static images)
*/
'/((?!api|_next/static|_next/image|images).*)',
],
};
|
Beta Was this translation helpful? Give feedback.
-
I am using clerk and this is recommended in their documentation. Hope it helps export const config = {
matcher: [
// Skip Next.js internals and all static files, unless found in search params
'/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)',
// Always run for API routes
'/(api|trpc)(.*)',
],
}; |
Beta Was this translation helpful? Give feedback.
-
to clarify: is middleware regex matching next's best/only option for creating public routes? my mind is mush after spending 6 hours trying nearly every forum suggestion. edit: if so, shouldn't something like '/((?!_next/static|_next/image|favicon.ico|home(/.)?|.\.(?:svg|png|jpg|jpeg|gif|webp)$).*)' bypass auth for https://example.com/home? |
Beta Was this translation helpful? Give feedback.
-
My site doesn't even need middleware on any served files, so I can go rogue and just exclude all URLs that have dots in them: export const config = {
matcher: [
'/((?!api/|test/|.*[.]).*)',
],
} or |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Describe the feature you'd like to request
We use the same next.js app for both our marketing site and for our authenticated webapp.
We host our marketing site at
blah.com
& our app atapp.blah.com
.We have all of our "app" pages in
pages/app
. We also have root_middleware.ts
that rewrites all requests fromapp.blah.com
=>blah.com/app
.The setup is lovely, the only wrinkle is that we share public assets between these different sections of our app. We have stuff like:
public/font.css
. From theapp.blah.com
subdomain the request for these resources fail because the middleware runs and looks for the resources atpublic/app/font.css
.Describe the solution you'd like
I'd love it if any resource requests that are filled from the
/public
directory would not pass through the page middleware. They aren't page requests, so why do they go through the page middleware?Describe alternatives you've considered
If I duplicate all of my resources from
/public
=>/public/app
it avoids the 404s because once the middleware has rewritten the path it can still find the resources. This is suboptimal because it requires duplicating files.I can put all resources in a sub folder like
/public/resources
& then in the root page middleware I can avoid rewriting requests prefixed by/resources
. This is suboptimal because we have files that are expected to be in specific locations on our root path (i.e.favicon.ico
,/.well-known/appple-app-site-association
).I can handle each file/folder present in
/public
in my/pages/_middleware.ts
& avoid rewriting - this is suboptimal because it requires a lot of extra code & is error prone because if anything in that folder changes or if a root level file is added it won't be accessible.Any other options?
Beta Was this translation helpful? Give feedback.
All reactions