@luwiostack/router 0.2.0 → 0.3.0
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/README.md +67 -0
- package/dist/index.cjs +43 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +66 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +66 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +42 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -305,6 +305,7 @@ TanStack route option is accepted and forwarded untouched**.
|
|
|
305
305
|
| `shouldReload` | `boolean \| (ctx) => boolean` | Whether the loader re-runs on navigation. |
|
|
306
306
|
| `caseSensitive` | `boolean` | Case-sensitive path matching. |
|
|
307
307
|
| `wrapInSuspense` | `boolean` | Wrap the component in `<Suspense>`. |
|
|
308
|
+
| `staticData` | `RouteStaticData` | Static flags for this route and its children, read with `useRouteStaticData()`. See [Route flags](#route-flags-staticdata). |
|
|
308
309
|
| _…anything else_ | | Forwarded to TanStack's `createRoute` untouched. |
|
|
309
310
|
|
|
310
311
|
\* required. `path` and `getParentRoute` are **not** accepted — the URL segment comes from
|
|
@@ -485,6 +486,38 @@ Resulting tree — `auth` and `app` never appear in the URL, but run on every re
|
|
|
485
486
|
| `en-BE` | `/en-BE/account` |
|
|
486
487
|
| `fr-BE` | `/fr-BE/compte` |
|
|
487
488
|
|
|
489
|
+
## Route flags (`staticData`)
|
|
490
|
+
|
|
491
|
+
Some app chrome depends on the page: no cookie banner over the privacy policy, a full-bleed layout,
|
|
492
|
+
a hidden footer. Put that decision on the route itself with `staticData`, and read it back anywhere
|
|
493
|
+
under the router with `useRouteStaticData()` — also in a shell or banner outside the page component.
|
|
494
|
+
No path checks needed, so it keeps working across locales and translated aliases.
|
|
495
|
+
|
|
496
|
+
```tsx
|
|
497
|
+
// Type your flags once.
|
|
498
|
+
declare module '@luwiostack/router' {
|
|
499
|
+
interface RouteStaticData {
|
|
500
|
+
hideCookieBanner?: boolean
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
// privacy.route.tsx
|
|
505
|
+
export default createRoute({
|
|
506
|
+
id: 'privacy',
|
|
507
|
+
staticData: { hideCookieBanner: true },
|
|
508
|
+
title, description, component: Privacy,
|
|
509
|
+
})
|
|
510
|
+
|
|
511
|
+
// CookieBanner.tsx (rendered by the app shell, on every page)
|
|
512
|
+
const hideBanner = useRouteStaticData((d) => d.hideCookieBanner === true)
|
|
513
|
+
```
|
|
514
|
+
|
|
515
|
+
The flags **merge down the matched tree**: a layout route's flags apply to every child, and the
|
|
516
|
+
deepest route that sets a key wins — put `hideCookieBanner` on a `legal` layout and every legal page
|
|
517
|
+
gets it. Without a `select`, `useRouteStaticData()` returns the whole merged object (`{}` when no
|
|
518
|
+
route sets any). The flags are static: anything that depends on params or loaded data belongs in
|
|
519
|
+
`beforeLoad` / `loader` instead.
|
|
520
|
+
|
|
488
521
|
## Config-driven availability
|
|
489
522
|
|
|
490
523
|
`createRouter`'s config decides what actually mounts, so one build can serve different route/locale
|
|
@@ -813,6 +846,37 @@ On an unprefixed route the active locale is the default locale — `useRouter().
|
|
|
813
846
|
the default locale: `router.href({ id: 'home' })` returns `/home`, not `/nl-BE/home` — so `navigate`
|
|
814
847
|
and every generated link stay unprefixed while you're in the default locale.
|
|
815
848
|
|
|
849
|
+
## Page views
|
|
850
|
+
|
|
851
|
+
`onPageView(router, listener)` calls `listener` with each page the visitor lands on: once for the
|
|
852
|
+
first load, then once per navigation that resolves to a different path or query string.
|
|
853
|
+
|
|
854
|
+
```ts
|
|
855
|
+
import { createRouter, onPageView } from '@luwiostack/router'
|
|
856
|
+
|
|
857
|
+
const router = createRouter(routeRegistry, config)
|
|
858
|
+
|
|
859
|
+
onPageView(router, (page) => {
|
|
860
|
+
// { routeId: 'about', locale, title: 'Over ons', path: '/nl-BE/over-ons', href: '/nl-BE/over-ons?ref=mail', params: {} }
|
|
861
|
+
analytics.pageView({ title: page.title, route_id: page.routeId })
|
|
862
|
+
})
|
|
863
|
+
```
|
|
864
|
+
|
|
865
|
+
- **One per page.** A hash-only change isn't a new page, and neither is a re-resolve of the same URL
|
|
866
|
+
(`router.invalidate()`); a new query string is.
|
|
867
|
+
- **Route id, not path.** `routeId` is the same in every locale, so `/en-BE/about` and
|
|
868
|
+
`/nl-BE/over-ons` report as one page in two locales.
|
|
869
|
+
- **The route's own title.** `title` is the route's resolved `title()`, the value the router puts in
|
|
870
|
+
`<title>`. Read it here rather than from `document.title`, which `<HeadContent>` only updates after
|
|
871
|
+
render, so on a navigation it can still be the previous page's.
|
|
872
|
+
- **Synthetic routes are skipped.** The 404 catch-all, a forbidden guard and a legacy redirect aren't
|
|
873
|
+
pages: the page they redirect to is reported instead.
|
|
874
|
+
- **No analytics dependency.** Hand the page to whatever tracks it (`@luwiostack/analytics`, GTM, your
|
|
875
|
+
own backend).
|
|
876
|
+
|
|
877
|
+
Subscribe before the router first loads (before rendering `<RouterProvider>`) to see the first page.
|
|
878
|
+
It returns the unsubscribe.
|
|
879
|
+
|
|
816
880
|
## API
|
|
817
881
|
|
|
818
882
|
| Export | Description |
|
|
@@ -828,7 +892,9 @@ and every generated link stay unprefixed while you're in the default locale.
|
|
|
828
892
|
| `referrer: true` | On `href` / `navigate` / `redirect`: add the current page (in a guard, the page being loaded) as `?referrer=`. `forbidden()` adds it by default; `referrer: false` leaves it out. See [Back to the referrer](#back-to-the-referrer). |
|
|
829
893
|
| `router.canGoToReferrer({ from? })` / `router.goToReferrer({ from?, replace? })` | Like `canGoBack()` / `goBack()`, but for `?referrer=` instead of history: whether there's a safe page to go back to, and go there (a no-op when there isn't). |
|
|
830
894
|
| `router.referrer({ from? })` | The referrer as `{ id, params, query?, hash?, locale? }` in the active locale, or `undefined` unless it's a page on this site the router serves (and not the current route or a status page). `from` checks a value that arrived another way, like an OAuth `state`. See [Through an OAuth round trip](#through-an-oauth-round-trip). |
|
|
895
|
+
| `onPageView(router, listener)` | Call `listener` with each page the visitor lands on: `{ routeId, locale, title, path, href, params }`. See [Page views](#page-views). |
|
|
831
896
|
| `useRouteFailure()` | `{ status, error? } \| undefined` — the failure that redirected to the current `statusPages` page. See [Not found, forbidden & error](#not-found-forbidden--error-404--403--5xx). |
|
|
897
|
+
| `useRouteStaticData(select?)` | The active route's `staticData`, merged down the matched tree (deepest wins). See [Route flags](#route-flags-staticdata). |
|
|
832
898
|
| `RouterProvider` / `Outlet` | Re-exported from the bundled TanStack Router. Mount the tree / render children. |
|
|
833
899
|
| `HeadContent` / `Scripts` | Re-exported from the bundled TanStack Router. Render a route's `head()` tags / body scripts. |
|
|
834
900
|
| `Await` / `useAwaited` / `defer` | Re-exported from the bundled TanStack Router. Stream deferred loader data (skeletons). |
|
|
@@ -836,6 +902,7 @@ and every generated link stay unprefixed while you're in the default locale.
|
|
|
836
902
|
| `RouteConfig` / `RouterConfig` | Config types. `createRoute<TParams, TSearch>` types the handler `ctx`. |
|
|
837
903
|
| `StatusPages` | The `statusPages` config type — `Record<number, string> & { default: string }`. |
|
|
838
904
|
| `RouteRegister` | Augment to map `id` → `{ params?; search? }` for typed `href` / `navigate` (see below). |
|
|
905
|
+
| `RouteStaticData` | Augment to type the keys of `staticData` / `useRouteStaticData()`. |
|
|
839
906
|
|
|
840
907
|
## Type safety
|
|
841
908
|
|
package/dist/index.cjs
CHANGED
|
@@ -7849,6 +7849,40 @@ function lazy(importer, exportName) {
|
|
|
7849
7849
|
return lazyRouteComponent(importer, exportName);
|
|
7850
7850
|
}
|
|
7851
7851
|
//#endregion
|
|
7852
|
+
//#region src/page-view.ts
|
|
7853
|
+
/**
|
|
7854
|
+
* Call `listener` with each page the visitor lands on: once for the first load, then once per
|
|
7855
|
+
* navigation that resolves to a different path or query string. A hash-only change isn't a new page,
|
|
7856
|
+
* and neither is a re-resolve of the same URL. Synthetic routes (the 404 catch-all, a forbidden
|
|
7857
|
+
* guard, a legacy redirect) are skipped; the page they redirect to is reported instead. The title is the route's
|
|
7858
|
+
* resolved `title()`, which is ready before `<HeadContent>` updates `document.title`.
|
|
7859
|
+
*
|
|
7860
|
+
* Knows nothing about analytics: hand the page to whatever tracks it, e.g.
|
|
7861
|
+
* `onPageView(router, (page) => analytics.pageView({ title: page.title, route_id: page.routeId }))`.
|
|
7862
|
+
*
|
|
7863
|
+
* Subscribe before the router first loads (before rendering `<RouterProvider>`) to see the first
|
|
7864
|
+
* page. Returns the unsubscribe.
|
|
7865
|
+
*/
|
|
7866
|
+
function onPageView(router, listener) {
|
|
7867
|
+
let lastKey;
|
|
7868
|
+
return router.subscribe("onResolved", ({ toLocation }) => {
|
|
7869
|
+
const key = `${toLocation.pathname}?${toLocation.searchStr}`;
|
|
7870
|
+
if (key === lastKey) return;
|
|
7871
|
+
const leaf = router.state.matches.at(-1);
|
|
7872
|
+
const tag = leaf ? getRouteTag(router.routesById[leaf.routeId]) : void 0;
|
|
7873
|
+
if (!leaf || !tag) return;
|
|
7874
|
+
lastKey = key;
|
|
7875
|
+
listener({
|
|
7876
|
+
routeId: tag.id,
|
|
7877
|
+
locale: tag.locale,
|
|
7878
|
+
title: (leaf.meta ?? []).find((m) => m?.title !== void 0)?.title,
|
|
7879
|
+
path: toLocation.pathname,
|
|
7880
|
+
href: toLocation.href,
|
|
7881
|
+
params: { ...leaf.params ?? {} }
|
|
7882
|
+
});
|
|
7883
|
+
});
|
|
7884
|
+
}
|
|
7885
|
+
//#endregion
|
|
7852
7886
|
//#region src/route-builder.ts
|
|
7853
7887
|
/**
|
|
7854
7888
|
* The route-config keys we recognise: this package's own three, the SEO/head keys the router adds,
|
|
@@ -7886,6 +7920,7 @@ const KNOWN_KEYS = /* @__PURE__ */ new Set([
|
|
|
7886
7920
|
"preloadGcTime",
|
|
7887
7921
|
"caseSensitive",
|
|
7888
7922
|
"wrapInSuspense",
|
|
7923
|
+
"staticData",
|
|
7889
7924
|
"preload",
|
|
7890
7925
|
"ssr",
|
|
7891
7926
|
"pendingMs",
|
|
@@ -8132,6 +8167,12 @@ function useRouteLoaderData() {
|
|
|
8132
8167
|
function useRouteContext() {
|
|
8133
8168
|
return useRouteContext$1({ strict: false });
|
|
8134
8169
|
}
|
|
8170
|
+
function useRouteStaticData(select) {
|
|
8171
|
+
return useRouterState({ select: (s) => {
|
|
8172
|
+
const merged = Object.assign({}, ...s.matches.map((m) => m.staticData));
|
|
8173
|
+
return select ? select(merged) : merged;
|
|
8174
|
+
} });
|
|
8175
|
+
}
|
|
8135
8176
|
/**
|
|
8136
8177
|
* The failure that redirected here, for a page configured in `createRouter`'s `statusPages` (404,
|
|
8137
8178
|
* 403, a dedicated status like 503, or the `default` catch-all). The router attaches this as history
|
|
@@ -8167,6 +8208,7 @@ exports.createRoute = createRoute;
|
|
|
8167
8208
|
exports.createRouter = createRouter;
|
|
8168
8209
|
exports.defer = defer;
|
|
8169
8210
|
exports.lazy = lazy;
|
|
8211
|
+
exports.onPageView = onPageView;
|
|
8170
8212
|
exports.registerModules = registerModules;
|
|
8171
8213
|
exports.routePath = routePath;
|
|
8172
8214
|
exports.routeRegistry = routeRegistry;
|
|
@@ -8179,6 +8221,7 @@ exports.useRouteLoaderData = useRouteLoaderData;
|
|
|
8179
8221
|
exports.useRouteLocale = useRouteLocale;
|
|
8180
8222
|
exports.useRouteParams = useRouteParams;
|
|
8181
8223
|
exports.useRouteQuery = useRouteQuery;
|
|
8224
|
+
exports.useRouteStaticData = useRouteStaticData;
|
|
8182
8225
|
exports.useRouter = useRouter;
|
|
8183
8226
|
|
|
8184
8227
|
//# sourceMappingURL=index.cjs.map
|