@vaadin/hilla-file-router
Version:
Hilla file-based router
56 lines • 1.78 kB
JavaScript
import { transformTree } from "../../shared/transformTree.js";
import { getHandleFlag, RouteHandleFlag } from "./utils.js";
/**
* Processes a tree of route objects and splits them into two separate trees:
* - routes marked with {@link ViewConfiguration.skipLayout} flag that require
* no layout rendering,
* - regular routes that should render with their layouts.
*
* For routes with the {@link ViewConfiguration.skipLayout} flag, the layout
* element is removed. The resulting routes are also marked with
* {@link ViewConfiguration.ignoreFallback} flag to prevent rendering
* server-side layouts for them.
*
* @param originalRoutes - The current route tree to process.
*
* @returns A new array containing reorganized routes with skipped routes first
* (wrapped in an ignore-fallback container) followed by regular routes.
*/
export default function mergeSkipLayouts(originalRoutes) {
if (!originalRoutes) {
return originalRoutes;
}
const result = transformTree(originalRoutes, null, (routes, next) => routes.reduce((lists, route) => {
if (getHandleFlag(route, RouteHandleFlag.SKIP_LAYOUTS)) {
lists.skipped.push(route);
return lists;
}
if (!route.children?.length) {
lists.regular.push(route);
return lists;
}
const { skipped, regular } = next(route.children ?? []);
if (skipped.length > 0) {
const { element,...rest } = route;
lists.skipped.push({
...rest,
children: skipped
});
}
if (regular.length > 0) {
lists.regular.push({
...route,
children: regular
});
}
return lists;
}, {
skipped: [],
regular: []
}));
return [...result.skipped.length ? [{
children: result.skipped,
handle: { [RouteHandleFlag.IGNORE_FALLBACK]: true }
}] : [], ...result.regular];
}
//# sourceMappingURL=./mergeSkipLayout.js.map