@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/dist/index.mjs
CHANGED
|
@@ -6068,8 +6068,20 @@ function setRouterMeta(router, meta) {
|
|
|
6068
6068
|
function getRouterMeta(router) {
|
|
6069
6069
|
return store.get(router);
|
|
6070
6070
|
}
|
|
6071
|
+
const tags = /* @__PURE__ */ new WeakMap();
|
|
6072
|
+
/** Tag a built route with the route id + locale it came from. Called by {@link createRouter}. */
|
|
6073
|
+
function tagRoute(route, tag) {
|
|
6074
|
+
tags.set(route, tag);
|
|
6075
|
+
}
|
|
6076
|
+
/** The route id + locale a built route came from, or `undefined` for a synthetic route. */
|
|
6077
|
+
function getRouteTag(route) {
|
|
6078
|
+
return route ? tags.get(route) : void 0;
|
|
6079
|
+
}
|
|
6071
6080
|
//#endregion
|
|
6072
6081
|
//#region src/use-router.ts
|
|
6082
|
+
/** The query param the referrer travels in. */
|
|
6083
|
+
const REFERRER = "referrer";
|
|
6084
|
+
const SAME_ORIGIN = "http://same-origin.invalid";
|
|
6073
6085
|
const fillSegment = (seg, params, id) => {
|
|
6074
6086
|
if (seg === "$") return params["*"] ?? "";
|
|
6075
6087
|
if (!seg.startsWith("$")) return seg;
|
|
@@ -6156,53 +6168,66 @@ function routerApi(input) {
|
|
|
6156
6168
|
const { registry, config, mounted, active, activeId, getTanstack } = input;
|
|
6157
6169
|
const toLocale = (l) => l == null ? active : typeof l !== "string" ? l : config.locales.find((x) => x.code === l) ?? Locale$1.new({ languageOrLocale: l });
|
|
6158
6170
|
const path = ({ id, locale, params }) => routePath(registry, config, id, toLocale(locale), params);
|
|
6159
|
-
const
|
|
6171
|
+
const latest = () => getTanstack()?.latestLocation;
|
|
6172
|
+
const withReferrer = (query, referrer) => {
|
|
6173
|
+
const current = referrer ? latest()?.href : void 0;
|
|
6174
|
+
return current === void 0 ? query : {
|
|
6175
|
+
...query,
|
|
6176
|
+
[REFERRER]: current
|
|
6177
|
+
};
|
|
6178
|
+
};
|
|
6179
|
+
const href = ({ id, locale, params, query, hash, referrer }) => path({
|
|
6160
6180
|
id,
|
|
6161
6181
|
locale,
|
|
6162
6182
|
params
|
|
6163
|
-
}) + suffix(query, hash);
|
|
6183
|
+
}) + suffix(withReferrer(query, referrer), hash);
|
|
6164
6184
|
const absolute = ({ baseUrl, ...rest }) => resolveOrigin(config, baseUrl) + href(rest);
|
|
6165
6185
|
const liveRouter = () => {
|
|
6166
6186
|
const t = getTanstack();
|
|
6167
6187
|
if (!t) throw new Error("router: navigation is unavailable in this context (no live router yet).");
|
|
6168
6188
|
return t;
|
|
6169
6189
|
};
|
|
6170
|
-
const navigate = async ({ id, from, params, query, hash, locale, replace, state, target }) => {
|
|
6190
|
+
const navigate = async ({ id, from, params, query, hash, locale, replace, state, target, referrer }) => {
|
|
6171
6191
|
if (target === "_blank") {
|
|
6172
6192
|
if (typeof window !== "undefined") window.open(absolute({
|
|
6173
6193
|
id,
|
|
6174
6194
|
locale,
|
|
6175
6195
|
params,
|
|
6176
6196
|
query,
|
|
6177
|
-
hash
|
|
6197
|
+
hash,
|
|
6198
|
+
referrer
|
|
6178
6199
|
}), "_blank");
|
|
6179
6200
|
return;
|
|
6180
6201
|
}
|
|
6202
|
+
const search = withReferrer(query, referrer);
|
|
6181
6203
|
await liveRouter().navigate({
|
|
6182
6204
|
to: path({
|
|
6183
6205
|
id,
|
|
6184
6206
|
locale,
|
|
6185
6207
|
params
|
|
6186
6208
|
}),
|
|
6187
|
-
search:
|
|
6209
|
+
search: search ? cleanSearch(search) : void 0,
|
|
6188
6210
|
hash,
|
|
6189
6211
|
replace,
|
|
6190
6212
|
state,
|
|
6191
6213
|
from
|
|
6192
6214
|
});
|
|
6193
6215
|
};
|
|
6194
|
-
const buildRedirect = (opts) =>
|
|
6195
|
-
|
|
6196
|
-
|
|
6197
|
-
|
|
6198
|
-
|
|
6199
|
-
|
|
6200
|
-
|
|
6201
|
-
|
|
6202
|
-
|
|
6203
|
-
|
|
6204
|
-
|
|
6205
|
-
|
|
6216
|
+
const buildRedirect = (opts) => {
|
|
6217
|
+
const search = withReferrer(opts.query, opts.referrer);
|
|
6218
|
+
return redirect({
|
|
6219
|
+
to: path({
|
|
6220
|
+
id: opts.id,
|
|
6221
|
+
locale: opts.locale,
|
|
6222
|
+
params: opts.params
|
|
6223
|
+
}),
|
|
6224
|
+
search: search ? cleanSearch(search) : void 0,
|
|
6225
|
+
hash: opts.hash,
|
|
6226
|
+
replace: opts.replace,
|
|
6227
|
+
...opts.statusCode != null ? { statusCode: opts.statusCode } : {},
|
|
6228
|
+
...opts.state ? { state: opts.state } : {}
|
|
6229
|
+
});
|
|
6230
|
+
};
|
|
6206
6231
|
const redirect$1 = (params) => buildRedirect(params);
|
|
6207
6232
|
const notFound$1 = (options) => notFound(options);
|
|
6208
6233
|
const forbidden = (params) => buildRedirect({
|
|
@@ -6213,8 +6238,38 @@ function routerApi(input) {
|
|
|
6213
6238
|
hash: params?.hash,
|
|
6214
6239
|
replace: params?.replace,
|
|
6215
6240
|
statusCode: params?.statusCode ?? 403,
|
|
6216
|
-
state: { status: 403 }
|
|
6241
|
+
state: { status: 403 },
|
|
6242
|
+
referrer: params?.referrer ?? true
|
|
6217
6243
|
});
|
|
6244
|
+
const referrer = (options) => {
|
|
6245
|
+
const raw = options && "from" in options ? options.from : new URLSearchParams(latest()?.searchStr ?? "").get(REFERRER);
|
|
6246
|
+
if (typeof raw !== "string" || !raw.startsWith("/") || /^\/[/\\]/.test(raw)) return void 0;
|
|
6247
|
+
let url;
|
|
6248
|
+
try {
|
|
6249
|
+
url = new URL(raw, SAME_ORIGIN);
|
|
6250
|
+
} catch {
|
|
6251
|
+
return;
|
|
6252
|
+
}
|
|
6253
|
+
if (url.origin !== SAME_ORIGIN) return void 0;
|
|
6254
|
+
const live = getTanstack();
|
|
6255
|
+
if (!live) return void 0;
|
|
6256
|
+
const [, rawParams, foundRoute] = live.getMatchedRoutes(url.pathname);
|
|
6257
|
+
const tag = getRouteTag(foundRoute);
|
|
6258
|
+
if (!tag) return void 0;
|
|
6259
|
+
if (tag.id === activeId || Object.values(config.statusPages).includes(tag.id)) return void 0;
|
|
6260
|
+
const { _splat: _, ...params } = rawParams;
|
|
6261
|
+
url.searchParams.delete(REFERRER);
|
|
6262
|
+
const query = Object.fromEntries(url.searchParams);
|
|
6263
|
+
const hash = url.hash.slice(1);
|
|
6264
|
+
const inActive = availableLocales(registry, config, tag.id).some((l) => l.code === active.code);
|
|
6265
|
+
return {
|
|
6266
|
+
id: tag.id,
|
|
6267
|
+
params,
|
|
6268
|
+
...Object.keys(query).length > 0 ? { query } : {},
|
|
6269
|
+
...hash ? { hash } : {},
|
|
6270
|
+
...inActive ? {} : { locale: tag.locale }
|
|
6271
|
+
};
|
|
6272
|
+
};
|
|
6218
6273
|
return {
|
|
6219
6274
|
locale: active,
|
|
6220
6275
|
routeId: activeId,
|
|
@@ -6232,6 +6287,15 @@ function routerApi(input) {
|
|
|
6232
6287
|
redirect: redirect$1,
|
|
6233
6288
|
notFound: notFound$1,
|
|
6234
6289
|
forbidden,
|
|
6290
|
+
referrer,
|
|
6291
|
+
canGoToReferrer: (options) => referrer(options) !== void 0,
|
|
6292
|
+
goToReferrer: async ({ replace, ...options } = {}) => {
|
|
6293
|
+
const target = referrer(options);
|
|
6294
|
+
if (target) await navigate({
|
|
6295
|
+
...target,
|
|
6296
|
+
replace
|
|
6297
|
+
});
|
|
6298
|
+
},
|
|
6235
6299
|
reload: () => liveRouter().invalidate(),
|
|
6236
6300
|
goBack: async () => {
|
|
6237
6301
|
liveRouter().history.back();
|
|
@@ -6443,6 +6507,10 @@ function createRouter(registry, config) {
|
|
|
6443
6507
|
...userLoader ? { loader: guardLoadPhase(userLoader, apiCtx, locale) } : {},
|
|
6444
6508
|
...head ? { head } : {}
|
|
6445
6509
|
});
|
|
6510
|
+
if (!b.layout) tagRoute(route, {
|
|
6511
|
+
id: b.id,
|
|
6512
|
+
locale
|
|
6513
|
+
});
|
|
6446
6514
|
const children = build(b.id, route);
|
|
6447
6515
|
return children.length ? route.addChildren(children) : route;
|
|
6448
6516
|
});
|
|
@@ -6801,6 +6869,40 @@ function lazy(importer, exportName) {
|
|
|
6801
6869
|
return lazyRouteComponent(importer, exportName);
|
|
6802
6870
|
}
|
|
6803
6871
|
//#endregion
|
|
6872
|
+
//#region src/page-view.ts
|
|
6873
|
+
/**
|
|
6874
|
+
* Call `listener` with each page the visitor lands on: once for the first load, then once per
|
|
6875
|
+
* navigation that resolves to a different path or query string. A hash-only change isn't a new page,
|
|
6876
|
+
* and neither is a re-resolve of the same URL. Synthetic routes (the 404 catch-all, a forbidden
|
|
6877
|
+
* guard, a legacy redirect) are skipped; the page they redirect to is reported instead. The title is the route's
|
|
6878
|
+
* resolved `title()`, which is ready before `<HeadContent>` updates `document.title`.
|
|
6879
|
+
*
|
|
6880
|
+
* Knows nothing about analytics: hand the page to whatever tracks it, e.g.
|
|
6881
|
+
* `onPageView(router, (page) => analytics.pageView({ title: page.title, route_id: page.routeId }))`.
|
|
6882
|
+
*
|
|
6883
|
+
* Subscribe before the router first loads (before rendering `<RouterProvider>`) to see the first
|
|
6884
|
+
* page. Returns the unsubscribe.
|
|
6885
|
+
*/
|
|
6886
|
+
function onPageView(router, listener) {
|
|
6887
|
+
let lastKey;
|
|
6888
|
+
return router.subscribe("onResolved", ({ toLocation }) => {
|
|
6889
|
+
const key = `${toLocation.pathname}?${toLocation.searchStr}`;
|
|
6890
|
+
if (key === lastKey) return;
|
|
6891
|
+
const leaf = router.state.matches.at(-1);
|
|
6892
|
+
const tag = leaf ? getRouteTag(router.routesById[leaf.routeId]) : void 0;
|
|
6893
|
+
if (!leaf || !tag) return;
|
|
6894
|
+
lastKey = key;
|
|
6895
|
+
listener({
|
|
6896
|
+
routeId: tag.id,
|
|
6897
|
+
locale: tag.locale,
|
|
6898
|
+
title: (leaf.meta ?? []).find((m) => m?.title !== void 0)?.title,
|
|
6899
|
+
path: toLocation.pathname,
|
|
6900
|
+
href: toLocation.href,
|
|
6901
|
+
params: { ...leaf.params ?? {} }
|
|
6902
|
+
});
|
|
6903
|
+
});
|
|
6904
|
+
}
|
|
6905
|
+
//#endregion
|
|
6804
6906
|
//#region src/route-builder.ts
|
|
6805
6907
|
/**
|
|
6806
6908
|
* The route-config keys we recognise: this package's own three, the SEO/head keys the router adds,
|
|
@@ -6838,6 +6940,7 @@ const KNOWN_KEYS = /* @__PURE__ */ new Set([
|
|
|
6838
6940
|
"preloadGcTime",
|
|
6839
6941
|
"caseSensitive",
|
|
6840
6942
|
"wrapInSuspense",
|
|
6943
|
+
"staticData",
|
|
6841
6944
|
"preload",
|
|
6842
6945
|
"ssr",
|
|
6843
6946
|
"pendingMs",
|
|
@@ -7084,6 +7187,12 @@ function useRouteLoaderData() {
|
|
|
7084
7187
|
function useRouteContext() {
|
|
7085
7188
|
return useRouteContext$1({ strict: false });
|
|
7086
7189
|
}
|
|
7190
|
+
function useRouteStaticData(select) {
|
|
7191
|
+
return useRouterState({ select: (s) => {
|
|
7192
|
+
const merged = Object.assign({}, ...s.matches.map((m) => m.staticData));
|
|
7193
|
+
return select ? select(merged) : merged;
|
|
7194
|
+
} });
|
|
7195
|
+
}
|
|
7087
7196
|
/**
|
|
7088
7197
|
* The failure that redirected here, for a page configured in `createRouter`'s `statusPages` (404,
|
|
7089
7198
|
* 403, a dedicated status like 503, or the `default` catch-all). The router attaches this as history
|
|
@@ -7107,6 +7216,6 @@ function useRouteFailure() {
|
|
|
7107
7216
|
} : void 0;
|
|
7108
7217
|
}
|
|
7109
7218
|
//#endregion
|
|
7110
|
-
export { Await, HeadContent, Outlet, RouteBuilder, RouteRegistry, RouterProvider, Scripts, availableLocales, createRoute, createRouter, defer, lazy, registerModules, routePath, routeRegistry, statusPageFor, useAwaited, useLocation, useRouteContext, useRouteFailure, useRouteLoaderData, useRouteLocale, useRouteParams, useRouteQuery, useRouter };
|
|
7219
|
+
export { Await, HeadContent, Outlet, RouteBuilder, RouteRegistry, RouterProvider, Scripts, availableLocales, createRoute, createRouter, defer, lazy, onPageView, registerModules, routePath, routeRegistry, statusPageFor, useAwaited, useLocation, useRouteContext, useRouteFailure, useRouteLoaderData, useRouteLocale, useRouteParams, useRouteQuery, useRouteStaticData, useRouter };
|
|
7111
7220
|
|
|
7112
7221
|
//# sourceMappingURL=index.mjs.map
|