@modern-js/server-core
Version:
A Progressive React Framework for modern web development.
42 lines (41 loc) • 1.13 kB
JavaScript
import { MAIN_ENTRY_NAME } from "@modern-js/utils/universal/constants";
import { sortRoutes } from "../utils";
function injectRoute(route) {
return async (c, next) => {
if (route && !c.get("route")) {
c.set("route", route);
}
await next();
};
}
function getPageRoutes(routes) {
return routes.filter((route) => route.entryName).sort(sortRoutes);
}
const injectRoutePlugin = () => ({
name: "@modern-js/plugin-inject-route",
setup(api) {
return {
async prepare() {
const { middlewares, routes } = api.useAppContext();
if (!routes) {
return;
}
const pageRoutes = getPageRoutes(routes);
for (const route of pageRoutes) {
const { urlPath: originUrlPath, entryName = MAIN_ENTRY_NAME } = route;
const urlPath = originUrlPath.endsWith("/") ? `${originUrlPath}*` : `${originUrlPath}/*`;
middlewares.push({
name: "inject-route-info",
path: urlPath,
handler: injectRoute({
entryName
})
});
}
}
};
}
});
export {
injectRoutePlugin
};