@luwiostack/router 0.1.1 → 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 +193 -7
- package/dist/index.cjs +129 -18
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +132 -3
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +132 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +128 -19
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
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
|
|
@@ -521,8 +554,8 @@ page), not requirements: omit either and it falls back to `default` too.
|
|
|
521
554
|
**matched** route (e.g. a loader that couldn't find its resource) — every kind of "not found" ends
|
|
522
555
|
up on the same real, translated, linkable 404 page. Define it as a normal route.
|
|
523
556
|
- **403** — a guard throws `context.router.forbidden()`, a locale-aware redirect to
|
|
524
|
-
`statusPages[403]`. The target lives in config, so the guard never repeats it
|
|
525
|
-
|
|
557
|
+
`statusPages[403]`. The target lives in config, so the guard never repeats it. It adds the
|
|
558
|
+
requested page as `?referrer=`, so sign-in can [send the user back](#back-to-the-referrer).
|
|
526
559
|
- **Any other status** (`503`, `429`, …) — a dedicated page for a `beforeLoad`/`loader` failure whose
|
|
527
560
|
thrown value carries that `status` (an `HttpError` from `@luwiostack/http`, or anything else shaped
|
|
528
561
|
like one: `{ status: 503, … }`).
|
|
@@ -573,11 +606,9 @@ createRoute({
|
|
|
573
606
|
.alias(Locale.new({ languageOrLocale: 'en-BE' }), '404')
|
|
574
607
|
.alias(Locale.new({ languageOrLocale: 'nl-BE' }), '404')
|
|
575
608
|
|
|
576
|
-
// a guard — 403 → sign-in,
|
|
577
|
-
beforeLoad: ({ context
|
|
578
|
-
if (!getSession())
|
|
579
|
-
throw context.router.forbidden({ query: { redirect: (location as { href: string }).href } })
|
|
580
|
-
}
|
|
609
|
+
// a guard — 403 → sign-in, adding ?referrer= so sign-in can send the user back
|
|
610
|
+
beforeLoad: ({ context }) => {
|
|
611
|
+
if (!getSession()) throw context.router.forbidden()
|
|
581
612
|
}
|
|
582
613
|
|
|
583
614
|
// the 500 route — just like the 404 route, a normal aliased page
|
|
@@ -628,6 +659,124 @@ has no dedicated entry) to be a **registered** route, else the build throws. `ro
|
|
|
628
659
|
reports an excluded route as **not** mounted — a 403 guard is not
|
|
629
660
|
something you link to.
|
|
630
661
|
|
|
662
|
+
### Back to the referrer
|
|
663
|
+
|
|
664
|
+
The typical case: a signed-out visitor opens `/nl-BE/bestellingen/42`, a guard sends them to
|
|
665
|
+
sign-in, and after signing in they should land on order 42, not on a generic start page. That takes
|
|
666
|
+
two steps:
|
|
667
|
+
|
|
668
|
+
1. **Add the referrer** where you send them away. `forbidden()` does this for you; anywhere else,
|
|
669
|
+
pass `referrer: true`.
|
|
670
|
+
2. **Go to the referrer** when they're done: `canGoToReferrer() ? goToReferrer() : …`
|
|
671
|
+
|
|
672
|
+
`forbidden()` adds the page being loaded, which in a guard is the page the user was trying to reach.
|
|
673
|
+
For any other detour, add it yourself with `referrer: true` on `href`, `navigate` or `redirect`:
|
|
674
|
+
|
|
675
|
+
```tsx
|
|
676
|
+
// 1. ADD — routes/auth.route.tsx: forbidden() adds the page the user asked for, by default.
|
|
677
|
+
export default createRoute({
|
|
678
|
+
id: 'auth',
|
|
679
|
+
layout: true,
|
|
680
|
+
beforeLoad: ({ context }) => {
|
|
681
|
+
// /nl-BE/bestellingen/42 → /nl-BE/aanmelden?referrer=%2Fnl-BE%2Fbestellingen%2F42
|
|
682
|
+
if (!getSession()) throw context.router.forbidden() // forbidden({ referrer: false }) to leave it out
|
|
683
|
+
},
|
|
684
|
+
})
|
|
685
|
+
|
|
686
|
+
// Anywhere else, ask for it with referrer: true — on any link, navigate or redirect, navigate or redirect: "edit your address, then come back here".
|
|
687
|
+
<a href={router.href({ id: 'address.edit', referrer: true })}>Edit address</a>
|
|
688
|
+
```
|
|
689
|
+
|
|
690
|
+
On the other side, it works like `canGoBack()` / `goBack()`, except it follows `?referrer=` instead
|
|
691
|
+
of browser history. `canGoToReferrer()` tells you whether there's somewhere safe to go,
|
|
692
|
+
`goToReferrer()` goes there, and what to do otherwise is up to you. `referrer()` gives you the route
|
|
693
|
+
itself, for a `beforeLoad` redirect:
|
|
694
|
+
|
|
695
|
+
```tsx
|
|
696
|
+
// 2. GO — routes/sign-in.route.tsx: after signing in, go to the referrer. Where there's none, you decide.
|
|
697
|
+
function SignIn() {
|
|
698
|
+
const { router } = useRouter()
|
|
699
|
+
|
|
700
|
+
async function onSignedIn() {
|
|
701
|
+
router.canGoToReferrer()
|
|
702
|
+
? await router.goToReferrer({ replace: true }) // back to '/nl-BE/bestellingen/42'
|
|
703
|
+
: await router.navigate({ id: 'dashboard', replace: true })
|
|
704
|
+
}
|
|
705
|
+
// …form that calls onSignedIn()
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
// In a beforeLoad you throw a redirect instead: referrer() gives you the route, or undefined.
|
|
709
|
+
export default createRoute({
|
|
710
|
+
id: 'signIn',
|
|
711
|
+
component: SignIn,
|
|
712
|
+
beforeLoad: ({ context }) => {
|
|
713
|
+
const back = context.router.referrer()
|
|
714
|
+
if (getSession()) throw context.router.redirect(back ?? { id: 'dashboard' })
|
|
715
|
+
},
|
|
716
|
+
// …title, description
|
|
717
|
+
})
|
|
718
|
+
```
|
|
719
|
+
|
|
720
|
+
`?referrer=` is untrusted, because anyone can link to `/aanmelden?referrer=https://evil.example`. So
|
|
721
|
+
it's checked every time it's read, and there is no referrer unless all of these hold:
|
|
722
|
+
|
|
723
|
+
- **It's on this site.** A root-relative path, not another origin, `//evil.example` or a backslash
|
|
724
|
+
trick.
|
|
725
|
+
- **It's a page.** The router itself matches the path to one of your routes. Unknown paths, excluded
|
|
726
|
+
routes and paths behind a 403 guard don't count.
|
|
727
|
+
- **It isn't a loop.** Not the current route, and not a status page.
|
|
728
|
+
|
|
729
|
+
The route has no `locale`, so it opens in the *active* one: someone who switched to French on the
|
|
730
|
+
sign-in page goes back to `/fr-BE/commandes/42`. It keeps its own locale only when the page doesn't
|
|
731
|
+
exist in the active one. It reads the raw query string, so the sign-in route doesn't need `referrer`
|
|
732
|
+
in its `validateSearch`. It's not the browser's `document.referrer`, which a client-side navigation
|
|
733
|
+
never updates.
|
|
734
|
+
|
|
735
|
+
#### Through an OAuth round trip
|
|
736
|
+
|
|
737
|
+
Signing in with an identity provider leaves your site, and the provider sends the user back to a
|
|
738
|
+
fixed callback URL that only carries what you put in `state`. So stash the referrer in `state` on the
|
|
739
|
+
way out, and on the callback hand it to the same methods with `from`:
|
|
740
|
+
|
|
741
|
+
```tsx
|
|
742
|
+
// OAuth: the provider only sends back what you put in `state`, so the referrer travels there.
|
|
743
|
+
|
|
744
|
+
// 1. Sign-in page, before leaving for the provider: stash the CHECKED referrer as an href.
|
|
745
|
+
function signInWithProvider() {
|
|
746
|
+
const back = router.referrer() // undefined when there's nothing safe to go back to
|
|
747
|
+
const state = encodeState({ nonce: newNonce(), referrer: back && router.href(back) })
|
|
748
|
+
window.location.assign(`${provider}/authorize?client_id=…&state=${state}`)
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
// 2. routes/auth-callback.route.tsx: /auth/callback?code=…&state=…
|
|
752
|
+
export default createRoute({
|
|
753
|
+
id: 'authCallback',
|
|
754
|
+
loader: async ({ context, search }) => {
|
|
755
|
+
const { nonce, referrer } = decodeState(search.state) // verifying state is your app's job
|
|
756
|
+
await exchangeCode(search.code, nonce)
|
|
757
|
+
|
|
758
|
+
// `from` checks the value from state exactly like ?referrer= — anyone can craft a callback URL.
|
|
759
|
+
// In a loader you redirect rather than navigate, so take the route itself; where there's none, you decide.
|
|
760
|
+
const back = context.router.referrer({ from: referrer })
|
|
761
|
+
throw context.router.redirect({ ...(back ?? { id: 'dashboard' }), replace: true })
|
|
762
|
+
},
|
|
763
|
+
// …title, description
|
|
764
|
+
})
|
|
765
|
+
```
|
|
766
|
+
|
|
767
|
+
- **Stash the checked href**, `router.href(router.referrer())`, not the raw `?referrer=`. It's smaller,
|
|
768
|
+
and it's already a page on your site.
|
|
769
|
+
- **Check it again on the callback.** A valid nonce proves the callback belongs to your sign-in, not
|
|
770
|
+
that what's inside `state` is safe. `from` runs it through the same checks as `?referrer=`.
|
|
771
|
+
- **Once you pass `from`, the URL isn't read.** `from: undefined`, when `state` had no referrer, means
|
|
772
|
+
there is none.
|
|
773
|
+
- **Mind the callback's locale.** The referrer opens in the active locale, which on the callback is
|
|
774
|
+
the callback route's. If your provider only accepts one fixed redirect URL, put the user's locale
|
|
775
|
+
in `state` too and pass it along: `redirect({ ...back, locale: state.locale })`.
|
|
776
|
+
|
|
777
|
+
Encoding, signing and verifying `state` stays with your app or OAuth library; the router only checks
|
|
778
|
+
the referrer that comes out of it.
|
|
779
|
+
|
|
631
780
|
### Legacy redirects
|
|
632
781
|
|
|
633
782
|
When URLs change (renamed locale codes, a moved page), old bookmarks and indexed links shouldn't
|
|
@@ -697,6 +846,37 @@ On an unprefixed route the active locale is the default locale — `useRouter().
|
|
|
697
846
|
the default locale: `router.href({ id: 'home' })` returns `/home`, not `/nl-BE/home` — so `navigate`
|
|
698
847
|
and every generated link stay unprefixed while you're in the default locale.
|
|
699
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
|
+
|
|
700
880
|
## API
|
|
701
881
|
|
|
702
882
|
| Export | Description |
|
|
@@ -709,7 +889,12 @@ and every generated link stay unprefixed while you're in the default locale.
|
|
|
709
889
|
| `createRouter(registry, config)` | Expand a registry into a TanStack `Router`. |
|
|
710
890
|
| `useRouter()` | `{ router }` — `href`/`path`/`absolute`/`navigate`/`redirect`/`notFound`/`forbidden`, plus `locale`, `routeId`, `locales`, `has`, `availableLocales`. |
|
|
711
891
|
| `router.redirect` / `.notFound` / `.forbidden` | Locale-aware, throwable results for `beforeLoad` / `loader` — redirect by route id, notFound, or forbidden (→ `statusPages[403]`). Also on `context.router`. |
|
|
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). |
|
|
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). |
|
|
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). |
|
|
712
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). |
|
|
713
898
|
| `RouterProvider` / `Outlet` | Re-exported from the bundled TanStack Router. Mount the tree / render children. |
|
|
714
899
|
| `HeadContent` / `Scripts` | Re-exported from the bundled TanStack Router. Render a route's `head()` tags / body scripts. |
|
|
715
900
|
| `Await` / `useAwaited` / `defer` | Re-exported from the bundled TanStack Router. Stream deferred loader data (skeletons). |
|
|
@@ -717,6 +902,7 @@ and every generated link stay unprefixed while you're in the default locale.
|
|
|
717
902
|
| `RouteConfig` / `RouterConfig` | Config types. `createRoute<TParams, TSearch>` types the handler `ctx`. |
|
|
718
903
|
| `StatusPages` | The `statusPages` config type — `Record<number, string> & { default: string }`. |
|
|
719
904
|
| `RouteRegister` | Augment to map `id` → `{ params?; search? }` for typed `href` / `navigate` (see below). |
|
|
905
|
+
| `RouteStaticData` | Augment to type the keys of `staticData` / `useRouteStaticData()`. |
|
|
720
906
|
|
|
721
907
|
## Type safety
|
|
722
908
|
|
package/dist/index.cjs
CHANGED
|
@@ -7048,8 +7048,20 @@ function setRouterMeta(router, meta) {
|
|
|
7048
7048
|
function getRouterMeta(router) {
|
|
7049
7049
|
return store.get(router);
|
|
7050
7050
|
}
|
|
7051
|
+
const tags = /* @__PURE__ */ new WeakMap();
|
|
7052
|
+
/** Tag a built route with the route id + locale it came from. Called by {@link createRouter}. */
|
|
7053
|
+
function tagRoute(route, tag) {
|
|
7054
|
+
tags.set(route, tag);
|
|
7055
|
+
}
|
|
7056
|
+
/** The route id + locale a built route came from, or `undefined` for a synthetic route. */
|
|
7057
|
+
function getRouteTag(route) {
|
|
7058
|
+
return route ? tags.get(route) : void 0;
|
|
7059
|
+
}
|
|
7051
7060
|
//#endregion
|
|
7052
7061
|
//#region src/use-router.ts
|
|
7062
|
+
/** The query param the referrer travels in. */
|
|
7063
|
+
const REFERRER = "referrer";
|
|
7064
|
+
const SAME_ORIGIN = "http://same-origin.invalid";
|
|
7053
7065
|
const fillSegment = (seg, params, id) => {
|
|
7054
7066
|
if (seg === "$") return params["*"] ?? "";
|
|
7055
7067
|
if (!seg.startsWith("$")) return seg;
|
|
@@ -7136,53 +7148,66 @@ function routerApi(input) {
|
|
|
7136
7148
|
const { registry, config, mounted, active, activeId, getTanstack } = input;
|
|
7137
7149
|
const toLocale = (l) => l == null ? active : typeof l !== "string" ? l : config.locales.find((x) => x.code === l) ?? _luwiostack_locale.Locale.new({ languageOrLocale: l });
|
|
7138
7150
|
const path = ({ id, locale, params }) => routePath(registry, config, id, toLocale(locale), params);
|
|
7139
|
-
const
|
|
7151
|
+
const latest = () => getTanstack()?.latestLocation;
|
|
7152
|
+
const withReferrer = (query, referrer) => {
|
|
7153
|
+
const current = referrer ? latest()?.href : void 0;
|
|
7154
|
+
return current === void 0 ? query : {
|
|
7155
|
+
...query,
|
|
7156
|
+
[REFERRER]: current
|
|
7157
|
+
};
|
|
7158
|
+
};
|
|
7159
|
+
const href = ({ id, locale, params, query, hash, referrer }) => path({
|
|
7140
7160
|
id,
|
|
7141
7161
|
locale,
|
|
7142
7162
|
params
|
|
7143
|
-
}) + suffix(query, hash);
|
|
7163
|
+
}) + suffix(withReferrer(query, referrer), hash);
|
|
7144
7164
|
const absolute = ({ baseUrl, ...rest }) => resolveOrigin(config, baseUrl) + href(rest);
|
|
7145
7165
|
const liveRouter = () => {
|
|
7146
7166
|
const t = getTanstack();
|
|
7147
7167
|
if (!t) throw new Error("router: navigation is unavailable in this context (no live router yet).");
|
|
7148
7168
|
return t;
|
|
7149
7169
|
};
|
|
7150
|
-
const navigate = async ({ id, from, params, query, hash, locale, replace, state, target }) => {
|
|
7170
|
+
const navigate = async ({ id, from, params, query, hash, locale, replace, state, target, referrer }) => {
|
|
7151
7171
|
if (target === "_blank") {
|
|
7152
7172
|
if (typeof window !== "undefined") window.open(absolute({
|
|
7153
7173
|
id,
|
|
7154
7174
|
locale,
|
|
7155
7175
|
params,
|
|
7156
7176
|
query,
|
|
7157
|
-
hash
|
|
7177
|
+
hash,
|
|
7178
|
+
referrer
|
|
7158
7179
|
}), "_blank");
|
|
7159
7180
|
return;
|
|
7160
7181
|
}
|
|
7182
|
+
const search = withReferrer(query, referrer);
|
|
7161
7183
|
await liveRouter().navigate({
|
|
7162
7184
|
to: path({
|
|
7163
7185
|
id,
|
|
7164
7186
|
locale,
|
|
7165
7187
|
params
|
|
7166
7188
|
}),
|
|
7167
|
-
search:
|
|
7189
|
+
search: search ? cleanSearch(search) : void 0,
|
|
7168
7190
|
hash,
|
|
7169
7191
|
replace,
|
|
7170
7192
|
state,
|
|
7171
7193
|
from
|
|
7172
7194
|
});
|
|
7173
7195
|
};
|
|
7174
|
-
const buildRedirect = (opts) =>
|
|
7175
|
-
|
|
7176
|
-
|
|
7177
|
-
|
|
7178
|
-
|
|
7179
|
-
|
|
7180
|
-
|
|
7181
|
-
|
|
7182
|
-
|
|
7183
|
-
|
|
7184
|
-
|
|
7185
|
-
|
|
7196
|
+
const buildRedirect = (opts) => {
|
|
7197
|
+
const search = withReferrer(opts.query, opts.referrer);
|
|
7198
|
+
return redirect({
|
|
7199
|
+
to: path({
|
|
7200
|
+
id: opts.id,
|
|
7201
|
+
locale: opts.locale,
|
|
7202
|
+
params: opts.params
|
|
7203
|
+
}),
|
|
7204
|
+
search: search ? cleanSearch(search) : void 0,
|
|
7205
|
+
hash: opts.hash,
|
|
7206
|
+
replace: opts.replace,
|
|
7207
|
+
...opts.statusCode != null ? { statusCode: opts.statusCode } : {},
|
|
7208
|
+
...opts.state ? { state: opts.state } : {}
|
|
7209
|
+
});
|
|
7210
|
+
};
|
|
7186
7211
|
const redirect$1 = (params) => buildRedirect(params);
|
|
7187
7212
|
const notFound$1 = (options) => notFound(options);
|
|
7188
7213
|
const forbidden = (params) => buildRedirect({
|
|
@@ -7193,8 +7218,38 @@ function routerApi(input) {
|
|
|
7193
7218
|
hash: params?.hash,
|
|
7194
7219
|
replace: params?.replace,
|
|
7195
7220
|
statusCode: params?.statusCode ?? 403,
|
|
7196
|
-
state: { status: 403 }
|
|
7221
|
+
state: { status: 403 },
|
|
7222
|
+
referrer: params?.referrer ?? true
|
|
7197
7223
|
});
|
|
7224
|
+
const referrer = (options) => {
|
|
7225
|
+
const raw = options && "from" in options ? options.from : new URLSearchParams(latest()?.searchStr ?? "").get(REFERRER);
|
|
7226
|
+
if (typeof raw !== "string" || !raw.startsWith("/") || /^\/[/\\]/.test(raw)) return void 0;
|
|
7227
|
+
let url;
|
|
7228
|
+
try {
|
|
7229
|
+
url = new URL(raw, SAME_ORIGIN);
|
|
7230
|
+
} catch {
|
|
7231
|
+
return;
|
|
7232
|
+
}
|
|
7233
|
+
if (url.origin !== SAME_ORIGIN) return void 0;
|
|
7234
|
+
const live = getTanstack();
|
|
7235
|
+
if (!live) return void 0;
|
|
7236
|
+
const [, rawParams, foundRoute] = live.getMatchedRoutes(url.pathname);
|
|
7237
|
+
const tag = getRouteTag(foundRoute);
|
|
7238
|
+
if (!tag) return void 0;
|
|
7239
|
+
if (tag.id === activeId || Object.values(config.statusPages).includes(tag.id)) return void 0;
|
|
7240
|
+
const { _splat: _, ...params } = rawParams;
|
|
7241
|
+
url.searchParams.delete(REFERRER);
|
|
7242
|
+
const query = Object.fromEntries(url.searchParams);
|
|
7243
|
+
const hash = url.hash.slice(1);
|
|
7244
|
+
const inActive = availableLocales(registry, config, tag.id).some((l) => l.code === active.code);
|
|
7245
|
+
return {
|
|
7246
|
+
id: tag.id,
|
|
7247
|
+
params,
|
|
7248
|
+
...Object.keys(query).length > 0 ? { query } : {},
|
|
7249
|
+
...hash ? { hash } : {},
|
|
7250
|
+
...inActive ? {} : { locale: tag.locale }
|
|
7251
|
+
};
|
|
7252
|
+
};
|
|
7198
7253
|
return {
|
|
7199
7254
|
locale: active,
|
|
7200
7255
|
routeId: activeId,
|
|
@@ -7212,6 +7267,15 @@ function routerApi(input) {
|
|
|
7212
7267
|
redirect: redirect$1,
|
|
7213
7268
|
notFound: notFound$1,
|
|
7214
7269
|
forbidden,
|
|
7270
|
+
referrer,
|
|
7271
|
+
canGoToReferrer: (options) => referrer(options) !== void 0,
|
|
7272
|
+
goToReferrer: async ({ replace, ...options } = {}) => {
|
|
7273
|
+
const target = referrer(options);
|
|
7274
|
+
if (target) await navigate({
|
|
7275
|
+
...target,
|
|
7276
|
+
replace
|
|
7277
|
+
});
|
|
7278
|
+
},
|
|
7215
7279
|
reload: () => liveRouter().invalidate(),
|
|
7216
7280
|
goBack: async () => {
|
|
7217
7281
|
liveRouter().history.back();
|
|
@@ -7423,6 +7487,10 @@ function createRouter(registry, config) {
|
|
|
7423
7487
|
...userLoader ? { loader: guardLoadPhase(userLoader, apiCtx, locale) } : {},
|
|
7424
7488
|
...head ? { head } : {}
|
|
7425
7489
|
});
|
|
7490
|
+
if (!b.layout) tagRoute(route, {
|
|
7491
|
+
id: b.id,
|
|
7492
|
+
locale
|
|
7493
|
+
});
|
|
7426
7494
|
const children = build(b.id, route);
|
|
7427
7495
|
return children.length ? route.addChildren(children) : route;
|
|
7428
7496
|
});
|
|
@@ -7781,6 +7849,40 @@ function lazy(importer, exportName) {
|
|
|
7781
7849
|
return lazyRouteComponent(importer, exportName);
|
|
7782
7850
|
}
|
|
7783
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
|
|
7784
7886
|
//#region src/route-builder.ts
|
|
7785
7887
|
/**
|
|
7786
7888
|
* The route-config keys we recognise: this package's own three, the SEO/head keys the router adds,
|
|
@@ -7818,6 +7920,7 @@ const KNOWN_KEYS = /* @__PURE__ */ new Set([
|
|
|
7818
7920
|
"preloadGcTime",
|
|
7819
7921
|
"caseSensitive",
|
|
7820
7922
|
"wrapInSuspense",
|
|
7923
|
+
"staticData",
|
|
7821
7924
|
"preload",
|
|
7822
7925
|
"ssr",
|
|
7823
7926
|
"pendingMs",
|
|
@@ -8064,6 +8167,12 @@ function useRouteLoaderData() {
|
|
|
8064
8167
|
function useRouteContext() {
|
|
8065
8168
|
return useRouteContext$1({ strict: false });
|
|
8066
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
|
+
}
|
|
8067
8176
|
/**
|
|
8068
8177
|
* The failure that redirected here, for a page configured in `createRouter`'s `statusPages` (404,
|
|
8069
8178
|
* 403, a dedicated status like 503, or the `default` catch-all). The router attaches this as history
|
|
@@ -8099,6 +8208,7 @@ exports.createRoute = createRoute;
|
|
|
8099
8208
|
exports.createRouter = createRouter;
|
|
8100
8209
|
exports.defer = defer;
|
|
8101
8210
|
exports.lazy = lazy;
|
|
8211
|
+
exports.onPageView = onPageView;
|
|
8102
8212
|
exports.registerModules = registerModules;
|
|
8103
8213
|
exports.routePath = routePath;
|
|
8104
8214
|
exports.routeRegistry = routeRegistry;
|
|
@@ -8111,6 +8221,7 @@ exports.useRouteLoaderData = useRouteLoaderData;
|
|
|
8111
8221
|
exports.useRouteLocale = useRouteLocale;
|
|
8112
8222
|
exports.useRouteParams = useRouteParams;
|
|
8113
8223
|
exports.useRouteQuery = useRouteQuery;
|
|
8224
|
+
exports.useRouteStaticData = useRouteStaticData;
|
|
8114
8225
|
exports.useRouter = useRouter;
|
|
8115
8226
|
|
|
8116
8227
|
//# sourceMappingURL=index.cjs.map
|