rwsdk 0.2.0-alpha.10 → 0.2.0-alpha.11

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.
@@ -72,31 +72,40 @@ export function defineRoutes(routes) {
72
72
  path = path + "/";
73
73
  }
74
74
  // Flow below; helpers are declared after the main flow for readability
75
- // 1) Global middlewares
76
- // ----------------------
77
- const globalResult = await handleGlobalMiddlewares();
78
- if (globalResult) {
79
- return globalResult;
80
- }
81
- // 2) Match route
82
- // ----------------------
83
- const match = matchRoute();
84
- if (!match) {
85
- // todo(peterp, 2025-01-28): Allow the user to define their own "not found" route.
86
- return new Response("Not Found", { status: 404 });
87
- }
88
- return await runWithRequestInfoOverrides({ params: match.params }, async () => {
89
- const { routeMiddlewares, componentHandler } = parseHandlers(match.handler);
90
- // 3) Route-specific middlewares
91
- // -----------------------------
92
- const mwHandled = await handleRouteMiddlewares(routeMiddlewares);
93
- if (mwHandled) {
94
- return mwHandled;
75
+ // 1) Global middlewares: run any middleware functions encountered (Response short-circuits, element renders)
76
+ // 2) Route matching: skip non-matching route definitions; stop at first match
77
+ // 3) Route-specific middlewares: run middlewares attached to the matched route
78
+ // 4) Final component: render the matched route's final component (layouts apply only here)
79
+ for (const route of flattenedRoutes) {
80
+ // 1) Global middlewares (encountered before a match)
81
+ if (typeof route === "function") {
82
+ const result = await route(getRequestInfo());
83
+ const handled = await handleMiddlewareResult(result);
84
+ if (handled) {
85
+ return handled;
86
+ }
87
+ continue;
95
88
  }
96
- // 4) Final component (always last item)
97
- // -------------------------------------
98
- return await handleRouteComponent(componentHandler, match.layouts || []);
99
- });
89
+ // 2) Route matching (skip if not matched)
90
+ const params = matchPath(route.path, path);
91
+ if (!params) {
92
+ continue;
93
+ }
94
+ // Found a match: 3) route middlewares, then 4) final component, then stop
95
+ return await runWithRequestInfoOverrides({ params }, async () => {
96
+ const { routeMiddlewares, componentHandler } = parseHandlers(route.handler);
97
+ // 3) Route-specific middlewares
98
+ const mwHandled = await handleRouteMiddlewares(routeMiddlewares);
99
+ if (mwHandled) {
100
+ return mwHandled;
101
+ }
102
+ // 4) Final component (always last item)
103
+ return await handleRouteComponent(componentHandler, route.layouts || []);
104
+ });
105
+ }
106
+ // No route matched and no middleware handled the request
107
+ // todo(peterp, 2025-01-28): Allow the user to define their own "not found" route.
108
+ return new Response("Not Found", { status: 404 });
100
109
  // --- Helpers ---
101
110
  function parseHandlers(handler) {
102
111
  const handlers = Array.isArray(handler) ? handler : [handler];
@@ -121,29 +130,8 @@ export function defineRoutes(routes) {
121
130
  }
122
131
  return undefined;
123
132
  }
124
- async function handleGlobalMiddlewares() {
125
- for (const route of flattenedRoutes) {
126
- if (typeof route !== "function") {
127
- continue;
128
- }
129
- const result = await route(getRequestInfo());
130
- const handled = await handleMiddlewareResult(result);
131
- if (handled)
132
- return handled;
133
- }
134
- return undefined;
135
- }
136
- function matchRoute() {
137
- for (const route of flattenedRoutes) {
138
- if (typeof route === "function")
139
- continue;
140
- const params = matchPath(route.path, path);
141
- if (params) {
142
- return { params, handler: route.handler, layouts: route.layouts };
143
- }
144
- }
145
- return null;
146
- }
133
+ // Note: We no longer have separate global pass or match-only pass;
134
+ // the outer single pass above handles both behaviors correctly.
147
135
  async function handleRouteMiddlewares(mws) {
148
136
  for (const mw of mws) {
149
137
  const result = await mw(getRequestInfo());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rwsdk",
3
- "version": "0.2.0-alpha.10",
3
+ "version": "0.2.0-alpha.11",
4
4
  "description": "Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime",
5
5
  "type": "module",
6
6
  "bin": {