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.
- package/dist/runtime/lib/router.js +35 -47
- package/package.json +1 -1
|
@@ -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
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
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
|
-
//
|
|
97
|
-
|
|
98
|
-
|
|
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
|
-
|
|
125
|
-
|
|
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());
|