Unverified Commit 3468f10c authored by Lee Robinson's avatar Lee Robinson Committed by GitHub

docs: clarify middleware use cases (#77438)

parent 948d59fa
...@@ -11,21 +11,16 @@ Middleware runs before cached content and routes are matched. See [Matching Path ...@@ -11,21 +11,16 @@ Middleware runs before cached content and routes are matched. See [Matching Path
## Use Cases ## Use Cases
Integrating Middleware into your application can lead to significant improvements in performance, security, and user experience. Some common scenarios where Middleware is particularly effective include: Some common scenarios where Middleware is effective include:
- Authentication and Authorization: Ensure user identity and check session cookies before granting access to specific pages or API routes. - Quick redirects after reading parts of the incoming request
- Server-Side Redirects: Redirect users at the server level based on certain conditions (e.g., locale, user role). - Rewriting to different pages based on A/B tests or experiments
- Path Rewriting: Support A/B testing, feature rollouts, or legacy paths by dynamically rewriting paths to API routes or pages based on request properties. - Modifying headers for all pages or a subet of pages
- Bot Detection: Protect your resources by detecting and blocking bot traffic.
- Logging and Analytics: Capture and analyze request data for insights before processing by the page or API.
- Feature Flagging: Enable or disable features dynamically for seamless feature rollouts or testing.
Recognizing situations where middleware may not be the optimal approach is just as crucial. Here are some scenarios to be mindful of: Middleware is _not_ a good fit for:
- Complex Data Fetching and Manipulation: Middleware is not designed for direct data fetching or manipulation, this should be done within Route Handlers or server-side utilities instead. - Slow data fetching
- Heavy Computational Tasks: Middleware should be lightweight and respond quickly or it can cause delays in page load. Heavy computational tasks or long-running processes should be done within dedicated Route Handlers. - Session management
- Extensive Session Management: While Middleware can manage basic session tasks, extensive session management should be managed by dedicated authentication services or within Route Handlers.
- Direct Database Operations: Performing direct database operations within Middleware is not recommended. Database interactions should be done within Route Handlers or server-side utilities.
## Convention ## Convention
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment