@luwiostack/router 0.3.0 → 0.4.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 +23 -17
- package/dist/index.cjs +39 -36
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +29 -32
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +29 -32
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +40 -36
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -297,6 +297,7 @@ TanStack route option is accepted and forwarded untouched**.
|
|
|
297
297
|
| `validateSearch` | `(search) => T` | Parse/validate the query string into typed `search`. |
|
|
298
298
|
| `title` / `description` | `(ctx) => string` | **Required** for URL routes (SEO). The router emits them (plus canonical, hreflang, og) into the head. `ctx.context.locale` is the active locale. See below. |
|
|
299
299
|
| `head` | `(ctx) => RouteHead` | **Optional** extra head tags beyond the SEO essentials the router already emits. See below. |
|
|
300
|
+
| `onPageView` | `(ctx) => void` | **Optional.** Runs for each page view of this route (not inherited; URL routes only). See [Page views](#page-views). |
|
|
300
301
|
| `component` | component | The page (or a layout shell around `<Outlet />`). |
|
|
301
302
|
| `pendingComponent` | component | Shown while the loader is pending. |
|
|
302
303
|
| `errorComponent` | component | Shown when the route (or a child) throws. |
|
|
@@ -848,34 +849,39 @@ and every generated link stay unprefixed while you're in the default locale.
|
|
|
848
849
|
|
|
849
850
|
## Page views
|
|
850
851
|
|
|
851
|
-
`onPageView
|
|
852
|
-
|
|
852
|
+
A route's `onPageView` runs for each page view of that route: once for the first load, then once per
|
|
853
|
+
navigation that resolves to a different path or query string. Track it there, next to `title()` and
|
|
854
|
+
`head()` — the router knows nothing about analytics.
|
|
853
855
|
|
|
854
856
|
```ts
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
// { routeId: 'about', locale, title: 'Over ons', path: '/nl-BE/over-ons',
|
|
861
|
-
|
|
857
|
+
const about = createRoute({
|
|
858
|
+
id: 'about',
|
|
859
|
+
parent: 'shell',
|
|
860
|
+
title: ({ context }) => context.t('about.title'),
|
|
861
|
+
description: ({ context }) => context.t('about.body'),
|
|
862
|
+
// { context: { routeId: 'about', locale, … }, title: 'Over ons', path: '/nl-BE/over-ons',
|
|
863
|
+
// href: '/nl-BE/over-ons?ref=mail', params: {}, search: { ref: 'mail' } }
|
|
864
|
+
onPageView: ({ context, title }) =>
|
|
865
|
+
context.analytics.pageView({ title, route_id: context.routeId }),
|
|
862
866
|
})
|
|
863
867
|
```
|
|
864
868
|
|
|
869
|
+
Put what it needs on context: an ancestor's `beforeLoad` (`return { analytics }`) or the router's
|
|
870
|
+
own context (`createRouter(registry, { …, router: { context: { analytics } } })`), typed by augmenting
|
|
871
|
+
`RouteContext`.
|
|
872
|
+
|
|
873
|
+
- **Only the page's own.** It runs for the route that is the page and isn't inherited: a child's page
|
|
874
|
+
view never runs its parent's `onPageView`. A layout route can't set one (`createRouter` throws).
|
|
865
875
|
- **One per page.** A hash-only change isn't a new page, and neither is a re-resolve of the same URL
|
|
866
876
|
(`router.invalidate()`); a new query string is.
|
|
867
|
-
- **Route id, not path.** `routeId` is the same in every locale, so `/en-BE/about` and
|
|
877
|
+
- **Route id, not path.** `context.routeId` is the same in every locale, so `/en-BE/about` and
|
|
868
878
|
`/nl-BE/over-ons` report as one page in two locales.
|
|
869
879
|
- **The route's own title.** `title` is the route's resolved `title()`, the value the router puts in
|
|
870
880
|
`<title>`. Read it here rather than from `document.title`, which `<HeadContent>` only updates after
|
|
871
881
|
render, so on a navigation it can still be the previous page's.
|
|
872
882
|
- **Synthetic routes are skipped.** The 404 catch-all, a forbidden guard and a legacy redirect aren't
|
|
873
|
-
pages: the page they redirect to
|
|
874
|
-
- **
|
|
875
|
-
own backend).
|
|
876
|
-
|
|
877
|
-
Subscribe before the router first loads (before rendering `<RouterProvider>`) to see the first page.
|
|
878
|
-
It returns the unsubscribe.
|
|
883
|
+
pages: the page they redirect to runs its `onPageView` instead.
|
|
884
|
+
- **Browser only.** It never runs during server rendering; the client's first load reports the page.
|
|
879
885
|
|
|
880
886
|
## API
|
|
881
887
|
|
|
@@ -892,7 +898,7 @@ It returns the unsubscribe.
|
|
|
892
898
|
| `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). |
|
|
893
899
|
| `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). |
|
|
894
900
|
| `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
|
-
| `
|
|
901
|
+
| `RoutePageViewCtx` | What a route's `onPageView` receives: `{ context, params, search, title, path, href }`. See [Page views](#page-views). |
|
|
896
902
|
| `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
903
|
| `useRouteStaticData(select?)` | The active route's `staticData`, merged down the matched tree (deepest wins). See [Route flags](#route-flags-staticdata). |
|
|
898
904
|
| `RouterProvider` / `Outlet` | Re-exported from the bundled TanStack Router. Mount the tree / render children. |
|
package/dist/index.cjs
CHANGED
|
@@ -7058,6 +7058,41 @@ function getRouteTag(route) {
|
|
|
7058
7058
|
return route ? tags.get(route) : void 0;
|
|
7059
7059
|
}
|
|
7060
7060
|
//#endregion
|
|
7061
|
+
//#region src/page-view.ts
|
|
7062
|
+
/**
|
|
7063
|
+
* Call the leaf route's own `onPageView` for each page the visitor lands on: once for the first load,
|
|
7064
|
+
* then once per navigation that resolves to a different path or query string. A hash-only change
|
|
7065
|
+
* isn't a new page, and neither is a re-resolve of the same URL. Synthetic routes (the 404 catch-all,
|
|
7066
|
+
* a forbidden guard, a legacy redirect) are skipped; the page they redirect to is reported instead.
|
|
7067
|
+
* The title is the route's resolved `title()`, ready before `<HeadContent>` updates `document.title`.
|
|
7068
|
+
*
|
|
7069
|
+
* Subscribed by `createRouter` itself, before the router first loads, so the first page counts.
|
|
7070
|
+
*/
|
|
7071
|
+
function reportPageViews(router, registry) {
|
|
7072
|
+
let lastKey;
|
|
7073
|
+
router.subscribe("onResolved", ({ toLocation }) => {
|
|
7074
|
+
const key = `${toLocation.pathname}?${toLocation.searchStr}`;
|
|
7075
|
+
if (key === lastKey) return;
|
|
7076
|
+
const leaf = router.state.matches.at(-1);
|
|
7077
|
+
const tag = leaf ? getRouteTag(router.routesById[leaf.routeId]) : void 0;
|
|
7078
|
+
if (!leaf || !tag) return;
|
|
7079
|
+
lastKey = key;
|
|
7080
|
+
const onPageView = registry.get(tag.id)?.options.onPageView;
|
|
7081
|
+
onPageView?.({
|
|
7082
|
+
context: {
|
|
7083
|
+
routeId: tag.id,
|
|
7084
|
+
locale: tag.locale,
|
|
7085
|
+
...leaf.context ?? {}
|
|
7086
|
+
},
|
|
7087
|
+
params: { ...leaf.params ?? {} },
|
|
7088
|
+
search: { ...leaf.search ?? {} },
|
|
7089
|
+
title: (leaf.meta ?? []).find((m) => m?.title !== void 0)?.title ?? "",
|
|
7090
|
+
path: toLocation.pathname,
|
|
7091
|
+
href: toLocation.href
|
|
7092
|
+
});
|
|
7093
|
+
});
|
|
7094
|
+
}
|
|
7095
|
+
//#endregion
|
|
7061
7096
|
//#region src/use-router.ts
|
|
7062
7097
|
/** The query param the referrer travels in. */
|
|
7063
7098
|
const REFERRER = "referrer";
|
|
@@ -7438,6 +7473,7 @@ function createRouter(registry, config) {
|
|
|
7438
7473
|
}
|
|
7439
7474
|
const { kept: builders, tombstones } = planRoutes(registry.all(), config);
|
|
7440
7475
|
for (const b of builders) {
|
|
7476
|
+
if (b.layout && b.options.onPageView !== void 0) throw new Error(`Route "${b.id}" is a layout route — onPageView is only for URL routes (it isn't inherited).`);
|
|
7441
7477
|
if (b.layout) continue;
|
|
7442
7478
|
if (b.aliasCount === 0) throw new Error(`Route "${b.id}" has no alias — call .alias(locale, slug) at least once (only layout routes may omit it).`);
|
|
7443
7479
|
if (typeof b.options.title !== "function") throw new Error(`Route "${b.id}" has no title() — it's required for SEO (layout routes exempt).`);
|
|
@@ -7478,7 +7514,7 @@ function createRouter(registry, config) {
|
|
|
7478
7514
|
const build = (parentId, parent) => {
|
|
7479
7515
|
const routes = (byParent.get(parentId) ?? []).filter((b) => b.layout || b.hasAlias(locale)).map((b) => {
|
|
7480
7516
|
const head = withHead(b, locale, apiCtx);
|
|
7481
|
-
const { title: _t, description: _d, head: _h, loader: userLoader, ...tanstackOptions } = b.options;
|
|
7517
|
+
const { title: _t, description: _d, head: _h, onPageView: _pv, loader: userLoader, ...tanstackOptions } = b.options;
|
|
7482
7518
|
const route = buildRoute({
|
|
7483
7519
|
...tanstackOptions,
|
|
7484
7520
|
getParentRoute: () => parent,
|
|
@@ -7556,6 +7592,7 @@ function createRouter(registry, config) {
|
|
|
7556
7592
|
config,
|
|
7557
7593
|
mounted
|
|
7558
7594
|
});
|
|
7595
|
+
if (typeof document !== "undefined" && builders.some((b) => b.options.onPageView)) reportPageViews(router, registry);
|
|
7559
7596
|
return router;
|
|
7560
7597
|
}
|
|
7561
7598
|
/** Group builders by their parent id for tree assembly. */
|
|
@@ -7849,40 +7886,6 @@ function lazy(importer, exportName) {
|
|
|
7849
7886
|
return lazyRouteComponent(importer, exportName);
|
|
7850
7887
|
}
|
|
7851
7888
|
//#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
|
|
7886
7889
|
//#region src/route-builder.ts
|
|
7887
7890
|
/**
|
|
7888
7891
|
* The route-config keys we recognise: this package's own three, the SEO/head keys the router adds,
|
|
@@ -7899,6 +7902,7 @@ const KNOWN_KEYS = /* @__PURE__ */ new Set([
|
|
|
7899
7902
|
"title",
|
|
7900
7903
|
"description",
|
|
7901
7904
|
"head",
|
|
7905
|
+
"onPageView",
|
|
7902
7906
|
"beforeLoad",
|
|
7903
7907
|
"loader",
|
|
7904
7908
|
"loaderDeps",
|
|
@@ -8208,7 +8212,6 @@ exports.createRoute = createRoute;
|
|
|
8208
8212
|
exports.createRouter = createRouter;
|
|
8209
8213
|
exports.defer = defer;
|
|
8210
8214
|
exports.lazy = lazy;
|
|
8211
|
-
exports.onPageView = onPageView;
|
|
8212
8215
|
exports.registerModules = registerModules;
|
|
8213
8216
|
exports.routePath = routePath;
|
|
8214
8217
|
exports.routeRegistry = routeRegistry;
|