Home / Companies / Clerk / Blog / Post Details
Content Deep Dive

How to skip Next.js middleware for static and public files

Blog post from Clerk

Post Details
Company
Date Published
Author
Colin Sidoti
Word Count
440
Company Posts That Month
4
Language
English
Hacker News Points
-
Post removed?
No
Summary

Next.js middleware is highly versatile but by default tends to run on more requests than needed, which can include unnecessary static assets and potentially disrupt your application. A simple way to observe this is by adding a logger to your middleware, which will reveal multiple requests to static files when loading a new Next.js application. To optimize, you can configure a matcher using regular expressions to ensure middleware only runs on dynamic files by excluding those with a period in their path. This approach reduces middleware invocation to just one request, but as your application grows, you might need to adjust it to include or exclude specific paths or assets. For example, you can include certain static assets like favicon.ico by defining additional matchers or exclude specific paths such as API routes by expanding the regular expression or using conditional statements, though the latter might inefficiently use compute resources. This foundational setup allows for a more efficient use of middleware in Next.js applications.

Trends Found in this Post

No tracked trend matches for this post yet.

Use This Data

Use this post, company, and trend context to find content marketing opportunities, perform competitive analysis, or address product feature gaps via the Plushcap MCP server or the Plushcap API.