Skip to content

[Feature?]: File-based routing for middlewares #1860

Closed as not planned
Closed as not planned

Description

@Sleepful

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Summary 💡

Middlewares that run only on a given route, with file-based routing. The file rules would be similar to those of a layout that works on all routes of a given directory.

A good example can be seen in the Fresh framework, this is a good reference of the feature:

https://fresh.deno.dev/docs/concepts/middleware

Examples 🌈

No response

Motivation 🔦

No response

Activity

  1. Sleepful commented on Mar 20, 2025

    @Sleepful
    Author

    Tangentially related issue: #1811

  2. brenelz commented on Sep 18, 2026

    @brenelz
    Contributor

    So can this be closed as resolved. The other issue is closed

  3. lxsmnsyc commented on Oct 14, 2026

    @lxsmnsyc
    Member

    This is a separate request from #1811. That issue was closed after it was answered with "check the request path in your middleware", not by adding directory-scoped middleware. I'm closing this one as not planned, mainly because of how it would interact with server functions.

    Server functions aren't sent to the page's path. On both 1.x and 2.x, every server function call is a POST /_server, whichever page it was called from. A middleware scoped to routes/admin/ would run for /admin/* page requests, but not for server functions called from those pages. An auth check placed there would look like it protects the admin data while the server functions behind it stay reachable. One middleware module that checks the path sees every request, including /_server, so it doesn't have that gap.

    Scoping middleware to paths already works on both lines.

    2.x, with h3 middleware:

    // src/middleware.ts
    import type { Middleware } from "h3";
    import { createMiddleware } from "@solidjs/start/middleware";
    
    const forPath =
      (prefix: string, middleware: Middleware): Middleware =>
      (event, next) =>
        event.url.pathname.startsWith(prefix) ? middleware(event, next) : next();
    
    export default createMiddleware([
      forPath("/admin", requireAdmin),
      forPath("/api", rateLimit),
    ]);

    1.x, with onRequest:

    export default createMiddleware({
      onRequest: [
        event => {
          const { pathname } = new URL(event.request.url);
          if (pathname.startsWith("/admin")) return requireAdmin(event);
        },
      ],
    });

    I checked both on the respective lines: the path-scoped middleware ran only for matching paths, and could set headers or block a request (403). A server function called from an /admin page arrived as POST /_server, so protect server functions with checks inside the function, or with a middleware that also handles /_server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementfile routes

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions