nitro-nightly 3.1.0-20251026-222805-a13234bf → 3.1.0-20251026-232057-ce388de2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -20679,6 +20679,9 @@ class Router {
20679
20679
  addRoute(this.#router, route.method, route.route, route.data);
20680
20680
  }
20681
20681
  }
20682
+ hasRoutes() {
20683
+ return this.#routes.length > 0;
20684
+ }
20682
20685
  compileToString(opts) {
20683
20686
  return this.#compiled || (this.#compiled = compileRouterToString(this.#router, void 0, opts));
20684
20687
  }
@@ -10360,6 +10360,7 @@ import * as __routeRules__ from "nitro/runtime/internal/route-rules";
10360
10360
  ${nitro.options.serverEntry ? `import __serverEntry__ from ${JSON.stringify(nitro.options.serverEntry)};` : ""}
10361
10361
  import {${h3Imports.join(", ")}} from "nitro/deps/h3";
10362
10362
 
10363
+ export const hasRouteRules = ${nitro.routing.routeRules.hasRoutes() ? "true" : "false"};
10363
10364
  export const findRouteRules = ${nitro.routing.routeRules.compileToString({ serialize: serializeRouteRule, matchAll: true })}
10364
10365
 
10365
10366
  ${allHandlers.filter((h) => !h.lazy).map((h) => (
@@ -10374,10 +10375,13 @@ ${allHandlers.filter((h) => h.lazy).map(
10374
10375
  )
10375
10376
  ).join("\n")}
10376
10377
 
10378
+ export const hasRoutes = ${nitro.routing.routes.hasRoutes() ? "true" : "false"};
10377
10379
  export const findRoute = ${nitro.routing.routes.compileToString({ serialize: serializeHandler })}
10378
10380
 
10381
+ export const hasRoutedMiddleware = ${nitro.routing.routedMiddleware.hasRoutes() ? "true" : "false"};
10379
10382
  export const findRoutedMiddleware = ${nitro.routing.routedMiddleware.compileToString({ serialize: serializeHandler, matchAll: true })};
10380
10383
 
10384
+ export const hasGlobalMiddleware = ${nitro.routing.globalMiddleware.length > 0 || nitro.options.serverEntry ? "true" : "false"};
10381
10385
  export const globalMiddleware = [${nitro.routing.globalMiddleware.map((h) => h.lazy ? h._importHash : `toEventHandler(${h._importHash})`).join(",")}];
10382
10386
 
10383
10387
  ${nitro.options.serverEntry && /* js */
@@ -4,10 +4,14 @@ import { nitroAsyncContext } from "./context.mjs";
4
4
  import errorHandler from "#nitro-internal-virtual/error-handler";
5
5
  import { plugins } from "#nitro-internal-virtual/plugins";
6
6
  import {
7
+ hasRouteRules,
7
8
  findRoute,
8
9
  findRouteRules,
9
10
  globalMiddleware,
10
- findRoutedMiddleware
11
+ findRoutedMiddleware,
12
+ hasRoutedMiddleware,
13
+ hasGlobalMiddleware,
14
+ hasRoutes
11
15
  } from "#nitro-internal-virtual/routing";
12
16
  export function useNitroApp() {
13
17
  return useNitroApp.__instance__ ??= initNitroApp();
@@ -97,18 +101,33 @@ function createNitroApp() {
97
101
  }
98
102
  function createH3App(config) {
99
103
  const h3App = new H3Core(config);
100
- h3App._findRoute = (event) => findRoute(event.req.method, event.url.pathname);
104
+ if (hasRoutes) {
105
+ h3App._findRoute = (event) => findRoute(event.req.method, event.url.pathname);
106
+ }
101
107
  h3App._getMiddleware = (event, route) => {
102
- const pathname = event.url.pathname;
103
- const method = event.req.method;
104
- const { routeRules, routeRuleMiddleware } = getRouteRules(method, pathname);
105
- event.context.routeRules = routeRules;
106
- return [
107
- ...routeRuleMiddleware,
108
- ...globalMiddleware,
109
- ...findRoutedMiddleware(method, pathname).map((r) => r.data),
110
- ...route?.data?.middleware || []
111
- ].filter(Boolean);
108
+ const needsRouting = hasRouteRules || hasRoutedMiddleware;
109
+ const pathname = needsRouting ? event.url.pathname : void 0;
110
+ const method = needsRouting ? event.req.method : void 0;
111
+ const middleware = [];
112
+ if (hasRouteRules) {
113
+ const routeRules = getRouteRules(method, pathname);
114
+ event.context.routeRules = routeRules?.routeRules;
115
+ if (routeRules?.routeRuleMiddleware.length) {
116
+ middleware.push(...routeRules.routeRuleMiddleware);
117
+ }
118
+ }
119
+ if (hasGlobalMiddleware) {
120
+ middleware.push(...globalMiddleware);
121
+ }
122
+ if (hasRoutedMiddleware) {
123
+ middleware.push(
124
+ ...findRoutedMiddleware(method, pathname).map((r) => r.data)
125
+ );
126
+ }
127
+ if (route?.data?.middleware?.length) {
128
+ middleware.push(...route.data.middleware);
129
+ }
130
+ return middleware;
112
131
  };
113
132
  return h3App;
114
133
  }
@@ -3817,6 +3817,7 @@ declare class Router<T> {
3817
3817
  constructor(matchAll?: boolean);
3818
3818
  get routes(): Route<T>[];
3819
3819
  _update(routes: Route<T>[]): void;
3820
+ hasRoutes(): boolean;
3820
3821
  compileToString(opts?: RouterCompilerOptions): string;
3821
3822
  match(method: string, path: string): undefined | T;
3822
3823
  matchAll(method: string, path: string): T[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nitro-nightly",
3
- "version": "3.1.0-20251026-222805-a13234bf",
3
+ "version": "3.1.0-20251026-232057-ce388de2",
4
4
  "description": "Build and Deploy Universal JavaScript Servers",
5
5
  "homepage": "https://nitro.build",
6
6
  "repository": "nitrojs/nitro",