@qwik.dev/router 2.0.0-beta.4 → 2.0.0-beta.40

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.
Files changed (87) hide show
  1. package/adapters/static/vite.d.ts +1 -1
  2. package/lib/adapters/azure-swa/vite/index.d.ts +2 -2
  3. package/lib/adapters/azure-swa/vite/index.mjs +44 -58
  4. package/lib/adapters/bun-server/vite/index.d.ts +2 -2
  5. package/lib/adapters/bun-server/vite/index.mjs +16 -23
  6. package/lib/adapters/cloud-run/vite/index.d.ts +2 -2
  7. package/lib/adapters/cloud-run/vite/index.mjs +15 -20
  8. package/lib/adapters/cloudflare-pages/vite/index.d.ts +2 -2
  9. package/lib/adapters/cloudflare-pages/vite/index.mjs +45 -77
  10. package/lib/adapters/deno-server/vite/index.d.ts +2 -2
  11. package/lib/adapters/deno-server/vite/index.mjs +25 -35
  12. package/lib/adapters/netlify-edge/vite/index.d.ts +2 -2
  13. package/lib/adapters/netlify-edge/vite/index.mjs +49 -89
  14. package/lib/adapters/node-server/vite/index.d.ts +2 -2
  15. package/lib/adapters/node-server/vite/index.mjs +16 -23
  16. package/lib/adapters/shared/vite/index.d.ts +9 -22
  17. package/lib/adapters/shared/vite/index.mjs +273 -333
  18. package/lib/adapters/ssg/vite/index.d.ts +13 -0
  19. package/lib/adapters/ssg/vite/index.mjs +13 -0
  20. package/lib/adapters/vercel-edge/vite/index.d.ts +3 -3
  21. package/lib/adapters/vercel-edge/vite/index.mjs +73 -80
  22. package/lib/chunks/dev-preloaded-route-loader.mjs +2 -0
  23. package/lib/chunks/format.mjs +24 -0
  24. package/lib/chunks/fs.mjs +133 -0
  25. package/lib/chunks/head.qwik.mjs +1217 -0
  26. package/lib/chunks/http-error.qwik.mjs +27 -0
  27. package/lib/chunks/pathname.mjs +57 -0
  28. package/lib/chunks/request-body-limit.mjs +27 -0
  29. package/lib/chunks/request-path.mjs +49 -0
  30. package/lib/chunks/routes.mjs +26 -0
  31. package/lib/chunks/server-error.mjs +16 -0
  32. package/lib/chunks/system.mjs +265 -0
  33. package/lib/chunks/url.mjs +59 -0
  34. package/lib/chunks/user-response.mjs +1707 -0
  35. package/lib/chunks/worker-thread.mjs +264 -0
  36. package/lib/index.d.ts +626 -170
  37. package/lib/index.qwik.mjs +1649 -1964
  38. package/lib/middleware/aws-lambda/index.d.ts +15 -16
  39. package/lib/middleware/aws-lambda/index.mjs +45 -54
  40. package/lib/middleware/azure-swa/index.mjs +85 -295
  41. package/lib/middleware/bun/index.d.ts +20 -5
  42. package/lib/middleware/bun/index.mjs +119 -205
  43. package/lib/middleware/cloudflare-pages/index.mjs +83 -105
  44. package/lib/middleware/deno/index.d.ts +22 -5
  45. package/lib/middleware/deno/index.mjs +105 -192
  46. package/lib/middleware/firebase/index.mjs +22 -35
  47. package/lib/middleware/netlify-edge/index.mjs +63 -81
  48. package/lib/middleware/node/index.d.ts +17 -5
  49. package/lib/middleware/node/index.mjs +299 -273
  50. package/lib/middleware/request-handler/index.d.ts +206 -99
  51. package/lib/middleware/request-handler/index.mjs +117 -1648
  52. package/lib/middleware/vercel-edge/index.mjs +76 -101
  53. package/lib/modules.d.ts +11 -16
  54. package/lib/service-worker/index.mjs +13 -0
  55. package/lib/{static → ssg}/index.d.ts +46 -22
  56. package/lib/ssg/index.mjs +257 -0
  57. package/lib/vite/index.d.ts +58 -10
  58. package/lib/vite/index.mjs +2459 -27446
  59. package/modules.d.ts +11 -16
  60. package/package.json +63 -67
  61. package/ssg.d.ts +2 -0
  62. package/static.d.ts +1 -1
  63. package/lib/adapters/azure-swa/vite/index.cjs +0 -96
  64. package/lib/adapters/bun-server/vite/index.cjs +0 -50
  65. package/lib/adapters/cloud-run/vite/index.cjs +0 -47
  66. package/lib/adapters/cloudflare-pages/vite/index.cjs +0 -115
  67. package/lib/adapters/deno-server/vite/index.cjs +0 -62
  68. package/lib/adapters/netlify-edge/vite/index.cjs +0 -129
  69. package/lib/adapters/node-server/vite/index.cjs +0 -50
  70. package/lib/adapters/shared/vite/index.cjs +0 -378
  71. package/lib/adapters/static/vite/index.cjs +0 -368
  72. package/lib/adapters/static/vite/index.d.ts +0 -10
  73. package/lib/adapters/static/vite/index.mjs +0 -331
  74. package/lib/adapters/vercel-edge/vite/index.cjs +0 -118
  75. package/lib/index.qwik.cjs +0 -2022
  76. package/lib/middleware/node/index.cjs +0 -314
  77. package/lib/middleware/request-handler/index.cjs +0 -1691
  78. package/lib/service-worker.cjs +0 -17
  79. package/lib/service-worker.mjs +0 -15
  80. package/lib/static/deno.mjs +0 -8
  81. package/lib/static/index.cjs +0 -67
  82. package/lib/static/index.mjs +0 -48
  83. package/lib/static/node.cjs +0 -1126
  84. package/lib/static/node.mjs +0 -1088
  85. package/lib/vite/index.cjs +0 -27525
  86. package/middleware/request-handler/generated/not-found-paths.ts +0 -7
  87. package/middleware/request-handler/generated/static-paths.ts +0 -35
@@ -1,2003 +1,1688 @@
1
- import { jsx, Fragment, jsxs } from "@qwik.dev/core/jsx-runtime";
2
- import { component$, useErrorBoundary, useOnWindow, $, Slot, createAsyncComputed$, isBrowser, createContextId, implicit$FirstArg, useContext, useVisibleTask$, noSerialize, useServerData, useSignal, untrack, sync$, isDev, withLocale, event$, useStyles$, isServer, useStore, useContextProvider, useTask$, getLocale, jsx as jsx$1, SkipRender } from "@qwik.dev/core";
3
- import { p } from "@qwik.dev/core/preloader";
4
- import { _deserialize, _UNINITIALIZED, _getContextContainer, SerializerSymbol, _getContextElement, _getQContainerElement, _waitUntilRendered, _useInvokeContext, _getContextEvent, _serialize } from "@qwik.dev/core/internal";
1
+ import { t as ensureSlash } from "./chunks/pathname.mjs";
2
+ import { A as routeLoader$, B as RouteLoaderCtxContext, E as isImmutableLoader, F as ContentContext, G as QACTION_KEY, H as RouteNavigateContext, I as ContentInternalContext, J as Q_ROUTE, K as QDATA_KEY, L as DocumentHeadContext, M as setLoaderSignalValue, P as updateRouteLoaderPaths, R as HttpStatusContext, T as invalidateNavRouteLoaders, U as RoutePreventNavigateContext, V as RouteLocationContext, W as RouteStateContext, _ as fetchRouteLoaderData, b as getRequestEvent, c as shouldPreload, d as loadRoute, g as ensureRouteLoaderSignals, i as getClientNavPath, j as routeLoaderQrl, l as toPath, m as clearNavFetchCache, n as resolveHead, o as isSameOrigin, q as QFN_KEY, s as isSamePath, t as createDocumentHead, u as toUrl, y as getModuleRouteLoaders, z as RouteActionContext } from "./chunks/head.qwik.mjs";
3
+ import { SkipRender, Slot, _captures, _fnSignal, _getConstProps, _getVarProps, _jsxSorted, _jsxSplit, _qrlSync, _restProps, _wrapProp, componentQrl, createComputedQrl, createElement, eventQrl, getLocale, implicit$FirstArg, inlinedQrl, isBrowser, isDev, isServer, noSerialize, untrack, useContext, useContextProvider, useErrorBoundary, useOnWindow, useServerData, useSignal, useStore, useTaskQrl, useVisibleTaskQrl, withLocale } from "@qwik.dev/core";
4
+ import { Fragment } from "@qwik.dev/core/jsx-runtime";
5
5
  import * as qwikRouterConfig from "@qwik-router-config";
6
+ import { p } from "@qwik.dev/core/preloader";
7
+ import { _deserialize, _getContextContainer, _getContextHostElement, _hasStoreEffects, _serialize, _waitUntilRendered, forceStoreEffects } from "@qwik.dev/core/internal";
8
+ import * as v from "valibot";
9
+ import * as z$1 from "zod";
6
10
  import { z } from "zod";
7
- import { z as z2 } from "zod";
8
11
  import swRegister from "@qwik-router-sw-register";
9
- const ErrorBoundary = component$((props) => {
10
- const store2 = useErrorBoundary();
11
- useOnWindow("qerror", $((e) => {
12
- store2.error = e.detail.error;
13
- }));
14
- if (store2.error && props.fallback$) {
15
- return /* @__PURE__ */ jsx(Fragment, {
16
- children: props.fallback$(store2.error)
17
- });
18
- }
19
- return /* @__PURE__ */ jsx(Slot, {});
20
- });
21
- const MODULE_CACHE = /* @__PURE__ */ new WeakMap();
22
- const CLIENT_DATA_CACHE = /* @__PURE__ */ new Map();
23
- const QACTION_KEY = "qaction";
24
- const QLOADER_KEY = "qloaders";
25
- const QFN_KEY = "qfunc";
26
- const QDATA_KEY = "qdata";
27
- const Q_ROUTE = "q:route";
28
- const DEFAULT_LOADERS_SERIALIZATION_STRATEGY = globalThis.__DEFAULT_LOADERS_SERIALIZATION_STRATEGY__ || "never";
29
- const MAX_Q_DATA_RETRY_COUNT = 3;
30
- const loadClientData = async (url, element, opts, retryCount = 0) => {
31
- const pagePathname = url.pathname;
32
- const pageSearch = url.search;
33
- const clientDataPath = getClientDataPath(pagePathname, pageSearch, {
34
- actionId: opts?.action?.id,
35
- loaderIds: opts?.loaderIds
36
- });
37
- let qData;
38
- if (!opts?.action) {
39
- qData = CLIENT_DATA_CACHE.get(clientDataPath);
40
- }
41
- if (opts?.prefetchSymbols !== false) {
42
- prefetchSymbols(pagePathname);
43
- }
44
- let resolveFn;
45
- if (!qData) {
46
- const fetchOptions = getFetchOptions(opts?.action, opts?.clearCache);
47
- if (opts?.action) {
48
- opts.action.data = void 0;
49
- }
50
- qData = fetch(clientDataPath, fetchOptions).then((rsp) => {
51
- if (rsp.status === 404 && opts?.loaderIds && retryCount < MAX_Q_DATA_RETRY_COUNT) {
52
- opts.loaderIds = void 0;
53
- return loadClientData(url, element, opts, retryCount + 1);
54
- }
55
- if (rsp.redirected) {
56
- const redirectedURL = new URL(rsp.url);
57
- const isQData = redirectedURL.pathname.endsWith("/q-data.json");
58
- if (!isQData || redirectedURL.origin !== location.origin) {
59
- location.href = redirectedURL.href;
60
- return;
61
- }
62
- }
63
- if ((rsp.headers.get("content-type") || "").includes("json")) {
64
- return rsp.text().then((text) => {
65
- const [clientData] = _deserialize(text, element);
66
- if (!clientData) {
67
- location.href = url.href;
68
- return;
69
- }
70
- if (opts?.clearCache) {
71
- CLIENT_DATA_CACHE.delete(clientDataPath);
72
- }
73
- if (clientData.redirect) {
74
- location.href = clientData.redirect;
75
- } else if (opts?.action) {
76
- const { action } = opts;
77
- const actionData = clientData.loaders[action.id];
78
- resolveFn = () => {
79
- action.resolve({
80
- status: rsp.status,
81
- result: actionData
82
- });
83
- };
84
- }
85
- return clientData;
86
- });
87
- } else {
88
- if (opts?.isPrefetch !== true) {
89
- location.href = url.href;
90
- }
91
- return void 0;
92
- }
93
- });
94
- if (!opts?.action) {
95
- CLIENT_DATA_CACHE.set(clientDataPath, qData);
96
- }
97
- }
98
- return qData.then((v) => {
99
- if (!v) {
100
- CLIENT_DATA_CACHE.delete(clientDataPath);
101
- }
102
- resolveFn && resolveFn();
103
- return v;
104
- });
12
+ import { renderToStream } from "@qwik.dev/core/server";
13
+ /** @public */ var ErrorBoundary = /*#__PURE__*/ componentQrl(/*#__PURE__*/ inlinedQrl((props) => {
14
+ const store = useErrorBoundary();
15
+ useOnWindow("qerror", /*#__PURE__*/ inlinedQrl((e) => {
16
+ const store = _captures[0];
17
+ store.error = e.detail.error;
18
+ }, "ErrorBoundary_component_useOnWindow_G0jFRpoNY0M", [store]));
19
+ if (store.error && props.fallback$) return /*#__PURE__*/ _jsxSorted(Fragment, null, null, props.fallback$(store.error), 1, "bA_0");
20
+ return /*#__PURE__*/ _jsxSorted(Slot, null, null, null, 3, "bA_1");
21
+ }, "ErrorBoundary_component_pOa6vjtC7ik"));
22
+ /**
23
+ * Prefetch a route's loader data in parallel (fire-and-forget). The per-nav fetch map dedupes
24
+ * repeat hovers and shares the promise with the navigation's own fetch; the browser HTTP cache
25
+ * handles freshness. Immutable loaders are skipped: they download lazily on first actual read and
26
+ * are then browser-cached for the life of the deploy.
27
+ */ var prefetchLoaderData = (loadedRoute, url, manifestHash) => {
28
+ if (!loadedRoute.$loaders$?.length || !loadedRoute.$loaderPaths$) return;
29
+ const immutableIds = new Set(getModuleRouteLoaders(loadedRoute.$mods$).filter((loader) => loader.__cacheControl === "immutable").map((loader) => loader.__id));
30
+ const basePath = qwikRouterConfig.basePathname ?? "/";
31
+ for (const hash of loadedRoute.$loaders$) {
32
+ if (immutableIds.has(hash)) continue;
33
+ let loaderPath = loadedRoute.$loaderPaths$[hash];
34
+ if (!loaderPath) continue;
35
+ if (basePath !== "/" && !loaderPath.startsWith(basePath)) loaderPath = basePath + loaderPath.slice(1);
36
+ fetchRouteLoaderData(hash, loaderPath, manifestHash, {
37
+ pageUrl: url,
38
+ basePath
39
+ }).catch(() => {});
40
+ }
105
41
  };
106
- const getFetchOptions = (action, noCache) => {
107
- const actionData = action?.data;
108
- if (!actionData) {
109
- if (noCache) {
110
- return {
111
- cache: "no-cache",
112
- headers: {
113
- "Cache-Control": "no-cache",
114
- Pragma: "no-cache"
115
- }
116
- };
117
- }
118
- return void 0;
119
- }
120
- if (actionData instanceof FormData) {
121
- return {
122
- method: "POST",
123
- body: actionData
124
- };
125
- } else {
126
- return {
127
- method: "POST",
128
- body: JSON.stringify(actionData),
129
- headers: {
130
- "Content-Type": "application/json, charset=UTF-8"
131
- }
132
- };
133
- }
134
- };
135
- const toPath = (url) => url.pathname + url.search + url.hash;
136
- const toUrl = (url, baseUrl) => new URL(url, baseUrl.href);
137
- const isSameOrigin = (a, b) => a.origin === b.origin;
138
- const withSlash = (path) => path.endsWith("/") ? path : path + "/";
139
- const isSamePathname = ({ pathname: a }, { pathname: b }) => {
140
- const lDiff = Math.abs(a.length - b.length);
141
- return lDiff === 0 ? a === b : lDiff === 1 && withSlash(a) === withSlash(b);
42
+ /**
43
+ * Prefetch a route's JS bundles and optionally its loader data.
44
+ *
45
+ * Resolves the route from the trie to get the routeName (for the bundle graph preloader) and
46
+ * `$loaders$` (for data prefetching). The bundle graph is keyed by route name (e.g.
47
+ * `products/[id]/`), not by actual pathname (e.g. `products/123/`).
48
+ *
49
+ * @param url - The URL pathname to prefetch
50
+ * @param prefetchData - Whether to prefetch loader data
51
+ * @param probability - Bundle preload probability (0-1, default 0.8)
52
+ * @param manifestHash - Build manifest hash for loader URLs (from `useDocumentHead().manifestHash`)
53
+ * @param prefetchBundle - Whether to prefetch route JS bundles
54
+ */ async function prefetchRoute(url, prefetchData, probability = .8, manifestHash, prefetchBundle = true) {
55
+ if (!isBrowser || isDev) return;
56
+ try {
57
+ const loadedRoute = await loadRoute(qwikRouterConfig.routes, qwikRouterConfig.cacheModules, url.pathname);
58
+ if (!loadedRoute) return;
59
+ if (prefetchBundle) {
60
+ let routeName = loadedRoute.$routeName$;
61
+ routeName = ensureSlash(routeName);
62
+ if (routeName.length > 1 && routeName.startsWith("/")) routeName = routeName.slice(1);
63
+ p(routeName, probability);
64
+ }
65
+ if (!prefetchData || !manifestHash) return;
66
+ prefetchLoaderData(loadedRoute, url, manifestHash);
67
+ } catch {}
68
+ }
69
+ /** @public */ var useHttpStatus = () => useContext(HttpStatusContext).value;
70
+ /** @public */ var useContent = () => useContext(ContentContext);
71
+ /**
72
+ * Returns the document head for the current page. The generic type describes the front matter.
73
+ *
74
+ * @public
75
+ */ var useDocumentHead = () => useContext(DocumentHeadContext);
76
+ /** @public */ var useLocation = () => useContext(RouteLocationContext);
77
+ /** @public */ var useNavigate = () => useContext(RouteNavigateContext);
78
+ /** @internal Implementation of usePreventNavigate$ */ var usePreventNavigateQrl = (fn) => {
79
+ const registerPreventNav = useContext(RoutePreventNavigateContext);
80
+ useVisibleTaskQrl(/*#__PURE__*/ inlinedQrl(() => {
81
+ const fn = _captures[0], registerPreventNav = _captures[1];
82
+ return registerPreventNav(fn);
83
+ }, "usePreventNavigateQrl_useVisibleTask_BsYUaz9XhUs", [fn, registerPreventNav]));
142
84
  };
143
- const isSameSearchQuery = (a, b) => a.search === b.search;
144
- const isSamePath = (a, b) => isSameSearchQuery(a, b) && isSamePathname(a, b);
145
- const getClientDataPath = (pathname, pageSearch, options) => {
146
- let search = pageSearch ?? "";
147
- if (options?.actionId) {
148
- search += (search ? "&" : "?") + QACTION_KEY + "=" + encodeURIComponent(options.actionId);
149
- }
150
- if (options?.loaderIds) {
151
- for (const loaderId of options.loaderIds) {
152
- search += (search ? "&" : "?") + QLOADER_KEY + "=" + encodeURIComponent(loaderId);
153
- }
154
- }
155
- return pathname + (pathname.endsWith("/") ? "" : "/") + "q-data.json" + search;
85
+ /**
86
+ * Prevent navigation attempts. This hook registers a callback that will be called before SPA or
87
+ * browser navigation.
88
+ *
89
+ * Return `true` to prevent navigation.
90
+ *
91
+ * #### SPA Navigation
92
+ *
93
+ * For Single-Page-App (SPA) navigation (via `<Link />`, `const nav = useNavigate()`, and browser
94
+ * backwards/forwards inside SPA history), the callback will be provided with the target, either a
95
+ * URL or a number. It will only be a number if `nav(number)` was called to navigate forwards or
96
+ * backwards in SPA history.
97
+ *
98
+ * If you return a Promise, the navigation will be blocked until the promise resolves.
99
+ *
100
+ * This can be used to show a nice dialog to the user, and wait for the user to confirm, or to
101
+ * record the url, prevent the navigation, and navigate there later via `nav(url)`.
102
+ *
103
+ * #### Browser Navigation
104
+ *
105
+ * However, when the user navigates away by clicking on a regular `<a />`, reloading, or moving
106
+ * backwards/forwards outside SPA history, this callback will not be awaited. This is because the
107
+ * browser does not provide a way to asynchronously prevent these navigations.
108
+ *
109
+ * In this case, returning returning `true` will tell the browser to show a confirmation dialog,
110
+ * which cannot be customized. You are also not able to show your own `window.confirm()` dialog
111
+ * during the callback, the browser won't allow it. If you return a Promise, it will be considered
112
+ * as `true`.
113
+ *
114
+ * When the callback is called from the browser, no url will be provided. Use this to know whether
115
+ * you can show a dialog or just return `true` to prevent the navigation.
116
+ *
117
+ * @public
118
+ */ var usePreventNavigate$ = implicit$FirstArg(usePreventNavigateQrl);
119
+ var useAction = () => useContext(RouteActionContext);
120
+ var useQwikRouterEnv = () => noSerialize(useServerData("qwikrouter"));
121
+ /** @public */ var Link = /*#__PURE__*/ componentQrl(/*#__PURE__*/ inlinedQrl((props) => {
122
+ const nav = useNavigate();
123
+ const loc = useLocation();
124
+ const head = useDocumentHead();
125
+ const originalHref = props.href;
126
+ const { onClick$, prefetch: prefetchProp, reload, replaceState, scroll, prefetchBundles: prefetchBundlesProp = "visible", prefetchData: prefetchDataProp = prefetchProp === "js" ? "off" : "intent", ...linkProps } = props;
127
+ const clientNavPath = untrack(getClientNavPath, {
128
+ ...linkProps,
129
+ reload
130
+ }, loc);
131
+ linkProps.href = clientNavPath || originalHref;
132
+ const isDepratedPrefetchDisabled = prefetchProp === false;
133
+ const shouldPrefetch = untrack(shouldPreload, clientNavPath, loc);
134
+ const shouldVisiblePrefetchBundles = !!clientNavPath && shouldPrefetch && !isDepratedPrefetchDisabled && (prefetchBundlesProp === "visible" || prefetchProp === "js" || prefetchProp === true);
135
+ const shouldVisiblePrefetchData = !!clientNavPath && shouldPrefetch && !isDepratedPrefetchDisabled && (prefetchDataProp === "visible" || prefetchProp === true);
136
+ const handleDataPrefetch = !!clientNavPath && prefetchDataProp !== "off" && shouldPrefetch && !isDepratedPrefetchDisabled ? /*#__PURE__*/ inlinedQrl((_, elm) => {
137
+ const head = _captures[0];
138
+ if (navigator.connection?.saveData) return;
139
+ if (elm && elm.href) prefetchRoute(new URL(elm.href), true, .8, head.manifestHash, false);
140
+ }, "Link_component_handleDataPrefetch_lcd4O9LHbbA", [head]) : null;
141
+ const preventDefault = clientNavPath ? _qrlSync((event) => {
142
+ if (!(event.metaKey || event.ctrlKey || event.shiftKey || event.altKey)) event.preventDefault();
143
+ }, "event=>{if(!(event.metaKey||event.ctrlKey||event.shiftKey||event.altKey)){event.preventDefault();}}") : null;
144
+ const prefetchData = /*#__PURE__*/ inlinedQrl(async (_, elm) => {
145
+ const handleDataPrefetch = _captures[0];
146
+ handleDataPrefetch?.(null, elm);
147
+ }, "Link_component_prefetchData_UwVx4NhHkSo", [handleDataPrefetch]);
148
+ const onEnterKeyDown = /*#__PURE__*/ inlinedQrl((event, element) => {
149
+ const prefetchData = _captures[0];
150
+ if (event.key === "Enter") prefetchData(null, element);
151
+ }, "Link_component_onEnterKeyDown_z274ws00irY", [prefetchData]);
152
+ const handleClientSideNavigation = clientNavPath ? /*#__PURE__*/ inlinedQrl((event, elm) => {
153
+ const nav = _captures[0], reload = _captures[1], replaceState = _captures[2], scroll = _captures[3];
154
+ if (event.defaultPrevented) {
155
+ if (elm.href) {
156
+ elm.setAttribute("aria-pressed", "true");
157
+ nav(elm.href, {
158
+ forceReload: reload,
159
+ replaceState,
160
+ scroll
161
+ }).then(() => {
162
+ elm.removeAttribute("aria-pressed");
163
+ });
164
+ }
165
+ }
166
+ }, "Link_component_handleClientSideNavigation_h3qenoGeI6M", [
167
+ nav,
168
+ reload,
169
+ replaceState,
170
+ scroll
171
+ ]) : null;
172
+ const handlePreload = /*#__PURE__*/ inlinedQrl((_, elm) => {
173
+ prefetchRoute(new URL(elm.href), false, 1);
174
+ }, "Link_component_handlePreload_AAemwtuBjsE");
175
+ return /*#__PURE__*/ _jsxSplit("a", {
176
+ "q:link": !!clientNavPath,
177
+ ..._getVarProps(linkProps),
178
+ ..._getConstProps(linkProps),
179
+ "data-q-prefetch": shouldVisiblePrefetchBundles && shouldVisiblePrefetchData ? "bd" : shouldVisiblePrefetchBundles ? "b" : shouldVisiblePrefetchData ? "d" : null,
180
+ "q-e:click": [
181
+ preventDefault,
182
+ handlePreload,
183
+ onClick$,
184
+ handleClientSideNavigation
185
+ ],
186
+ "q-e:pointerenter": [linkProps.onMouseOver$, prefetchDataProp === "intent" ? prefetchData : null],
187
+ "q-e:focus": [linkProps.onFocus$, prefetchDataProp === "intent" ? prefetchData : null],
188
+ "q-e:pointerdown": [linkProps.onPointerDown$, prefetchDataProp === "commit" ? prefetchData : null],
189
+ "q-e:keydown": [linkProps.onKeyDown$, prefetchDataProp === "commit" ? onEnterKeyDown : null]
190
+ }, null, /*#__PURE__*/ _jsxSorted(Slot, null, null, null, 3, "jO_0"), 0, "jO_1");
191
+ }, "Link_component_VPmar9tb3t4"));
192
+ var newScrollState = () => {
193
+ return {
194
+ x: 0,
195
+ y: 0,
196
+ w: 0,
197
+ h: 0
198
+ };
156
199
  };
157
- const getClientNavPath = (props, baseUrl) => {
158
- const href = props.href;
159
- if (typeof href === "string" && typeof props.target !== "string" && !props.reload) {
160
- try {
161
- const linkUrl = toUrl(href.trim(), baseUrl.url);
162
- const currentUrl = toUrl("", baseUrl.url);
163
- if (isSameOrigin(linkUrl, currentUrl)) {
164
- return toPath(linkUrl);
165
- }
166
- } catch (e) {
167
- console.error(e);
168
- }
169
- } else if (props.reload) {
170
- return toPath(toUrl("", baseUrl.url));
171
- }
172
- return null;
200
+ var clientNavigate = (win, navType, fromURL, toURL, replaceState = false) => {
201
+ if (navType !== "popstate") {
202
+ const samePath = isSamePath(fromURL, toURL);
203
+ const sameHash = fromURL.hash === toURL.hash;
204
+ if (!samePath || !sameHash) {
205
+ const newState = { _qRouterScroll: newScrollState() };
206
+ if (replaceState) win.history.replaceState(newState, "", toPath(toURL));
207
+ else win.history.pushState(newState, "", toPath(toURL));
208
+ }
209
+ }
173
210
  };
174
- const shouldPreload = (clientNavPath, currentLoc) => {
175
- if (clientNavPath) {
176
- const prefetchUrl = toUrl(clientNavPath, currentLoc.url);
177
- const currentUrl = toUrl("", currentLoc.url);
178
- return !isSamePathname(prefetchUrl, currentUrl);
179
- }
180
- return false;
211
+ var preloadRouteBundles = (path, probability = .8) => {
212
+ if (isBrowser) {
213
+ path = ensureSlash(path);
214
+ path = path.length > 1 && path.startsWith("/") ? path.slice(1) : path;
215
+ p(path, probability);
216
+ }
181
217
  };
182
- const isPromise = (value) => {
183
- return value && typeof value.then === "function";
218
+ var prefetchedLinks = /* @__PURE__ */ new WeakSet();
219
+ var cleanupPrefetchObserver;
220
+ var resetLinkPrefetchState = () => {
221
+ prefetchedLinks = /* @__PURE__ */ new WeakSet();
184
222
  };
185
- const deepFreeze = (obj) => {
186
- if (obj == null) {
187
- return obj;
188
- }
189
- Object.getOwnPropertyNames(obj).forEach((prop) => {
190
- const value = obj[prop];
191
- if (value && typeof value === "object" && !Object.isFrozen(value)) {
192
- deepFreeze(value);
193
- }
194
- });
195
- return Object.freeze(obj);
223
+ var createLinkPrefetchObserver = (manifestHash) => {
224
+ const anchors = document.querySelectorAll("a[q\\:link][data-q-prefetch]");
225
+ const prefetchAnchor = (anchor, observer) => {
226
+ if (prefetchedLinks.has(anchor)) return;
227
+ prefetchedLinks.add(anchor);
228
+ observer?.unobserve(anchor);
229
+ if (navigator.connection?.saveData || !anchor.href) return;
230
+ let url;
231
+ try {
232
+ url = new URL(anchor.href);
233
+ } catch {
234
+ return;
235
+ }
236
+ const currentUrl = new URL(location.href);
237
+ if (!isSameOrigin(url, currentUrl) || !shouldPreload(toPath(url), { url: currentUrl })) return;
238
+ const mode = anchor.getAttribute("data-q-prefetch") || "";
239
+ if (mode.includes("b")) preloadRouteBundles(url.pathname);
240
+ if (mode.includes("d")) prefetchRoute(url, true, .8, manifestHash, false);
241
+ };
242
+ if (typeof IntersectionObserver === "undefined") {
243
+ anchors.forEach((anchor) => prefetchAnchor(anchor));
244
+ return () => {};
245
+ }
246
+ const observer = new IntersectionObserver((entries) => {
247
+ for (const entry of entries) if (entry.isIntersecting) prefetchAnchor(entry.target, observer);
248
+ });
249
+ anchors.forEach((anchor) => observer.observe(anchor));
250
+ return () => {
251
+ observer.disconnect();
252
+ };
196
253
  };
197
- const createLoaderSignal = (loadersObject, loaderId, url, serializationStrategy, container) => {
198
- return createAsyncComputed$(async () => {
199
- if (isBrowser && loadersObject[loaderId] === _UNINITIALIZED) {
200
- const data = await loadClientData(url, void 0, {
201
- loaderIds: [
202
- loaderId
203
- ]
204
- });
205
- loadersObject[loaderId] = data?.loaders[loaderId] ?? _UNINITIALIZED;
206
- }
207
- return loadersObject[loaderId];
208
- }, {
209
- container,
210
- serializationStrategy
211
- });
254
+ var refreshLinkPrefetchObserver = (manifestHash) => {
255
+ resetLinkPrefetchState();
256
+ cleanupPrefetchObserver?.();
257
+ cleanupPrefetchObserver = createLinkPrefetchObserver(manifestHash);
212
258
  };
213
- const clientNavigate = (win, navType, fromURL, toURL, replaceState = false) => {
214
- if (navType !== "popstate") {
215
- const samePath = isSamePath(fromURL, toURL);
216
- const sameHash = fromURL.hash === toURL.hash;
217
- if (!samePath || !sameHash) {
218
- const newState = {
219
- _qRouterScroll: newScrollState()
220
- };
221
- if (replaceState) {
222
- win.history.replaceState(newState, "", toPath(toURL));
223
- } else {
224
- win.history.pushState(newState, "", toPath(toURL));
225
- }
226
- }
227
- }
259
+ var linkPrefetchInit = eventQrl(/*#__PURE__*/ inlinedQrl((_, element) => {
260
+ refreshLinkPrefetchObserver(element.closest("[q\\:manifest-hash]")?.getAttribute("q:manifest-hash") || void 0);
261
+ }, "linkPrefetchInit_event_hoZr9O26Irg"));
262
+ function callRestoreScrollOnDocument() {
263
+ if (document.__q_scroll_restore__) {
264
+ document.__q_scroll_restore__();
265
+ document.__q_scroll_restore__ = void 0;
266
+ }
267
+ }
268
+ var hashScroll = (toUrl, fromUrl) => {
269
+ const elmId = toUrl.hash.slice(1);
270
+ const elm = elmId && document.getElementById(elmId);
271
+ if (elm) {
272
+ elm.scrollIntoView();
273
+ return true;
274
+ } else if (!elm && toUrl.hash && isSamePath(toUrl, fromUrl)) return true;
275
+ return false;
228
276
  };
229
- const newScrollState = () => {
230
- return {
231
- x: 0,
232
- y: 0,
233
- w: 0,
234
- h: 0
235
- };
277
+ var restoreScroll = (type, toUrl, fromUrl, scroller, scrollState) => {
278
+ if (type === "popstate" && scrollState) scroller.scrollTo(scrollState.x, scrollState.y);
279
+ else if (type === "link" || type === "form") {
280
+ if (!hashScroll(toUrl, fromUrl)) scroller.scrollTo(0, 0);
281
+ }
236
282
  };
237
- const prefetchSymbols = (path) => {
238
- if (isBrowser) {
239
- path = path.endsWith("/") ? path : path + "/";
240
- path = path.length > 1 && path.startsWith("/") ? path.slice(1) : path;
241
- p(path, 0.8);
242
- }
283
+ var currentScrollState = (elm) => {
284
+ return {
285
+ x: elm.scrollLeft,
286
+ y: elm.scrollTop,
287
+ w: Math.max(elm.scrollWidth, elm.clientWidth),
288
+ h: Math.max(elm.scrollHeight, elm.clientHeight)
289
+ };
243
290
  };
244
- const RouteStateContext = /* @__PURE__ */ createContextId("qc-s");
245
- const ContentContext = /* @__PURE__ */ createContextId("qc-c");
246
- const ContentInternalContext = /* @__PURE__ */ createContextId("qc-ic");
247
- const DocumentHeadContext = /* @__PURE__ */ createContextId("qc-h");
248
- const RouteLocationContext = /* @__PURE__ */ createContextId("qc-l");
249
- const RouteNavigateContext = /* @__PURE__ */ createContextId("qc-n");
250
- const RouteActionContext = /* @__PURE__ */ createContextId("qc-a");
251
- const RouteInternalContext = /* @__PURE__ */ createContextId("qc-ir");
252
- const RoutePreventNavigateContext = /* @__PURE__ */ createContextId("qc-p");
253
- const useContent = () => useContext(ContentContext);
254
- const useDocumentHead = () => useContext(DocumentHeadContext);
255
- const useLocation = () => useContext(RouteLocationContext);
256
- const useNavigate = () => useContext(RouteNavigateContext);
257
- const usePreventNavigateQrl = (fn) => {
258
- if (!__EXPERIMENTAL__.preventNavigate) {
259
- throw new Error('usePreventNavigate$ is experimental and must be enabled with `experimental: ["preventNavigate"]` in the `qwikVite` plugin.');
260
- }
261
- const registerPreventNav = useContext(RoutePreventNavigateContext);
262
- useVisibleTask$(() => registerPreventNav(fn));
291
+ var getScrollHistory = () => {
292
+ return history.state?._qRouterScroll;
263
293
  };
264
- const usePreventNavigate$ = implicit$FirstArg(usePreventNavigateQrl);
265
- const useAction = () => useContext(RouteActionContext);
266
- const useQwikRouterEnv = () => noSerialize(useServerData("qwikrouter"));
267
- const Link = component$((props) => {
268
- const nav = useNavigate();
269
- const loc = useLocation();
270
- const originalHref = props.href;
271
- const anchorRef = useSignal();
272
- const { onClick$, prefetch: prefetchProp, reload, replaceState, scroll, ...linkProps } = /* @__PURE__ */ (() => props)();
273
- const clientNavPath = untrack(() => getClientNavPath({
274
- ...linkProps,
275
- reload
276
- }, loc));
277
- linkProps.href = clientNavPath || originalHref;
278
- const prefetchData = untrack(() => !!clientNavPath && prefetchProp !== false && prefetchProp !== "js" || void 0);
279
- const prefetch = untrack(() => prefetchData || !!clientNavPath && prefetchProp !== false && shouldPreload(clientNavPath, loc));
280
- const handlePrefetch = prefetch ? $((_, elm) => {
281
- if (navigator.connection?.saveData) {
282
- return;
283
- }
284
- if (elm && elm.href) {
285
- const url = new URL(elm.href);
286
- prefetchSymbols(url.pathname);
287
- if (elm.hasAttribute("data-prefetch")) {
288
- loadClientData(url, elm, {
289
- prefetchSymbols: false,
290
- isPrefetch: true
291
- });
292
- }
293
- }
294
- }) : void 0;
295
- const preventDefault = clientNavPath ? sync$((event) => {
296
- if (!(event.metaKey || event.ctrlKey || event.shiftKey || event.altKey)) {
297
- event.preventDefault();
298
- }
299
- }) : void 0;
300
- const handleClick = clientNavPath ? $(async (event, elm) => {
301
- if (event.defaultPrevented) {
302
- if (elm.href) {
303
- elm.setAttribute("aria-pressed", "true");
304
- await nav(elm.href, {
305
- forceReload: reload,
306
- replaceState,
307
- scroll
308
- });
309
- elm.removeAttribute("aria-pressed");
310
- }
311
- }
312
- }) : void 0;
313
- useVisibleTask$(({ track }) => {
314
- track(() => loc.url.pathname);
315
- const handler = linkProps.onQVisible$;
316
- if (handler) {
317
- const event = new CustomEvent("qvisible");
318
- if (Array.isArray(handler)) {
319
- handler.flat(10).forEach((handler2) => handler2?.(event, anchorRef.value));
320
- } else {
321
- handler?.(event, anchorRef.value);
322
- }
323
- }
324
- if (!isDev && anchorRef.value) {
325
- handlePrefetch?.(void 0, anchorRef.value);
326
- }
327
- });
328
- return /* @__PURE__ */ jsx("a", {
329
- ref: anchorRef,
330
- "q:link": !!clientNavPath,
331
- ...linkProps,
332
- onClick$: [
333
- preventDefault,
334
- onClick$,
335
- handleClick
336
- ],
337
- "data-prefetch": prefetchData,
338
- onMouseOver$: [
339
- linkProps.onMouseOver$,
340
- handlePrefetch
341
- ],
342
- onFocus$: [
343
- linkProps.onFocus$,
344
- handlePrefetch
345
- ],
346
- // We need to prevent the onQVisible$ from being called twice since it is handled in the visible task
347
- onQVisible$: [],
348
- children: /* @__PURE__ */ jsx(Slot, {})
349
- });
350
- });
351
- const resolveHead = (endpoint, routeLocation, contentModules, locale) => {
352
- const head = createDocumentHead();
353
- const getData = (loaderOrAction) => {
354
- const id = loaderOrAction.__id;
355
- if (loaderOrAction.__brand === "server_loader") {
356
- if (!(id in endpoint.loaders)) {
357
- throw new Error("You can not get the returned data of a loader that has not been executed for this request.");
358
- }
359
- }
360
- const data = endpoint.loaders[id];
361
- if (isPromise(data)) {
362
- throw new Error("Loaders returning a promise can not be resolved for the head function.");
363
- }
364
- return data;
365
- };
366
- const headProps = {
367
- head,
368
- withLocale: (fn) => withLocale(locale, fn),
369
- resolveValue: getData,
370
- ...routeLocation
371
- };
372
- for (let i = contentModules.length - 1; i >= 0; i--) {
373
- const contentModuleHead = contentModules[i] && contentModules[i].head;
374
- if (contentModuleHead) {
375
- if (typeof contentModuleHead === "function") {
376
- resolveDocumentHead(head, withLocale(locale, () => contentModuleHead(headProps)));
377
- } else if (typeof contentModuleHead === "object") {
378
- resolveDocumentHead(head, contentModuleHead);
379
- }
380
- }
381
- }
382
- return headProps.head;
294
+ var saveScrollHistory = (scrollState) => {
295
+ const state = history.state || {};
296
+ state._qRouterScroll = scrollState;
297
+ history.replaceState(state, "");
383
298
  };
384
- const resolveDocumentHead = (resolvedHead, updatedHead) => {
385
- if (typeof updatedHead.title === "string") {
386
- resolvedHead.title = updatedHead.title;
387
- }
388
- mergeArray(resolvedHead.meta, updatedHead.meta);
389
- mergeArray(resolvedHead.links, updatedHead.links);
390
- mergeArray(resolvedHead.styles, updatedHead.styles);
391
- mergeArray(resolvedHead.scripts, updatedHead.scripts);
392
- Object.assign(resolvedHead.frontmatter, updatedHead.frontmatter);
299
+ var Q_ROUTER_POPSTATE_EVENT = "qrouterpopstate";
300
+ var createCurrentPathTracker = (initialPath) => {
301
+ let currentPath = initialPath;
302
+ return (nextPath) => {
303
+ if (currentPath !== nextPath) {
304
+ currentPath = nextPath;
305
+ return true;
306
+ }
307
+ return false;
308
+ };
393
309
  };
394
- const mergeArray = (existingArr, newArr) => {
395
- if (Array.isArray(newArr)) {
396
- for (const newItem of newArr) {
397
- if (typeof newItem.key === "string") {
398
- const existingIndex = existingArr.findIndex((i) => i.key === newItem.key);
399
- if (existingIndex > -1) {
400
- existingArr[existingIndex] = newItem;
401
- continue;
402
- }
403
- }
404
- existingArr.push(newItem);
405
- }
406
- }
310
+ var dispatchRouterPopstate = (document1, href) => {
311
+ document1.dispatchEvent(new CustomEvent(Q_ROUTER_POPSTATE_EVENT, { detail: { href } }));
407
312
  };
408
- const createDocumentHead = () => ({
409
- title: "",
410
- meta: [],
411
- links: [],
412
- styles: [],
413
- scripts: [],
414
- frontmatter: {}
415
- });
416
- function matchRoute(route, path) {
417
- const routeIdx = startIdxSkipSlash(route);
418
- const routeLength = lengthNoTrailingSlash(route);
419
- const pathIdx = startIdxSkipSlash(path);
420
- const pathLength = lengthNoTrailingSlash(path);
421
- return matchRoutePart(route, routeIdx, routeLength, path, pathIdx, pathLength);
422
- }
423
- function matchRoutePart(route, routeIdx, routeLength, path, pathIdx, pathLength) {
424
- let params = null;
425
- while (routeIdx < routeLength) {
426
- const routeCh = route.charCodeAt(routeIdx++);
427
- const pathCh = path.charCodeAt(pathIdx++);
428
- if (routeCh === 91) {
429
- const isMany = isThreeDots(route, routeIdx);
430
- const paramNameStart = routeIdx + (isMany ? 3 : 0);
431
- const paramNameEnd = scan(route, paramNameStart, routeLength, 93);
432
- const paramName = route.substring(paramNameStart, paramNameEnd);
433
- const paramSuffixEnd = scan(route, paramNameEnd + 1, routeLength, 47);
434
- const suffix = route.substring(paramNameEnd + 1, paramSuffixEnd);
435
- routeIdx = paramNameEnd + 1;
436
- const paramValueStart = pathIdx - 1;
437
- if (isMany) {
438
- const match = recursiveScan(paramName, suffix, path, paramValueStart, pathLength, route, routeIdx + suffix.length + 1, routeLength);
439
- if (match) {
440
- return Object.assign(params || (params = {}), match);
441
- }
442
- }
443
- const paramValueEnd = scan(path, paramValueStart, pathLength, 47, suffix);
444
- if (paramValueEnd == -1) {
445
- return null;
446
- }
447
- const paramValue = path.substring(paramValueStart, paramValueEnd);
448
- if (!isMany && !suffix && !paramValue) {
449
- return null;
450
- }
451
- pathIdx = paramValueEnd;
452
- (params || (params = {}))[paramName] = decodeURIComponent(paramValue);
453
- } else if (routeCh !== pathCh) {
454
- if (!(isNaN(pathCh) && isRestParameter(route, routeIdx))) {
455
- return null;
456
- }
457
- }
458
- }
459
- if (allConsumed(route, routeIdx) && allConsumed(path, pathIdx)) {
460
- return params || {};
461
- } else {
462
- return null;
463
- }
464
- }
465
- function isRestParameter(text, idx) {
466
- return text.charCodeAt(idx) === 91 && isThreeDots(text, idx + 1);
467
- }
468
- function lengthNoTrailingSlash(text) {
469
- const length = text.length;
470
- return length > 1 && text.charCodeAt(length - 1) === 47 ? length - 1 : length;
471
- }
472
- function allConsumed(text, idx) {
473
- const length = text.length;
474
- return idx >= length || idx == length - 1 && text.charCodeAt(idx) === 47;
475
- }
476
- function startIdxSkipSlash(text) {
477
- return text.charCodeAt(0) === 47 ? 1 : 0;
478
- }
479
- function isThreeDots(text, idx) {
480
- return text.charCodeAt(idx) === 46 && text.charCodeAt(idx + 1) === 46 && text.charCodeAt(idx + 2) === 46;
481
- }
482
- function scan(text, idx, end, ch, suffix = "") {
483
- while (idx < end && text.charCodeAt(idx) !== ch) {
484
- idx++;
485
- }
486
- const suffixLength = suffix.length;
487
- for (let i = 0; i < suffixLength; i++) {
488
- if (text.charCodeAt(idx - suffixLength + i) !== suffix.charCodeAt(i)) {
489
- return -1;
490
- }
491
- }
492
- return idx - suffixLength;
493
- }
494
- function recursiveScan(paramName, suffix, path, pathStart, pathLength, route, routeStart, routeLength) {
495
- if (path.charCodeAt(pathStart) === 47) {
496
- pathStart++;
497
- }
498
- let pathIdx = pathLength;
499
- const sep = suffix + "/";
500
- while (pathIdx >= pathStart) {
501
- const match = matchRoutePart(route, routeStart, routeLength, path, pathIdx, pathLength);
502
- if (match) {
503
- let value = path.substring(pathStart, Math.min(pathIdx, pathLength));
504
- if (value.endsWith(sep)) {
505
- value = value.substring(0, value.length - sep.length);
506
- }
507
- match[paramName] = decodeURIComponent(value);
508
- return match;
509
- }
510
- const newPathIdx = lastIndexOf(path, pathStart, sep, pathIdx, pathStart - 1) + sep.length;
511
- if (pathIdx === newPathIdx) {
512
- break;
513
- }
514
- pathIdx = newPathIdx;
515
- }
516
- return null;
517
- }
518
- function lastIndexOf(text, start, match, searchIdx, notFoundIdx) {
519
- let idx = text.lastIndexOf(match, searchIdx);
520
- if (idx == searchIdx - match.length) {
521
- idx = text.lastIndexOf(match, searchIdx - match.length - 1);
522
- }
523
- return idx > start ? idx : notFoundIdx;
313
+ var spa_init_default = eventQrl(/*#__PURE__*/ inlinedQrl((_, el) => {
314
+ if (!window._qRouterSPA && !window._qRouterInitPopstate) {
315
+ const hasPathChanged = createCurrentPathTracker(location.pathname + location.search);
316
+ const checkAndScroll = (scrollState) => {
317
+ if (scrollState) window.scrollTo(scrollState.x, scrollState.y);
318
+ };
319
+ const currentScrollState = () => {
320
+ const elm = document.documentElement;
321
+ return {
322
+ x: elm.scrollLeft,
323
+ y: elm.scrollTop,
324
+ w: Math.max(elm.scrollWidth, elm.clientWidth),
325
+ h: Math.max(elm.scrollHeight, elm.clientHeight)
326
+ };
327
+ };
328
+ const saveScrollState = (scrollState) => {
329
+ const state = history.state || {};
330
+ state._qRouterScroll = scrollState || currentScrollState();
331
+ history.replaceState(state, "");
332
+ };
333
+ saveScrollState();
334
+ window._qRouterInitPopstate = () => {
335
+ if (window._qRouterSPA) return;
336
+ window._qRouterScrollEnabled = false;
337
+ clearTimeout(window._qRouterScrollDebounce);
338
+ if (hasPathChanged(location.pathname + location.search)) dispatchRouterPopstate(document, location.href);
339
+ else if (history.scrollRestoration === "manual") {
340
+ const scrollState = history.state?._qRouterScroll;
341
+ checkAndScroll(scrollState);
342
+ window._qRouterScrollEnabled = true;
343
+ }
344
+ };
345
+ if (!window._qRouterHistoryPatch) {
346
+ window._qRouterHistoryPatch = true;
347
+ const pushState = history.pushState;
348
+ const replaceState = history.replaceState;
349
+ const prepareState = (state) => {
350
+ if (state === null || typeof state === "undefined") state = {};
351
+ else if (state?.constructor !== Object) {
352
+ state = { _data: state };
353
+ if (isDev) console.warn("In a Qwik SPA context, `history.state` is used to store scroll state. Direct calls to `pushState()` and `replaceState()` must supply an actual Object type. We need to be able to automatically attach the scroll state to your state object. A new state object has been created, your data has been moved to: `history.state._data`");
354
+ }
355
+ state._qRouterScroll = state._qRouterScroll || currentScrollState();
356
+ return state;
357
+ };
358
+ history.pushState = (state, title, url) => {
359
+ state = prepareState(state);
360
+ return pushState.call(history, state, title, url);
361
+ };
362
+ history.replaceState = (state, title, url) => {
363
+ state = prepareState(state);
364
+ return replaceState.call(history, state, title, url);
365
+ };
366
+ }
367
+ window._qRouterInitAnchors = (event) => {
368
+ if (window._qRouterSPA || event.defaultPrevented) return;
369
+ const target = event.target.closest("a[href]");
370
+ if (target && !target.hasAttribute("preventdefault:click")) {
371
+ const href = target.getAttribute("href");
372
+ const prev = new URL(location.href);
373
+ const dest = new URL(href, prev);
374
+ const sameOrigin = dest.origin === prev.origin;
375
+ const samePath = dest.pathname + dest.search === prev.pathname + prev.search;
376
+ if (sameOrigin && samePath) {
377
+ event.preventDefault();
378
+ if (dest.href !== prev.href) history.pushState(null, "", dest);
379
+ if (!dest.hash) {
380
+ if (dest.href.endsWith("#")) window.scrollTo(0, 0);
381
+ else {
382
+ window._qRouterScrollEnabled = false;
383
+ clearTimeout(window._qRouterScrollDebounce);
384
+ saveScrollState({
385
+ ...currentScrollState(),
386
+ x: 0,
387
+ y: 0
388
+ });
389
+ location.reload();
390
+ }
391
+ } else {
392
+ const elmId = dest.hash.slice(1);
393
+ const elm = document.getElementById(elmId);
394
+ if (elm) elm.scrollIntoView();
395
+ }
396
+ }
397
+ }
398
+ };
399
+ window._qRouterInitVisibility = () => {
400
+ if (!window._qRouterSPA && window._qRouterScrollEnabled && document.visibilityState === "hidden") saveScrollState();
401
+ };
402
+ window._qRouterInitScroll = () => {
403
+ if (window._qRouterSPA || !window._qRouterScrollEnabled) return;
404
+ clearTimeout(window._qRouterScrollDebounce);
405
+ window._qRouterScrollDebounce = setTimeout(() => {
406
+ saveScrollState();
407
+ window._qRouterScrollDebounce = void 0;
408
+ }, 200);
409
+ };
410
+ window._qRouterScrollEnabled = true;
411
+ window.addEventListener("popstate", window._qRouterInitPopstate);
412
+ window.addEventListener("scroll", window._qRouterInitScroll, { passive: true });
413
+ document.addEventListener("click", window._qRouterInitAnchors);
414
+ if (!window.navigation) document.addEventListener("visibilitychange", window._qRouterInitVisibility, { passive: true });
415
+ }
416
+ }, "spa_init_event_igI1pUsax0E"));
417
+ /**
418
+ * Submit an action to the server and get the result.
419
+ *
420
+ * POSTs to the route URL with `qaction={actionId}` appended and `Accept: application/json`. The
421
+ * server runs the action and returns the action result, optionally with the loader hashes that
422
+ * should be invalidated.
423
+ */ async function submitAction(action, routeUrl) {
424
+ const pathBase = ensureSlash(routeUrl.pathname);
425
+ const searchParams = new URLSearchParams(routeUrl.search);
426
+ searchParams.set(QACTION_KEY, action.id);
427
+ const url = `${pathBase}?${searchParams.toString()}`;
428
+ const actionData = action.data;
429
+ action.data = void 0;
430
+ let fetchOptions;
431
+ if (actionData instanceof FormData) fetchOptions = {
432
+ method: "POST",
433
+ body: actionData,
434
+ headers: { Accept: "application/json" }
435
+ };
436
+ else fetchOptions = {
437
+ method: "POST",
438
+ body: JSON.stringify(actionData),
439
+ headers: {
440
+ "Content-Type": "application/json; charset=UTF-8",
441
+ Accept: "application/json"
442
+ }
443
+ };
444
+ const response = await fetch(url, fetchOptions);
445
+ if (response.redirected) {
446
+ const redirectedURL = new URL(response.url);
447
+ if (redirectedURL.origin !== location.origin) {
448
+ location.href = redirectedURL.href;
449
+ return;
450
+ }
451
+ location.href = redirectedURL.href;
452
+ return;
453
+ }
454
+ if ((response.headers.get("content-type") || "").includes("json")) {
455
+ const text = await response.text();
456
+ const data = await _deserialize(text);
457
+ return {
458
+ status: response.status,
459
+ result: data?.result,
460
+ redirect: data?.redirect,
461
+ loaderHashes: data?.loaderHashes
462
+ };
463
+ }
524
464
  }
525
- var RouteDataProp = /* @__PURE__ */ function(RouteDataProp2) {
526
- RouteDataProp2[RouteDataProp2["RouteName"] = 0] = "RouteName";
527
- RouteDataProp2[RouteDataProp2["Loaders"] = 1] = "Loaders";
528
- RouteDataProp2[RouteDataProp2["OriginalPathname"] = 2] = "OriginalPathname";
529
- RouteDataProp2[RouteDataProp2["RouteBundleNames"] = 3] = "RouteBundleNames";
530
- return RouteDataProp2;
531
- }({});
532
- var MenuDataProp = /* @__PURE__ */ function(MenuDataProp2) {
533
- MenuDataProp2[MenuDataProp2["Pathname"] = 0] = "Pathname";
534
- MenuDataProp2[MenuDataProp2["MenuLoader"] = 1] = "MenuLoader";
535
- return MenuDataProp2;
536
- }({});
537
- const loadRoute = async (routes, menus, cacheModules, pathname) => {
538
- if (!Array.isArray(routes)) {
539
- return null;
540
- }
541
- for (const routeData of routes) {
542
- const routeName = routeData[RouteDataProp.RouteName];
543
- const params = matchRoute(routeName, pathname);
544
- if (!params) {
545
- continue;
546
- }
547
- const loaders = routeData[RouteDataProp.Loaders];
548
- const routeBundleNames = routeData[RouteDataProp.RouteBundleNames];
549
- const modules = new Array(loaders.length);
550
- const pendingLoads = [];
551
- loaders.forEach((moduleLoader, i) => {
552
- loadModule(moduleLoader, pendingLoads, (routeModule) => modules[i] = routeModule, cacheModules);
553
- });
554
- const menuLoader = getMenuLoader(menus, pathname);
555
- let menu = void 0;
556
- loadModule(menuLoader, pendingLoads, (menuModule) => menu = menuModule?.default, cacheModules);
557
- if (pendingLoads.length > 0) {
558
- await Promise.all(pendingLoads);
559
- }
560
- return [
561
- routeName,
562
- params,
563
- modules,
564
- deepFreeze(menu),
565
- routeBundleNames
566
- ];
567
- }
568
- return null;
465
+ var qwik_view_transition_default = "@layer qwik{@supports selector(html:active-view-transition-type(type)){html:active-view-transition-type(qwik-navigation){& :root{view-transition-name:none}}}@supports not selector(html:active-view-transition-type(type)){:root{view-transition-name:none}}}";
466
+ /** View transitions are opt-in and require browser support. */ var shouldStartViewTransition = (viewTransition) => viewTransition === true && "startViewTransition" in document;
467
+ var VIEW_TRANSITION_STYLE_ID = "qwik-view-transition";
468
+ /** Inject the view-transition stylesheet once, only when a transition actually runs. */ var ensureViewTransitionStyles = () => {
469
+ if (document.getElementById(VIEW_TRANSITION_STYLE_ID)) return;
470
+ const style = document.createElement("style");
471
+ style.id = VIEW_TRANSITION_STYLE_ID;
472
+ style.textContent = qwik_view_transition_default;
473
+ document.head.appendChild(style);
569
474
  };
570
- const loadModule = (moduleLoader, pendingLoads, moduleSetter, cacheModules) => {
571
- if (typeof moduleLoader === "function") {
572
- const loadedModule = MODULE_CACHE.get(moduleLoader);
573
- if (loadedModule) {
574
- moduleSetter(loadedModule);
575
- } else {
576
- const moduleOrPromise = moduleLoader();
577
- if (typeof moduleOrPromise.then === "function") {
578
- pendingLoads.push(moduleOrPromise.then((loadedModule2) => {
579
- if (cacheModules !== false) {
580
- MODULE_CACHE.set(moduleLoader, loadedModule2);
581
- }
582
- moduleSetter(loadedModule2);
583
- }));
584
- } else if (moduleOrPromise) {
585
- moduleSetter(moduleOrPromise);
586
- }
587
- }
588
- }
475
+ var startViewTransition = (params) => {
476
+ if ("startViewTransition" in document) {
477
+ ensureViewTransitionStyles();
478
+ let transition;
479
+ try {
480
+ transition = document.startViewTransition(params);
481
+ } catch {
482
+ transition = document.startViewTransition(params.update);
483
+ }
484
+ const event = new CustomEvent("qviewtransition", { detail: transition });
485
+ document.dispatchEvent(event);
486
+ return {
487
+ ready: transition.ready,
488
+ transition
489
+ };
490
+ } else return { ready: params.update() };
589
491
  };
590
- const getMenuLoader = (menus, pathname) => {
591
- if (menus) {
592
- pathname = pathname.endsWith("/") ? pathname : pathname + "/";
593
- const menu = menus.find((m) => m[MenuDataProp.Pathname] === pathname || pathname.startsWith(m[MenuDataProp.Pathname] + (pathname.endsWith("/") ? "" : "/")));
594
- if (menu) {
595
- return menu[MenuDataProp.MenuLoader];
596
- }
597
- }
492
+ /**
493
+ * Qwik Router Component
494
+ *
495
+ * This file contains the main Qwik Router component, which initializes the router and provides the
496
+ * necessary context for it to work. It also contains the logic for handling navigation, including
497
+ * updating the URL, managing scroll restoration, and resolving document head changes.
498
+ *
499
+ * Note: This component is designed to work both on the server and the client. During server-side
500
+ * rendering (SSR), it initializes the router state based on the URL and route data provided by the
501
+ * server environment. On the client, it handles navigation events and updates the router state
502
+ * accordingly.
503
+ *
504
+ * SSR is _required_ for the initial load.
505
+ *
506
+ * The flow of navigation is as follows:
507
+ *
508
+ * 1. During SSR, the server environment parses the initial URL and collects the route data.
509
+ * 2. It runs the middleware hooks (`onRequest`, `onGet`, `onPost`, etc.), and responds to q-loader,
510
+ * action$ and server$ requests.
511
+ * 3. If SSR is deemed appropriate, the route data is provided via `useServerData` and this component
512
+ * uses it to initialize the router contexts, register Tasks, and render the Slot. This component
513
+ * will never render again.
514
+ * 4. Then, slotted components like `<DocumentHeadTags />` and `<RouterOutlet />` can consume the route
515
+ * data to render the page.
516
+ * 5. On the client, when a navigation event occurs, this calls `goto()`, which updates the URL in the
517
+ * same tick, loads the route data and adjusts the router context.
518
+ * 6. The changed contexts trigger the slotted components to re-render with the new route data.
519
+ *
520
+ * Since the head data can depend on route loaders and they get their data asynchronously and can
521
+ * update without navigation, the head is resolved in a separate Task that tracks the relevant
522
+ * signals.
523
+ */
524
+ /**
525
+ * @deprecated Use `QWIK_ROUTER_SCROLLER` instead (will be removed in V3)
526
+ * @public
527
+ */ var QWIK_CITY_SCROLLER = "_qCityScroller";
528
+ /** @public */ var QWIK_ROUTER_SCROLLER = "_qRouterScroller";
529
+ var preventNav = {};
530
+ var internalState = { navCount: 0 };
531
+ var getScroller = () => {
532
+ let scroller = document.getElementById(QWIK_ROUTER_SCROLLER);
533
+ if (!scroller) {
534
+ scroller = document.getElementById(QWIK_CITY_SCROLLER);
535
+ if (scroller && isDev) console.warn(`Please update your scroller ID to "${QWIK_ROUTER_SCROLLER}" as "${QWIK_CITY_SCROLLER}" is deprecated and will be removed in V3`);
536
+ }
537
+ return scroller ?? document.documentElement;
598
538
  };
599
- function callRestoreScrollOnDocument() {
600
- if (document.__q_scroll_restore__) {
601
- document.__q_scroll_restore__();
602
- document.__q_scroll_restore__ = void 0;
603
- }
604
- }
605
- const restoreScroll = (type, toUrl2, fromUrl, scroller, scrollState) => {
606
- if (type === "popstate" && scrollState) {
607
- scroller.scrollTo(scrollState.x, scrollState.y);
608
- } else if (type === "link" || type === "form") {
609
- if (!hashScroll(toUrl2, fromUrl)) {
610
- scroller.scrollTo(0, 0);
611
- }
612
- }
613
- };
614
- const hashScroll = (toUrl2, fromUrl) => {
615
- const elmId = toUrl2.hash.slice(1);
616
- const elm = elmId && document.getElementById(elmId);
617
- if (elm) {
618
- elm.scrollIntoView();
619
- return true;
620
- } else if (!elm && toUrl2.hash && isSamePath(toUrl2, fromUrl)) {
621
- return true;
622
- }
623
- return false;
624
- };
625
- const currentScrollState = (elm) => {
626
- return {
627
- x: elm.scrollLeft,
628
- y: elm.scrollTop,
629
- w: Math.max(elm.scrollWidth, elm.clientWidth),
630
- h: Math.max(elm.scrollHeight, elm.clientHeight)
631
- };
632
- };
633
- const getScrollHistory = () => {
634
- const state = history.state;
635
- return state?._qRouterScroll;
636
- };
637
- const saveScrollHistory = (scrollState) => {
638
- const state = history.state || {};
639
- state._qRouterScroll = scrollState;
640
- history.replaceState(state, "");
641
- };
642
- const spaInit = event$((_, el) => {
643
- const win = window;
644
- const spa = "_qRouterSPA";
645
- const initPopstate = "_qRouterInitPopstate";
646
- const initAnchors = "_qRouterInitAnchors";
647
- const initVisibility = "_qRouterInitVisibility";
648
- const initScroll = "_qRouterInitScroll";
649
- if (!win[spa] && !win[initPopstate] && !win[initAnchors] && !win[initVisibility] && !win[initScroll]) {
650
- const currentPath = location.pathname + location.search;
651
- const historyPatch = "_qRouterHistoryPatch";
652
- const scrollEnabled = "_qRouterScrollEnabled";
653
- const debounceTimeout = "_qRouterScrollDebounce";
654
- const scrollHistory = "_qRouterScroll";
655
- const checkAndScroll = (scrollState) => {
656
- if (scrollState) {
657
- win.scrollTo(scrollState.x, scrollState.y);
658
- }
659
- };
660
- const currentScrollState2 = () => {
661
- const elm = document.documentElement;
662
- return {
663
- x: elm.scrollLeft,
664
- y: elm.scrollTop,
665
- w: Math.max(elm.scrollWidth, elm.clientWidth),
666
- h: Math.max(elm.scrollHeight, elm.clientHeight)
667
- };
668
- };
669
- const saveScrollState = (scrollState) => {
670
- const state = history.state || {};
671
- state[scrollHistory] = scrollState || currentScrollState2();
672
- history.replaceState(state, "");
673
- };
674
- saveScrollState();
675
- win[initPopstate] = () => {
676
- if (win[spa]) {
677
- return;
678
- }
679
- win[scrollEnabled] = false;
680
- clearTimeout(win[debounceTimeout]);
681
- if (currentPath !== location.pathname + location.search) {
682
- const getContainer = (el2) => el2.closest("[q\\:container]:not([q\\:container=html]):not([q\\:container=text])");
683
- const container = getContainer(el);
684
- const domContainer = container.qContainer;
685
- const hostElement = domContainer.vNodeLocate(el);
686
- const nav = domContainer?.resolveContext(hostElement, {
687
- id: "qc--n"
688
- });
689
- if (nav) {
690
- nav(location.href, {
691
- type: "popstate"
692
- });
693
- } else {
694
- location.reload();
695
- }
696
- } else {
697
- if (history.scrollRestoration === "manual") {
698
- const scrollState = history.state?.[scrollHistory];
699
- checkAndScroll(scrollState);
700
- win[scrollEnabled] = true;
701
- }
702
- }
703
- };
704
- if (!win[historyPatch]) {
705
- win[historyPatch] = true;
706
- const pushState = history.pushState;
707
- const replaceState = history.replaceState;
708
- const prepareState = (state) => {
709
- if (state === null || typeof state === "undefined") {
710
- state = {};
711
- } else if (state?.constructor !== Object) {
712
- state = {
713
- _data: state
714
- };
715
- if (isDev) {
716
- console.warn("In a Qwik SPA context, `history.state` is used to store scroll state. Direct calls to `pushState()` and `replaceState()` must supply an actual Object type. We need to be able to automatically attach the scroll state to your state object. A new state object has been created, your data has been moved to: `history.state._data`");
717
- }
718
- }
719
- state._qRouterScroll = state._qRouterScroll || currentScrollState2();
720
- return state;
721
- };
722
- history.pushState = (state, title, url) => {
723
- state = prepareState(state);
724
- return pushState.call(history, state, title, url);
725
- };
726
- history.replaceState = (state, title, url) => {
727
- state = prepareState(state);
728
- return replaceState.call(history, state, title, url);
729
- };
730
- }
731
- win[initAnchors] = (event) => {
732
- if (win[spa] || event.defaultPrevented) {
733
- return;
734
- }
735
- const target = event.target.closest("a[href]");
736
- if (target && !target.hasAttribute("preventdefault:click")) {
737
- const href = target.getAttribute("href");
738
- const prev = new URL(location.href);
739
- const dest = new URL(href, prev);
740
- const sameOrigin = dest.origin === prev.origin;
741
- const samePath = dest.pathname + dest.search === prev.pathname + prev.search;
742
- if (sameOrigin && samePath) {
743
- event.preventDefault();
744
- if (dest.href !== prev.href) {
745
- history.pushState(null, "", dest);
746
- }
747
- if (!dest.hash) {
748
- if (dest.href.endsWith("#")) {
749
- window.scrollTo(0, 0);
750
- } else {
751
- win[scrollEnabled] = false;
752
- clearTimeout(win[debounceTimeout]);
753
- saveScrollState({
754
- ...currentScrollState2(),
755
- x: 0,
756
- y: 0
757
- });
758
- location.reload();
759
- }
760
- } else {
761
- const elmId = dest.hash.slice(1);
762
- const elm = document.getElementById(elmId);
763
- if (elm) {
764
- elm.scrollIntoView();
765
- }
766
- }
767
- }
768
- }
769
- };
770
- win[initVisibility] = () => {
771
- if (!win[spa] && win[scrollEnabled] && document.visibilityState === "hidden") {
772
- saveScrollState();
773
- }
774
- };
775
- win[initScroll] = () => {
776
- if (win[spa] || !win[scrollEnabled]) {
777
- return;
778
- }
779
- clearTimeout(win[debounceTimeout]);
780
- win[debounceTimeout] = setTimeout(() => {
781
- saveScrollState();
782
- win[debounceTimeout] = void 0;
783
- }, 200);
784
- };
785
- win[scrollEnabled] = true;
786
- setTimeout(() => {
787
- addEventListener("popstate", win[initPopstate]);
788
- addEventListener("scroll", win[initScroll], {
789
- passive: true
790
- });
791
- document.body.addEventListener("click", win[initAnchors]);
792
- if (!win.navigation) {
793
- document.addEventListener("visibilitychange", win[initVisibility], {
794
- passive: true
795
- });
796
- }
797
- }, 0);
798
- }
799
- });
800
- const startViewTransition = (params) => {
801
- if (!params.update) {
802
- return;
803
- }
804
- if ("startViewTransition" in document) {
805
- let transition;
806
- try {
807
- transition = document.startViewTransition(params);
808
- } catch {
809
- transition = document.startViewTransition(params.update);
810
- }
811
- const event = new CustomEvent("qviewtransition", {
812
- detail: transition
813
- });
814
- document.dispatchEvent(event);
815
- return transition;
816
- } else {
817
- params.update?.();
818
- }
539
+ /** @public */ var useQwikMockRouter = (props) => {
540
+ const urlEnv = props.url ?? "http://localhost/";
541
+ const url = new URL(urlEnv);
542
+ const routeLocation = useStore({
543
+ url,
544
+ params: props.params ?? {},
545
+ isNavigating: false,
546
+ prevUrl: void 0
547
+ }, { deep: false });
548
+ const loadersData = props.loaders?.reduce((acc, { loader, data }) => {
549
+ acc[loader.__id] = data;
550
+ return acc;
551
+ }, {});
552
+ const loadersState = useStore({}, { deep: false });
553
+ for (const [loaderId, data] of Object.entries(loadersData ?? {})) loadersState[loaderId] ||= createComputedQrl(/*#__PURE__*/ inlinedQrl(() => {
554
+ return _captures[0];
555
+ }, "useQwikMockRouter_createComputed_JL6bChMwwyc", [data]));
556
+ const goto = props.goto ?? /*#__PURE__*/ inlinedQrl(async () => {
557
+ console.warn("QwikRouterMockProvider: goto not provided");
558
+ }, "useQwikMockRouter_goto_aViHFxQ1a3s");
559
+ const documentHead = useStore(createDocumentHead, { deep: false });
560
+ const content = useStore({
561
+ headings: void 0,
562
+ menu: void 0
563
+ }, { deep: false });
564
+ const contentInternal = useSignal();
565
+ const actionState = useSignal();
566
+ const httpStatus = useSignal({
567
+ status: 200,
568
+ message: ""
569
+ });
570
+ useContextProvider(ContentContext, content);
571
+ useContextProvider(ContentInternalContext, contentInternal);
572
+ useContextProvider(DocumentHeadContext, documentHead);
573
+ useContextProvider(HttpStatusContext, httpStatus);
574
+ useContextProvider(RouteLocationContext, routeLocation);
575
+ useContextProvider(RouteNavigateContext, goto);
576
+ useContextProvider(RouteStateContext, loadersState);
577
+ useContextProvider(RouteActionContext, actionState);
578
+ const actionsMocks = props.actions?.reduce((acc, { action, handler }) => {
579
+ acc[action.__id] = handler;
580
+ return acc;
581
+ }, {});
582
+ useTaskQrl(/*#__PURE__*/ inlinedQrl(async ({ track }) => {
583
+ const actionState = _captures[0], actionsMocks = _captures[1];
584
+ const action = track(actionState);
585
+ if (!action?.resolve) return;
586
+ const mock = actionsMocks?.[action.id];
587
+ if (mock) {
588
+ const actionResult = await mock(action.data);
589
+ action.resolve(actionResult);
590
+ }
591
+ }, "useQwikMockRouter_useTask_tXTLR4tzCy0", [actionState, actionsMocks]));
819
592
  };
820
- const QWIK_CITY_SCROLLER = "_qCityScroller";
821
- const QWIK_ROUTER_SCROLLER = "_qRouterScroller";
822
- const preventNav = {};
823
- const internalState = {
824
- navCount: 0
825
- };
826
- const QwikRouterProvider = component$((props) => {
827
- useStyles$(`
828
- @layer qwik {
829
- @supports selector(html:active-view-transition-type(type)) {
830
- html:active-view-transition-type(qwik-router-spa) {
831
- :root{view-transition-name:none}
832
- }
833
- }
834
- @supports not selector(html:active-view-transition-type(type)) {
835
- :root{view-transition-name:none}
836
- }
837
- }
838
- `);
839
- const env = useQwikRouterEnv();
840
- if (!env?.params) {
841
- throw new Error(`Missing Qwik Router Env Data for help visit https://github.com/QwikDev/qwik/issues/6237`);
842
- }
843
- const urlEnv = useServerData("url");
844
- if (!urlEnv) {
845
- throw new Error(`Missing Qwik URL Env Data`);
846
- }
847
- if (isServer) {
848
- if (env.ev.originalUrl.pathname !== env.ev.url.pathname && !__EXPERIMENTAL__.enableRequestRewrite) {
849
- throw new Error(`enableRequestRewrite is an experimental feature and is not enabled. Please enable the feature flag by adding \`experimental: ["enableRequestRewrite"]\` to your qwikVite plugin options.`);
850
- }
851
- }
852
- const url = new URL(urlEnv);
853
- const routeLocation = useStore({
854
- url,
855
- params: env.params,
856
- isNavigating: false,
857
- prevUrl: void 0
858
- }, {
859
- deep: false
860
- });
861
- const navResolver = {};
862
- const container = _getContextContainer();
863
- const getSerializationStrategy = (loaderId) => {
864
- return env.response.loadersSerializationStrategy.get(loaderId) || DEFAULT_LOADERS_SERIALIZATION_STRATEGY;
865
- };
866
- const loadersObject = {};
867
- const loaderState = {};
868
- for (const [key, value] of Object.entries(env.response.loaders)) {
869
- loadersObject[key] = value;
870
- loaderState[key] = createLoaderSignal(loadersObject, key, url, getSerializationStrategy(key), container);
871
- }
872
- loadersObject[SerializerSymbol] = (obj) => {
873
- const loadersSerializationObject = {};
874
- for (const [k, v] of Object.entries(obj)) {
875
- loadersSerializationObject[k] = getSerializationStrategy(k) === "always" ? v : _UNINITIALIZED;
876
- }
877
- return loadersSerializationObject;
878
- };
879
- const routeInternal = useSignal({
880
- type: "initial",
881
- dest: url,
882
- forceReload: false,
883
- replaceState: false,
884
- scroll: true
885
- });
886
- const documentHead = useStore(createDocumentHead);
887
- const content = useStore({
888
- headings: void 0,
889
- menu: void 0
890
- });
891
- const contentInternal = useSignal();
892
- const currentActionId = env.response.action;
893
- const currentAction = currentActionId ? env.response.loaders[currentActionId] : void 0;
894
- const actionState = useSignal(currentAction ? {
895
- id: currentActionId,
896
- data: env.response.formData,
897
- output: {
898
- result: currentAction,
899
- status: env.response.status
900
- }
901
- } : void 0);
902
- const registerPreventNav = $((fn$) => {
903
- if (!isBrowser) {
904
- return;
905
- }
906
- preventNav.$handler$ || (preventNav.$handler$ = (event) => {
907
- internalState.navCount++;
908
- if (!preventNav.$cbs$) {
909
- return;
910
- }
911
- const prevents = [
912
- ...preventNav.$cbs$.values()
913
- ].map((cb) => cb.resolved ? cb.resolved() : cb());
914
- if (prevents.some(Boolean)) {
915
- event.preventDefault();
916
- event.returnValue = true;
917
- }
918
- });
919
- (preventNav.$cbs$ || (preventNav.$cbs$ = /* @__PURE__ */ new Set())).add(fn$);
920
- fn$.resolve();
921
- window.addEventListener("beforeunload", preventNav.$handler$);
922
- return () => {
923
- if (preventNav.$cbs$) {
924
- preventNav.$cbs$.delete(fn$);
925
- if (!preventNav.$cbs$.size) {
926
- preventNav.$cbs$ = void 0;
927
- window.removeEventListener("beforeunload", preventNav.$handler$);
928
- }
929
- }
930
- };
931
- });
932
- const goto = $(async (path, opt) => {
933
- const { type = "link", forceReload = path === void 0, replaceState = false, scroll = true } = typeof opt === "object" ? opt : {
934
- forceReload: opt
935
- };
936
- internalState.navCount++;
937
- const lastDest = routeInternal.value.dest;
938
- const dest = path === void 0 ? lastDest : typeof path === "number" ? path : toUrl(path, routeLocation.url);
939
- if (preventNav.$cbs$ && (forceReload || typeof dest === "number" || !isSamePath(dest, lastDest) || !isSameOrigin(dest, lastDest))) {
940
- const ourNavId = internalState.navCount;
941
- const prevents = await Promise.all([
942
- ...preventNav.$cbs$.values()
943
- ].map((cb) => cb(dest)));
944
- if (ourNavId !== internalState.navCount || prevents.some(Boolean)) {
945
- if (ourNavId === internalState.navCount && type === "popstate") {
946
- history.pushState(null, "", lastDest);
947
- }
948
- return;
949
- }
950
- }
951
- if (typeof dest === "number") {
952
- if (isBrowser) {
953
- history.go(dest);
954
- }
955
- return;
956
- }
957
- if (!isSameOrigin(dest, lastDest)) {
958
- if (isBrowser) {
959
- location.href = dest.href;
960
- }
961
- return;
962
- }
963
- if (!forceReload && isSamePath(dest, lastDest)) {
964
- if (isBrowser) {
965
- if (type === "link" && dest.href !== location.href) {
966
- history.pushState(null, "", dest);
967
- }
968
- let scroller = document.getElementById(QWIK_ROUTER_SCROLLER);
969
- if (!scroller) {
970
- scroller = document.getElementById(QWIK_CITY_SCROLLER);
971
- if (scroller && isDev) {
972
- console.warn(`Please update your scroller ID to "${QWIK_ROUTER_SCROLLER}" as "${QWIK_CITY_SCROLLER}" is deprecated and will be removed in V3`);
973
- }
974
- }
975
- if (!scroller) {
976
- scroller = document.documentElement;
977
- }
978
- restoreScroll(type, dest, new URL(location.href), scroller, getScrollHistory());
979
- if (type === "popstate") {
980
- window._qRouterScrollEnabled = true;
981
- }
982
- }
983
- return;
984
- }
985
- routeInternal.value = {
986
- type,
987
- dest,
988
- forceReload,
989
- replaceState,
990
- scroll
991
- };
992
- if (isBrowser) {
993
- loadClientData(dest, _getContextElement());
994
- loadRoute(qwikRouterConfig.routes, qwikRouterConfig.menus, qwikRouterConfig.cacheModules, dest.pathname);
995
- }
996
- actionState.value = void 0;
997
- routeLocation.isNavigating = true;
998
- return new Promise((resolve) => {
999
- navResolver.r = resolve;
1000
- });
1001
- });
1002
- useContextProvider(ContentContext, content);
1003
- useContextProvider(ContentInternalContext, contentInternal);
1004
- useContextProvider(DocumentHeadContext, documentHead);
1005
- useContextProvider(RouteLocationContext, routeLocation);
1006
- useContextProvider(RouteNavigateContext, goto);
1007
- useContextProvider(RouteStateContext, loaderState);
1008
- useContextProvider(RouteActionContext, actionState);
1009
- useContextProvider(RouteInternalContext, routeInternal);
1010
- useContextProvider(RoutePreventNavigateContext, registerPreventNav);
1011
- useTask$(({ track }) => {
1012
- async function run() {
1013
- const [navigation, action] = track(() => [
1014
- routeInternal.value,
1015
- actionState.value
1016
- ]);
1017
- const locale = getLocale("");
1018
- const prevUrl = routeLocation.url;
1019
- const navType = action ? "form" : navigation.type;
1020
- const replaceState = navigation.replaceState;
1021
- let trackUrl;
1022
- let clientPageData;
1023
- let loadedRoute = null;
1024
- let elm;
1025
- if (isServer) {
1026
- trackUrl = new URL(navigation.dest, routeLocation.url);
1027
- loadedRoute = env.loadedRoute;
1028
- clientPageData = env.response;
1029
- } else {
1030
- trackUrl = new URL(navigation.dest, location);
1031
- if (trackUrl.pathname.endsWith("/")) {
1032
- if (!qwikRouterConfig.trailingSlash) {
1033
- trackUrl.pathname = trackUrl.pathname.slice(0, -1);
1034
- }
1035
- } else if (qwikRouterConfig.trailingSlash) {
1036
- trackUrl.pathname += "/";
1037
- }
1038
- let loadRoutePromise = loadRoute(qwikRouterConfig.routes, qwikRouterConfig.menus, qwikRouterConfig.cacheModules, trackUrl.pathname);
1039
- elm = _getContextElement();
1040
- const pageData = clientPageData = await loadClientData(trackUrl, elm, {
1041
- action,
1042
- clearCache: true
1043
- });
1044
- if (!pageData) {
1045
- routeInternal.untrackedValue = {
1046
- type: navType,
1047
- dest: trackUrl
1048
- };
1049
- return;
1050
- }
1051
- const newHref = pageData.href;
1052
- const newURL = new URL(newHref, trackUrl);
1053
- if (!isSamePath(newURL, trackUrl)) {
1054
- if (!pageData.isRewrite) {
1055
- trackUrl = newURL;
1056
- }
1057
- loadRoutePromise = loadRoute(
1058
- qwikRouterConfig.routes,
1059
- qwikRouterConfig.menus,
1060
- qwikRouterConfig.cacheModules,
1061
- newURL.pathname
1062
- // Load the actual required path.
1063
- );
1064
- }
1065
- try {
1066
- loadedRoute = await loadRoutePromise;
1067
- } catch (e) {
1068
- console.error(e);
1069
- window.location.href = newHref;
1070
- return;
1071
- }
1072
- }
1073
- if (loadedRoute) {
1074
- const [routeName, params, mods, menu] = loadedRoute;
1075
- const contentModules = mods;
1076
- const pageModule = contentModules[contentModules.length - 1];
1077
- const isRedirect = navType === "form" && !isSamePath(trackUrl, prevUrl);
1078
- if (navigation.dest.search && !isRedirect) {
1079
- trackUrl.search = navigation.dest.search;
1080
- }
1081
- if (!isSamePath(trackUrl, prevUrl)) {
1082
- routeLocation.prevUrl = prevUrl;
1083
- }
1084
- routeLocation.url = trackUrl;
1085
- routeLocation.params = {
1086
- ...params
1087
- };
1088
- routeInternal.untrackedValue = {
1089
- type: navType,
1090
- dest: trackUrl
1091
- };
1092
- const resolvedHead = resolveHead(clientPageData, routeLocation, contentModules, locale);
1093
- content.headings = pageModule.headings;
1094
- content.menu = menu;
1095
- contentInternal.value = noSerialize(contentModules);
1096
- documentHead.links = resolvedHead.links;
1097
- documentHead.meta = resolvedHead.meta;
1098
- documentHead.styles = resolvedHead.styles;
1099
- documentHead.scripts = resolvedHead.scripts;
1100
- documentHead.title = resolvedHead.title;
1101
- documentHead.frontmatter = resolvedHead.frontmatter;
1102
- if (isBrowser) {
1103
- let scrollState;
1104
- if (navType === "popstate") {
1105
- scrollState = getScrollHistory();
1106
- }
1107
- const scroller = document.getElementById(QWIK_ROUTER_SCROLLER) ?? document.documentElement;
1108
- if (navigation.scroll && (!navigation.forceReload || !isSamePath(trackUrl, prevUrl)) && (navType === "link" || navType === "popstate") || isRedirect) {
1109
- document.__q_scroll_restore__ = () => restoreScroll(navType, trackUrl, prevUrl, scroller, scrollState);
1110
- }
1111
- const loaders = clientPageData?.loaders;
1112
- if (loaders) {
1113
- const container2 = _getContextContainer();
1114
- for (const [key, value] of Object.entries(loaders)) {
1115
- const signal = loaderState[key];
1116
- const awaitedValue = await value;
1117
- loadersObject[key] = awaitedValue;
1118
- if (!signal) {
1119
- loaderState[key] = createLoaderSignal(loadersObject, key, trackUrl, DEFAULT_LOADERS_SERIALIZATION_STRATEGY, container2);
1120
- } else {
1121
- signal.invalidate();
1122
- }
1123
- }
1124
- }
1125
- CLIENT_DATA_CACHE.clear();
1126
- const win = window;
1127
- if (!win._qRouterSPA) {
1128
- win._qRouterSPA = true;
1129
- history.scrollRestoration = "manual";
1130
- win.addEventListener("popstate", () => {
1131
- win._qRouterScrollEnabled = false;
1132
- clearTimeout(win._qRouterScrollDebounce);
1133
- goto(location.href, {
1134
- type: "popstate"
1135
- });
1136
- });
1137
- win.removeEventListener("popstate", win._qRouterInitPopstate);
1138
- win._qRouterInitPopstate = void 0;
1139
- if (!win._qRouterHistoryPatch) {
1140
- win._qRouterHistoryPatch = true;
1141
- const pushState = history.pushState;
1142
- const replaceState2 = history.replaceState;
1143
- const prepareState = (state) => {
1144
- if (state === null || typeof state === "undefined") {
1145
- state = {};
1146
- } else if (state?.constructor !== Object) {
1147
- state = {
1148
- _data: state
1149
- };
1150
- if (isDev) {
1151
- console.warn("In a Qwik SPA context, `history.state` is used to store scroll state. Direct calls to `pushState()` and `replaceState()` must supply an actual Object type. We need to be able to automatically attach the scroll state to your state object. A new state object has been created, your data has been moved to: `history.state._data`");
1152
- }
1153
- }
1154
- state._qRouterScroll = state._qRouterScroll || currentScrollState(scroller);
1155
- return state;
1156
- };
1157
- history.pushState = (state, title, url2) => {
1158
- state = prepareState(state);
1159
- return pushState.call(history, state, title, url2);
1160
- };
1161
- history.replaceState = (state, title, url2) => {
1162
- state = prepareState(state);
1163
- return replaceState2.call(history, state, title, url2);
1164
- };
1165
- }
1166
- document.body.addEventListener("click", (event) => {
1167
- if (event.defaultPrevented) {
1168
- return;
1169
- }
1170
- const target = event.target.closest("a[href]");
1171
- if (target && !target.hasAttribute("preventdefault:click")) {
1172
- const href = target.getAttribute("href");
1173
- const prev = new URL(location.href);
1174
- const dest = new URL(href, prev);
1175
- if (isSameOrigin(dest, prev) && isSamePath(dest, prev)) {
1176
- event.preventDefault();
1177
- if (!dest.hash && !dest.href.endsWith("#")) {
1178
- if (dest.href !== prev.href) {
1179
- history.pushState(null, "", dest);
1180
- }
1181
- win._qRouterScrollEnabled = false;
1182
- clearTimeout(win._qRouterScrollDebounce);
1183
- saveScrollHistory({
1184
- ...currentScrollState(scroller),
1185
- x: 0,
1186
- y: 0
1187
- });
1188
- location.reload();
1189
- return;
1190
- }
1191
- goto(target.getAttribute("href"));
1192
- }
1193
- }
1194
- });
1195
- document.body.removeEventListener("click", win._qRouterInitAnchors);
1196
- win._qRouterInitAnchors = void 0;
1197
- if (!window.navigation) {
1198
- document.addEventListener("visibilitychange", () => {
1199
- if ((win._qRouterScrollEnabled || win._qCityScrollEnabled) && document.visibilityState === "hidden") {
1200
- if (win._qCityScrollEnabled) {
1201
- console.warn('"_qCityScrollEnabled" is deprecated. Use "_qRouterScrollEnabled" instead.');
1202
- }
1203
- const scrollState2 = currentScrollState(scroller);
1204
- saveScrollHistory(scrollState2);
1205
- }
1206
- }, {
1207
- passive: true
1208
- });
1209
- document.removeEventListener("visibilitychange", win._qRouterInitVisibility);
1210
- win._qRouterInitVisibility = void 0;
1211
- }
1212
- win.addEventListener("scroll", () => {
1213
- if (!win._qRouterScrollEnabled && !win._qCityScrollEnabled) {
1214
- return;
1215
- }
1216
- clearTimeout(win._qRouterScrollDebounce);
1217
- win._qRouterScrollDebounce = setTimeout(() => {
1218
- const scrollState2 = currentScrollState(scroller);
1219
- saveScrollHistory(scrollState2);
1220
- win._qRouterScrollDebounce = void 0;
1221
- }, 200);
1222
- }, {
1223
- passive: true
1224
- });
1225
- removeEventListener("scroll", win._qRouterInitScroll);
1226
- win._qRouterInitScroll = void 0;
1227
- spaInit.resolve();
1228
- }
1229
- if (navType !== "popstate") {
1230
- win._qRouterScrollEnabled = false;
1231
- clearTimeout(win._qRouterScrollDebounce);
1232
- const scrollState2 = currentScrollState(scroller);
1233
- saveScrollHistory(scrollState2);
1234
- }
1235
- const navigate = () => {
1236
- clientNavigate(window, navType, prevUrl, trackUrl, replaceState);
1237
- return _waitUntilRendered(elm);
1238
- };
1239
- const _waitNextPage = () => {
1240
- if (isServer || props.viewTransition === false) {
1241
- return navigate();
1242
- } else {
1243
- const viewTransition = startViewTransition({
1244
- update: navigate,
1245
- types: [
1246
- "qwik-navigation"
1247
- ]
1248
- });
1249
- if (!viewTransition) {
1250
- return Promise.resolve();
1251
- }
1252
- return viewTransition.ready;
1253
- }
1254
- };
1255
- _waitNextPage().then(() => {
1256
- const container2 = _getQContainerElement(elm);
1257
- container2.setAttribute(Q_ROUTE, routeName);
1258
- const scrollState2 = currentScrollState(scroller);
1259
- saveScrollHistory(scrollState2);
1260
- win._qRouterScrollEnabled = true;
1261
- if (isBrowser) {
1262
- callRestoreScrollOnDocument();
1263
- }
1264
- routeLocation.isNavigating = false;
1265
- navResolver.r?.();
1266
- });
1267
- }
1268
- }
1269
- }
1270
- if (isServer) {
1271
- return run();
1272
- } else {
1273
- run();
1274
- }
1275
- });
1276
- return /* @__PURE__ */ jsx(Slot, {});
1277
- });
1278
- const QwikCityProvider = QwikRouterProvider;
1279
- const QwikRouterMockProvider = component$((props) => {
1280
- const urlEnv = props.url ?? "http://localhost/";
1281
- const url = new URL(urlEnv);
1282
- const routeLocation = useStore({
1283
- url,
1284
- params: props.params ?? {},
1285
- isNavigating: false,
1286
- prevUrl: void 0
1287
- }, {
1288
- deep: false
1289
- });
1290
- const loaderState = {};
1291
- const routeInternal = useSignal({
1292
- type: "initial",
1293
- dest: url
1294
- });
1295
- const goto = props.goto ?? $(async () => {
1296
- console.warn("QwikRouterMockProvider: goto not provided");
1297
- });
1298
- const documentHead = useStore(createDocumentHead, {
1299
- deep: false
1300
- });
1301
- const content = useStore({
1302
- headings: void 0,
1303
- menu: void 0
1304
- }, {
1305
- deep: false
1306
- });
1307
- const contentInternal = useSignal();
1308
- const actionState = useSignal();
1309
- useContextProvider(ContentContext, content);
1310
- useContextProvider(ContentInternalContext, contentInternal);
1311
- useContextProvider(DocumentHeadContext, documentHead);
1312
- useContextProvider(RouteLocationContext, routeLocation);
1313
- useContextProvider(RouteNavigateContext, goto);
1314
- useContextProvider(RouteStateContext, loaderState);
1315
- useContextProvider(RouteActionContext, actionState);
1316
- useContextProvider(RouteInternalContext, routeInternal);
1317
- return /* @__PURE__ */ jsx(Slot, {});
1318
- });
1319
- const QwikCityMockProvider = QwikRouterMockProvider;
1320
- const RouterOutlet = component$(() => {
1321
- const serverData = useServerData("containerAttributes");
1322
- if (!serverData) {
1323
- throw new Error("PrefetchServiceWorker component must be rendered on the server.");
1324
- }
1325
- const { value } = useContext(ContentInternalContext);
1326
- if (value && value.length > 0) {
1327
- const contentsLen = value.length;
1328
- let cmp = null;
1329
- for (let i = contentsLen - 1; i >= 0; i--) {
1330
- if (value[i].default) {
1331
- cmp = jsx$1(value[i].default, {
1332
- children: cmp
1333
- });
1334
- }
1335
- }
1336
- return /* @__PURE__ */ jsxs(Fragment, {
1337
- children: [
1338
- cmp,
1339
- !__EXPERIMENTAL__.noSPA && /* @__PURE__ */ jsx("script", {
1340
- "document:onQCInit$": spaInit,
1341
- "document:onQInit$": sync$(() => {
1342
- ((w, h) => {
1343
- if (!w._qcs && h.scrollRestoration === "manual") {
1344
- w._qcs = true;
1345
- const s = h.state?._qRouterScroll;
1346
- if (s) {
1347
- w.scrollTo(s.x, s.y);
1348
- }
1349
- document.dispatchEvent(new Event("qcinit"));
1350
- }
1351
- })(window, history);
1352
- })
1353
- })
1354
- ]
1355
- });
1356
- }
1357
- return SkipRender;
1358
- });
1359
- var store;
1360
- function getGlobalConfig(config2) {
1361
- return {
1362
- lang: config2?.lang ?? store?.lang,
1363
- message: config2?.message,
1364
- abortEarly: config2?.abortEarly ?? store?.abortEarly,
1365
- abortPipeEarly: config2?.abortPipeEarly ?? store?.abortPipeEarly
1366
- };
1367
- }
1368
- function getDotPath(issue) {
1369
- if (issue.path) {
1370
- let key = "";
1371
- for (const item of issue.path) {
1372
- if (typeof item.key === "string" || typeof item.key === "number") {
1373
- if (key) {
1374
- key += `.${item.key}`;
1375
- } else {
1376
- key += item.key;
1377
- }
1378
- } else {
1379
- return null;
1380
- }
1381
- }
1382
- return key;
1383
- }
1384
- return null;
1385
- }
1386
- function flatten(issues) {
1387
- const flatErrors = {};
1388
- for (const issue of issues) {
1389
- if (issue.path) {
1390
- const dotPath = getDotPath(issue);
1391
- if (dotPath) {
1392
- if (!flatErrors.nested) {
1393
- flatErrors.nested = {};
1394
- }
1395
- if (flatErrors.nested[dotPath]) {
1396
- flatErrors.nested[dotPath].push(issue.message);
1397
- } else {
1398
- flatErrors.nested[dotPath] = [issue.message];
1399
- }
1400
- } else {
1401
- if (flatErrors.other) {
1402
- flatErrors.other.push(issue.message);
1403
- } else {
1404
- flatErrors.other = [issue.message];
1405
- }
1406
- }
1407
- } else {
1408
- if (flatErrors.root) {
1409
- flatErrors.root.push(issue.message);
1410
- } else {
1411
- flatErrors.root = [issue.message];
1412
- }
1413
- }
1414
- }
1415
- return flatErrors;
1416
- }
1417
- async function safeParseAsync(schema, input, config2) {
1418
- const dataset = await schema._run(
1419
- { typed: false, value: input },
1420
- getGlobalConfig(config2)
1421
- );
1422
- return {
1423
- typed: dataset.typed,
1424
- success: !dataset.issues,
1425
- output: dataset.value,
1426
- issues: dataset.issues
1427
- };
593
+ /** @public */ var QwikRouterMockProvider = /*#__PURE__*/ componentQrl(/*#__PURE__*/ inlinedQrl((props) => {
594
+ useQwikMockRouter(props);
595
+ return /*#__PURE__*/ _jsxSorted(Slot, null, null, null, 3, "5y_1");
596
+ }, "QwikRouterMockProvider_component_IN4dVpT0x74"));
597
+ /**
598
+ * @deprecated Use `useQwikMockRouter()` instead. Will be removed in V3
599
+ * @public
600
+ */ var QwikCityMockProvider = QwikRouterMockProvider;
601
+ function initializeSPA(goto, scroller) {
602
+ if (!window._qRouterSPA) {
603
+ window._qRouterSPA = true;
604
+ history.scrollRestoration = "manual";
605
+ window.addEventListener("popstate", () => {
606
+ window._qRouterScrollEnabled = false;
607
+ clearTimeout(window._qRouterScrollDebounce);
608
+ goto(location.href, { type: "popstate" });
609
+ });
610
+ window.removeEventListener("popstate", window._qRouterInitPopstate);
611
+ window._qRouterInitPopstate = void 0;
612
+ if (!window._qRouterHistoryPatch) {
613
+ window._qRouterHistoryPatch = true;
614
+ const pushState = history.pushState;
615
+ const replaceState = history.replaceState;
616
+ const prepareState = (state) => {
617
+ if (state === null || typeof state === "undefined") state = {};
618
+ else if (state?.constructor !== Object) {
619
+ state = { _data: state };
620
+ if (isDev) console.warn("In a Qwik SPA context, `history.state` is used to store scroll state. Direct calls to `pushState()` and `replaceState()` must supply an actual Object type. We need to be able to automatically attach the scroll state to your state object. A new state object has been created, your data has been moved to: `history.state._data`");
621
+ }
622
+ state._qRouterScroll = state._qRouterScroll || currentScrollState(scroller);
623
+ return state;
624
+ };
625
+ history.pushState = (state, title, url) => {
626
+ state = prepareState(state);
627
+ return pushState.call(history, state, title, url);
628
+ };
629
+ history.replaceState = (state, title, url) => {
630
+ state = prepareState(state);
631
+ return replaceState.call(history, state, title, url);
632
+ };
633
+ }
634
+ document.addEventListener("click", (event) => {
635
+ if (event.defaultPrevented) return;
636
+ const target = event.target.closest("a[href]");
637
+ if (target && !target.hasAttribute("preventdefault:click")) {
638
+ const href = target.getAttribute("href");
639
+ const prev = new URL(location.href);
640
+ const dest = new URL(href, prev);
641
+ if (isSameOrigin(dest, prev) && isSamePath(dest, prev)) {
642
+ event.preventDefault();
643
+ if (!dest.hash && !dest.href.endsWith("#")) {
644
+ if (dest.href !== prev.href) history.pushState(null, "", dest);
645
+ window._qRouterScrollEnabled = false;
646
+ clearTimeout(window._qRouterScrollDebounce);
647
+ saveScrollHistory({
648
+ ...currentScrollState(scroller),
649
+ x: 0,
650
+ y: 0
651
+ });
652
+ location.reload();
653
+ return;
654
+ }
655
+ goto(target.getAttribute("href"));
656
+ }
657
+ }
658
+ });
659
+ document.removeEventListener("click", window._qRouterInitAnchors);
660
+ window._qRouterInitAnchors = void 0;
661
+ if (!window.navigation) {
662
+ document.addEventListener("visibilitychange", () => {
663
+ if (window._qRouterScrollEnabled && document.visibilityState === "hidden") saveScrollHistory(currentScrollState(scroller));
664
+ }, { passive: true });
665
+ document.removeEventListener("visibilitychange", window._qRouterInitVisibility);
666
+ window._qRouterInitVisibility = void 0;
667
+ }
668
+ window.addEventListener("scroll", () => {
669
+ if (!window._qRouterScrollEnabled) return;
670
+ clearTimeout(window._qRouterScrollDebounce);
671
+ window._qRouterScrollDebounce = setTimeout(() => {
672
+ saveScrollHistory(currentScrollState(scroller));
673
+ window._qRouterScrollDebounce = void 0;
674
+ }, 200);
675
+ }, { passive: true });
676
+ removeEventListener("scroll", window._qRouterInitScroll);
677
+ window._qRouterInitScroll = void 0;
678
+ spa_init_default.resolve();
679
+ }
1428
680
  }
1429
- const routeActionQrl = (actionQrl, ...rest) => {
1430
- const { id, validators } = getValidators(rest, actionQrl);
1431
- function action() {
1432
- const loc = useLocation();
1433
- const currentAction = useAction();
1434
- const initialState = {
1435
- actionPath: `?${QACTION_KEY}=${id}`,
1436
- submitted: false,
1437
- isRunning: false,
1438
- status: void 0,
1439
- value: void 0,
1440
- formData: void 0
1441
- };
1442
- const state = useStore(() => {
1443
- const value = currentAction.value;
1444
- if (value && value?.id === id) {
1445
- const data = value.data;
1446
- if (data instanceof FormData) {
1447
- initialState.formData = data;
1448
- }
1449
- if (value.output) {
1450
- const { status, result } = value.output;
1451
- initialState.status = status;
1452
- initialState.value = result;
1453
- }
1454
- }
1455
- return initialState;
1456
- });
1457
- const submit = $((input = {}) => {
1458
- if (isServer) {
1459
- throw new Error(`Actions can not be invoked within the server during SSR.
1460
- Action.run() can only be called on the browser, for example when a user clicks a button, or submits a form.`);
1461
- }
1462
- let data;
1463
- let form;
1464
- if (input instanceof SubmitEvent) {
1465
- form = input.target;
1466
- data = new FormData(form);
1467
- if ((input.submitter instanceof HTMLInputElement || input.submitter instanceof HTMLButtonElement) && input.submitter.name) {
1468
- if (input.submitter.name) {
1469
- data.append(input.submitter.name, input.submitter.value);
1470
- }
1471
- }
1472
- } else {
1473
- data = input;
1474
- }
1475
- return new Promise((resolve) => {
1476
- if (data instanceof FormData) {
1477
- state.formData = data;
1478
- }
1479
- state.submitted = true;
1480
- state.isRunning = true;
1481
- loc.isNavigating = true;
1482
- currentAction.value = {
1483
- data,
1484
- id,
1485
- resolve: noSerialize(resolve)
1486
- };
1487
- }).then(({ result, status }) => {
1488
- state.isRunning = false;
1489
- state.status = status;
1490
- state.value = result;
1491
- if (form) {
1492
- if (form.getAttribute("data-spa-reset") === "true") {
1493
- form.reset();
1494
- }
1495
- const detail = {
1496
- status,
1497
- value: result
1498
- };
1499
- form.dispatchEvent(new CustomEvent("submitcompleted", {
1500
- bubbles: false,
1501
- cancelable: false,
1502
- composed: false,
1503
- detail
1504
- }));
1505
- }
1506
- return {
1507
- status,
1508
- value: result
1509
- };
1510
- });
1511
- });
1512
- initialState.submit = submit;
1513
- return state;
1514
- }
1515
- action.__brand = "server_action";
1516
- action.__validators = validators;
1517
- action.__qrl = actionQrl;
1518
- action.__id = id;
1519
- Object.freeze(action);
1520
- return action;
681
+ /**
682
+ * @public
683
+ * This hook initializes Qwik Router, providing the necessary context for it to work.
684
+ *
685
+ * This hook should be used once, at the root of your application.
686
+ */ var useQwikRouter = (props) => {
687
+ if (!isServer) throw new Error("useQwikRouter can only run during SSR on the server. If you are seeing this, it means you are re-rendering the root of your application. Fix that or use the <QwikRouterProvider> component around the root of your application.");
688
+ const env = useQwikRouterEnv();
689
+ if (!env?.params) throw new Error(`Missing Qwik Router Env Data for help visit https://github.com/QwikDev/qwik/issues/6237`);
690
+ const urlEnv = useServerData("url");
691
+ if (!urlEnv) throw new Error(`Missing Qwik URL Env Data`);
692
+ const serverHead = useServerData("documentHead");
693
+ const manifestHash = useServerData("containerAttributes")?.["q:manifest-hash"];
694
+ const url = new URL(urlEnv);
695
+ const routeLocationTarget = {
696
+ url,
697
+ params: env.params,
698
+ isNavigating: false,
699
+ prevUrl: void 0
700
+ };
701
+ const routeLocation = useStore(routeLocationTarget, { deep: false });
702
+ const navResolver = {};
703
+ const routeLoaderCtx = useStore(env.routeLoaderCtx);
704
+ routeLoaderCtx.manifestHash = manifestHash;
705
+ const loaderState = {};
706
+ const contentModulesForInit = env.loadedRoute.$mods$;
707
+ const loaders = ensureRouteLoaderSignals(contentModulesForInit, loaderState, routeLoaderCtx);
708
+ for (const loader of loaders) if (loader.__id in env.loaderValues) {
709
+ const value = env.loaderValues[loader.__id];
710
+ setLoaderSignalValue(loaderState[loader.__id], value);
711
+ }
712
+ const routeInternal = useSignal({
713
+ type: "initial",
714
+ dest: url
715
+ });
716
+ const documentHead = useStore(() => createDocumentHead(serverHead, manifestHash));
717
+ const content = useStore({
718
+ headings: void 0,
719
+ menu: void 0
720
+ });
721
+ const contentInternal = useSignal();
722
+ /**
723
+ * Non-serializable navigation context passed from the nav task to the head+commit task. Only the
724
+ * data that can't be derived from existing stores/signals.
725
+ */ const navContext = useSignal();
726
+ const httpStatus = useSignal({
727
+ status: env.response.status,
728
+ message: env.loadedRoute.$notFound$ ? "Not Found" : env.response.statusMessage ?? ""
729
+ });
730
+ const currentActionId = env.response.action;
731
+ const currentAction = currentActionId ? env.response.actionResult : void 0;
732
+ const actionState = useSignal(currentAction ? {
733
+ id: currentActionId,
734
+ data: env.response.formData,
735
+ output: {
736
+ result: currentAction,
737
+ status: env.response.status
738
+ }
739
+ } : void 0);
740
+ const actionDataSignal = useSignal(currentActionId ? {
741
+ action: currentActionId,
742
+ actionResult: currentAction,
743
+ status: env.response.status
744
+ } : void 0);
745
+ const registerPreventNav = /*#__PURE__*/ inlinedQrl((fn$) => {
746
+ if (!isBrowser) return;
747
+ preventNav.$handler$ ||= (event) => {
748
+ internalState.navCount++;
749
+ if (!preventNav.$cbs$) return;
750
+ if ([...preventNav.$cbs$.values()].map((cb) => cb.resolved ? cb.resolved() : cb()).some(Boolean)) {
751
+ event.preventDefault();
752
+ event.returnValue = true;
753
+ }
754
+ };
755
+ (preventNav.$cbs$ ||= /* @__PURE__ */ new Set()).add(fn$);
756
+ fn$.resolve();
757
+ window.addEventListener("beforeunload", preventNav.$handler$);
758
+ return () => {
759
+ if (preventNav.$cbs$) {
760
+ preventNav.$cbs$.delete(fn$);
761
+ if (!preventNav.$cbs$.size) {
762
+ preventNav.$cbs$ = void 0;
763
+ window.removeEventListener("beforeunload", preventNav.$handler$);
764
+ }
765
+ }
766
+ };
767
+ }, "useQwikRouter_registerPreventNav_69B0DK0eZJc");
768
+ /**
769
+ * This is the `nav()` function that `useNavigation()` returns. It is also used internally for SPA
770
+ * navigations and is provided in context for use in loaders and actions.
771
+ *
772
+ * Note: when goto is called, the address bar change must happen in the same tick for Safari to
773
+ * treat it as a user-initiated navigation and allow scroll restoration to work. For this reason,
774
+ * make sure to change the address bar before awaiting anything.
775
+ */ const goto = /*#__PURE__*/ inlinedQrl(async (path, opt) => {
776
+ const actionState = _captures[0], manifestHash = _captures[1], navResolver = _captures[2], routeInternal = _captures[3], routeLocation = _captures[4];
777
+ const { type = "link", forceReload = path === void 0, replaceState = false, scroll = true } = typeof opt === "object" ? opt : { forceReload: opt };
778
+ if (isBrowser && type === "link" && routeInternal.value.type === "initial") {
779
+ const url = new URL(window.location.href);
780
+ routeInternal.value.dest = url;
781
+ routeLocation.url = url;
782
+ }
783
+ const lastDest = routeInternal.value.dest;
784
+ const dest = path === void 0 ? lastDest : typeof path === "number" ? path : toUrl(path, routeLocation.url);
785
+ if (navResolver.p && !forceReload && typeof dest !== "number" && isSamePath(dest, lastDest) && dest.href === lastDest.href) return navResolver.p;
786
+ const navCount = ++internalState.navCount;
787
+ internalState.currentTransition?.skipTransition();
788
+ if (preventNav.$cbs$ && (forceReload || typeof dest === "number" || !isSamePath(dest, lastDest) || !isSameOrigin(dest, lastDest))) {
789
+ const prevents = await Promise.all([...preventNav.$cbs$.values()].map((cb) => cb(dest)));
790
+ if (navCount !== internalState.navCount || prevents.some(Boolean)) {
791
+ if (navCount === internalState.navCount && type === "popstate") history.pushState(null, "", lastDest);
792
+ return;
793
+ }
794
+ }
795
+ if (typeof dest === "number") {
796
+ if (isBrowser) history.go(dest);
797
+ return;
798
+ }
799
+ if (!isSameOrigin(dest, lastDest)) {
800
+ if (isBrowser) location.href = dest.href;
801
+ return;
802
+ }
803
+ if (!forceReload && isSamePath(dest, lastDest)) {
804
+ if (isBrowser) {
805
+ if (type === "link" && dest.href !== location.href) history.pushState(null, "", dest);
806
+ const scroller = getScroller();
807
+ restoreScroll(type, dest, new URL(location.href), scroller, getScrollHistory());
808
+ if (type === "popstate") window._qRouterScrollEnabled = true;
809
+ }
810
+ if (dest.href !== routeLocation.url.href) {
811
+ const newUrl = new URL(dest.href);
812
+ routeInternal.value.dest = newUrl;
813
+ routeLocation.url = newUrl;
814
+ }
815
+ return;
816
+ }
817
+ let historyUpdated = false;
818
+ if (isBrowser && type === "link" && !forceReload) {
819
+ const scroller = getScroller();
820
+ window._qRouterScrollEnabled = false;
821
+ clearTimeout(window._qRouterScrollDebounce);
822
+ saveScrollHistory(currentScrollState(scroller));
823
+ clientNavigate(window, type, new URL(location.href), dest, replaceState);
824
+ historyUpdated = true;
825
+ }
826
+ routeLocation.isNavigating = true;
827
+ const container = isBrowser ? _getContextContainer() : void 0;
828
+ if (container) {
829
+ await _waitUntilRendered(container);
830
+ if (navCount !== internalState.navCount) return;
831
+ }
832
+ actionState.value = void 0;
833
+ routeInternal.value = {
834
+ type,
835
+ dest,
836
+ forceReload,
837
+ replaceState,
838
+ scroll,
839
+ historyUpdated
840
+ };
841
+ if (isBrowser) prefetchRoute(dest, true, .8, manifestHash);
842
+ navResolver.p = new Promise((resolve) => {
843
+ navResolver.r = () => {
844
+ navResolver.r = void 0;
845
+ navResolver.p = void 0;
846
+ resolve();
847
+ };
848
+ });
849
+ return navResolver.p;
850
+ }, "useQwikRouter_goto_8j8Vrz2yUIM", [
851
+ actionState,
852
+ manifestHash,
853
+ navResolver,
854
+ routeInternal,
855
+ routeLocation
856
+ ]);
857
+ useContextProvider(ContentContext, content);
858
+ useContextProvider(ContentInternalContext, contentInternal);
859
+ useContextProvider(DocumentHeadContext, documentHead);
860
+ useContextProvider(HttpStatusContext, httpStatus);
861
+ useContextProvider(RouteLocationContext, routeLocation);
862
+ useContextProvider(RouteNavigateContext, goto);
863
+ useContextProvider(RouteStateContext, loaderState);
864
+ useContextProvider(RouteLoaderCtxContext, routeLoaderCtx);
865
+ useContextProvider(RouteActionContext, actionState);
866
+ useContextProvider(RoutePreventNavigateContext, registerPreventNav);
867
+ /**
868
+ * This is split in 3 tasks because we need to update the head once we figured out the route, and
869
+ * before we trigger the render, and we need to subscribe only head to loader signal updates
870
+ */ useTaskQrl(/*#__PURE__*/ inlinedQrl(async ({ track }) => {
871
+ const actionDataSignal = _captures[0], actionState = _captures[1], content = _captures[2], contentInternal = _captures[3], env = _captures[4], goto = _captures[5], httpStatus = _captures[6], loaderState = _captures[7], navContext = _captures[8], routeInternal = _captures[9], routeLoaderCtx = _captures[10], routeLocation = _captures[11], routeLocationTarget = _captures[12];
872
+ const navigation = track(routeInternal);
873
+ const action = track(actionState);
874
+ const prevUrl = routeLocation.url;
875
+ const navType = action ? "form" : navigation.type;
876
+ const replaceState = navigation.replaceState;
877
+ const navCountBefore = internalState.navCount;
878
+ let trackUrl;
879
+ let endpointResponse;
880
+ let actionData;
881
+ let actionLoaderHashes;
882
+ let shouldInvalidateActionLoaders = false;
883
+ let loadedRoute;
884
+ if (isServer) {
885
+ trackUrl = new URL(navigation.dest, routeLocation.url);
886
+ loadedRoute = env.loadedRoute;
887
+ endpointResponse = env.response;
888
+ actionData = endpointResponse;
889
+ } else {
890
+ trackUrl = new URL(navigation.dest, location);
891
+ if (trackUrl.pathname.endsWith("/")) {
892
+ if (globalThis.__NO_TRAILING_SLASH__) trackUrl.pathname = trackUrl.pathname.slice(0, -1);
893
+ } else if (!globalThis.__NO_TRAILING_SLASH__) trackUrl.pathname = ensureSlash(trackUrl.pathname);
894
+ const loadRoutePromise = loadRoute(qwikRouterConfig.routes, qwikRouterConfig.cacheModules, trackUrl.pathname);
895
+ try {
896
+ loadedRoute = await loadRoutePromise;
897
+ } catch (e) {
898
+ console.error(`Could not load route ${trackUrl.pathname}, reloading:`, e);
899
+ window.location.href = trackUrl.href;
900
+ return;
901
+ }
902
+ if (internalState.navCount !== navCountBefore) return;
903
+ if (action) {
904
+ const result = await submitAction(action, trackUrl);
905
+ if (!result) {
906
+ routeInternal.untrackedValue = {
907
+ type: navType,
908
+ dest: trackUrl
909
+ };
910
+ return;
911
+ }
912
+ if (action.resolve) {
913
+ action.resolve({
914
+ status: result.status,
915
+ result: result.result
916
+ });
917
+ action.resolve = void 0;
918
+ }
919
+ if (result.redirect) {
920
+ goto(result.redirect, { replaceState: true });
921
+ return;
922
+ }
923
+ actionData = {
924
+ status: result.status,
925
+ action: action.id,
926
+ actionResult: result.result
927
+ };
928
+ actionLoaderHashes = result.loaderHashes;
929
+ shouldInvalidateActionLoaders = true;
930
+ }
931
+ }
932
+ const { $routeName$, $params$, $mods$, $menu$, $notFound$ } = loadedRoute;
933
+ const contentModules = $mods$;
934
+ if (!isServer) routeLoaderCtx.goto = noSerialize(goto);
935
+ updateRouteLoaderPaths(routeLoaderCtx, loadedRoute.$loaderPaths$, trackUrl);
936
+ const routeLoaders = ensureRouteLoaderSignals(contentModules, loaderState, routeLoaderCtx);
937
+ if (!isServer) invalidateNavRouteLoaders(loaderState);
938
+ if (shouldInvalidateActionLoaders) {
939
+ if (actionLoaderHashes !== void 0) for (const hash of actionLoaderHashes) loaderState[hash]?.invalidate(true);
940
+ else for (const loader of routeLoaders) loaderState[loader.__id].invalidate(true);
941
+ }
942
+ if (routeLoaders.length > 0) for (let i = 0; i < routeLoaders.length; i++) {
943
+ const loader = routeLoaders[i];
944
+ if (!isImmutableLoader(loader.__id)) loaderState[loader.__id].untrackedPending;
945
+ }
946
+ if (!isServer) clearNavFetchCache();
947
+ if (internalState.navCount !== navCountBefore) return;
948
+ if ($notFound$) httpStatus.value = {
949
+ status: 404,
950
+ message: "Not Found"
951
+ };
952
+ else if (endpointResponse) httpStatus.value = {
953
+ status: endpointResponse.status,
954
+ message: endpointResponse.statusMessage ?? "OK"
955
+ };
956
+ else if (actionData) httpStatus.value = {
957
+ status: actionData.status,
958
+ message: "OK"
959
+ };
960
+ else httpStatus.value = {
961
+ status: 200,
962
+ message: "OK"
963
+ };
964
+ const pageModule = contentModules[contentModules.length - 1];
965
+ if (navigation.dest.search && !!isSamePath(trackUrl, prevUrl)) trackUrl.search = navigation.dest.search;
966
+ let shouldForcePrevUrl = false;
967
+ let shouldForceUrl = false;
968
+ let shouldForceParams = false;
969
+ if (!isSamePath(trackUrl, prevUrl)) {
970
+ if (_hasStoreEffects(routeLocation, "prevUrl")) shouldForcePrevUrl = true;
971
+ routeLocationTarget.prevUrl = prevUrl;
972
+ }
973
+ if (routeLocationTarget.url !== trackUrl) {
974
+ if (_hasStoreEffects(routeLocation, "url")) shouldForceUrl = true;
975
+ routeLocationTarget.url = trackUrl;
976
+ }
977
+ if (routeLocationTarget.params !== $params$) {
978
+ if (_hasStoreEffects(routeLocation, "params")) shouldForceParams = true;
979
+ routeLocationTarget.params = $params$;
980
+ }
981
+ const nextRouteInternal = {
982
+ type: navType,
983
+ dest: trackUrl
984
+ };
985
+ if (navigation.forceReload !== void 0) nextRouteInternal.forceReload = navigation.forceReload;
986
+ if (navigation.replaceState !== void 0) nextRouteInternal.replaceState = navigation.replaceState;
987
+ if (navigation.scroll !== void 0) nextRouteInternal.scroll = navigation.scroll;
988
+ if (navigation.historyUpdated !== void 0) nextRouteInternal.historyUpdated = navigation.historyUpdated;
989
+ routeInternal.untrackedValue = nextRouteInternal;
990
+ content.headings = pageModule.headings;
991
+ content.menu = $menu$;
992
+ contentInternal.untrackedValue = noSerialize(contentModules);
993
+ actionDataSignal.value = actionData;
994
+ navContext.value = noSerialize({
995
+ routeName: $routeName$,
996
+ navType,
997
+ prevUrl,
998
+ replaceState,
999
+ shouldForcePrevUrl,
1000
+ shouldForceUrl,
1001
+ shouldForceParams,
1002
+ navCount: navCountBefore
1003
+ });
1004
+ }, "useQwikRouter_useTask_XpalYii770E", [
1005
+ actionDataSignal,
1006
+ actionState,
1007
+ content,
1008
+ contentInternal,
1009
+ env,
1010
+ goto,
1011
+ httpStatus,
1012
+ loaderState,
1013
+ navContext,
1014
+ routeInternal,
1015
+ routeLoaderCtx,
1016
+ routeLocation,
1017
+ routeLocationTarget
1018
+ ]), { deferUpdates: isServer });
1019
+ /**
1020
+ * Calc head. This is in a separate task so that loader updates can trigger head recalculation
1021
+ * without re-running the navigation logic.
1022
+ *
1023
+ * Note that on the server these tasks run sequentially.
1024
+ */ useTaskQrl(/*#__PURE__*/ inlinedQrl(({ track }) => {
1025
+ const actionDataSignal = _captures[0], contentInternal = _captures[1], documentHead = _captures[2], loaderState = _captures[3], routeLocation = _captures[4], serverHead = _captures[5];
1026
+ const contentModules = track(contentInternal);
1027
+ if (!contentModules) return;
1028
+ const actionData = track(actionDataSignal);
1029
+ const head = track(() => resolveHead(actionData, loaderState, routeLocation, contentModules, getLocale(""), serverHead));
1030
+ documentHead.links = head.links;
1031
+ documentHead.meta = head.meta;
1032
+ documentHead.styles = head.styles;
1033
+ documentHead.scripts = head.scripts;
1034
+ documentHead.title = head.title;
1035
+ documentHead.frontmatter = head.frontmatter;
1036
+ }, "useQwikRouter_useTask_1_1sKEZ9Pg1ow", [
1037
+ actionDataSignal,
1038
+ contentInternal,
1039
+ documentHead,
1040
+ loaderState,
1041
+ routeLocation,
1042
+ serverHead
1043
+ ]), { deferUpdates: isServer });
1044
+ /** Actual navigation */ useTaskQrl(/*#__PURE__*/ inlinedQrl(({ track }) => {
1045
+ const contentInternal = _captures[0], goto = _captures[1], manifestHash = _captures[2], navContext = _captures[3], navResolver = _captures[4], props = _captures[5], routeInternal = _captures[6], routeLocation = _captures[7];
1046
+ const nav = track(navContext);
1047
+ if (isServer || !nav) return;
1048
+ if (nav.navCount !== internalState.navCount) return;
1049
+ const container = _getContextContainer();
1050
+ const navigation = routeInternal.untrackedValue;
1051
+ const { navType, prevUrl, replaceState, routeName } = nav;
1052
+ const trackUrl = routeLocation.url;
1053
+ const scroller = getScroller();
1054
+ let scrollState;
1055
+ if (navType === "popstate") scrollState = getScrollHistory();
1056
+ if (navigation.scroll && (!navigation.forceReload || !isSamePath(trackUrl, prevUrl)) && (navType === "link" || navType === "popstate") || navType === "form" && !isSamePath(trackUrl, prevUrl)) document.__q_scroll_restore__ = () => restoreScroll(navType, trackUrl, prevUrl, scroller, scrollState);
1057
+ initializeSPA(goto, scroller);
1058
+ if (navType !== "popstate") {
1059
+ window._qRouterScrollEnabled = false;
1060
+ clearTimeout(window._qRouterScrollDebounce);
1061
+ if (!navigation.historyUpdated) saveScrollHistory(currentScrollState(scroller));
1062
+ }
1063
+ let didNavigate = false;
1064
+ let navigatePromise;
1065
+ let currentTransition;
1066
+ const navigate = () => {
1067
+ if (navigatePromise) return navigatePromise;
1068
+ if (nav.navCount !== internalState.navCount) return Promise.resolve();
1069
+ didNavigate = true;
1070
+ if (navigation.historyUpdated) {
1071
+ const currentPath = location.pathname + location.search + location.hash;
1072
+ const nextPath = toPath(trackUrl);
1073
+ if (currentPath !== nextPath) history.replaceState(history.state, "", nextPath);
1074
+ } else clientNavigate(window, navType, prevUrl, trackUrl, replaceState);
1075
+ contentInternal.trigger();
1076
+ return navigatePromise = _waitUntilRendered(container);
1077
+ };
1078
+ const _waitNextPage = () => {
1079
+ if (!shouldStartViewTransition(props?.viewTransition)) return navigate().then(() => void 0);
1080
+ const { ready, transition } = startViewTransition({
1081
+ update: navigate,
1082
+ types: ["qwik-navigation"]
1083
+ });
1084
+ currentTransition = transition;
1085
+ internalState.currentTransition = transition;
1086
+ return ready.then(() => transition, (reason) => {
1087
+ if (!didNavigate && nav.navCount === internalState.navCount) return navigate().then(() => transition);
1088
+ if (reason?.name === "AbortError") return transition;
1089
+ throw reason;
1090
+ });
1091
+ };
1092
+ _waitNextPage().finally(() => {
1093
+ if (currentTransition && internalState.currentTransition === currentTransition) internalState.currentTransition = void 0;
1094
+ if (nav.navCount !== internalState.navCount) return;
1095
+ container.element.setAttribute?.(Q_ROUTE, routeName);
1096
+ saveScrollHistory(currentScrollState(scroller));
1097
+ window._qRouterScrollEnabled = true;
1098
+ callRestoreScrollOnDocument();
1099
+ refreshLinkPrefetchObserver(manifestHash);
1100
+ if (nav.shouldForcePrevUrl) forceStoreEffects(routeLocation, "prevUrl");
1101
+ if (nav.shouldForceUrl) forceStoreEffects(routeLocation, "url");
1102
+ if (nav.shouldForceParams) forceStoreEffects(routeLocation, "params");
1103
+ routeLocation.isNavigating = false;
1104
+ navResolver.r?.();
1105
+ });
1106
+ }, "useQwikRouter_useTask_2_yw17chooOac", [
1107
+ contentInternal,
1108
+ goto,
1109
+ manifestHash,
1110
+ navContext,
1111
+ navResolver,
1112
+ props,
1113
+ routeInternal,
1114
+ routeLocation
1115
+ ]), { deferUpdates: false });
1116
+ };
1117
+ /** @public This is a wrapper around the `useQwikRouter()` hook. We recommend using the hook instead of this component, unless you have a good reason to make your root component reactive. */ var QwikRouterProvider = /*#__PURE__*/ componentQrl(/*#__PURE__*/ inlinedQrl((props) => {
1118
+ useQwikRouter(props);
1119
+ return /*#__PURE__*/ _jsxSorted(Slot, null, null, null, 3, "5y_0");
1120
+ }, "QwikRouterProvider_component_6Kjfa79mqlY"));
1121
+ /**
1122
+ * @deprecated Use `useQwikRouter()` instead. Will be removed in v3.
1123
+ * @public
1124
+ */ var QwikCityProvider = QwikRouterProvider;
1125
+ var handleRouterPopstate = (nav, event) => {
1126
+ const href = event.detail?.href;
1127
+ if (href) return nav(href, { type: "popstate" });
1521
1128
  };
1522
- const globalActionQrl = (actionQrl, ...rest) => {
1523
- const action = routeActionQrl(actionQrl, ...rest);
1524
- if (isServer) {
1525
- if (typeof globalThis._qwikActionsMap === "undefined") {
1526
- globalThis._qwikActionsMap = /* @__PURE__ */ new Map();
1527
- }
1528
- globalThis._qwikActionsMap.set(action.__id, action);
1529
- }
1530
- return action;
1129
+ /** @public */ var RouterOutlet = /*#__PURE__*/ componentQrl(/*#__PURE__*/ inlinedQrl(() => {
1130
+ if (!useServerData("containerAttributes")) throw new Error("PrefetchServiceWorker component must be rendered on the server.");
1131
+ const internalContext = useContext(ContentInternalContext);
1132
+ const nav = useNavigate();
1133
+ const contents = internalContext.value;
1134
+ if (contents && contents.length > 0) {
1135
+ const contentsLen = contents.length;
1136
+ let cmp = null;
1137
+ for (let i = contentsLen - 1; i >= 0; i--) if (contents[i].default) cmp = _jsxSorted(contents[i].default, null, null, cmp, 1, "Fn_0");
1138
+ return /*#__PURE__*/ _jsxSorted(Fragment, null, null, [cmp, !__EXPERIMENTAL__.noSPA && /*#__PURE__*/ _jsxSorted("script", {
1139
+ "q-d:qinit": _qrlSync(() => {
1140
+ ((w, h) => {
1141
+ if (!w._qcs) {
1142
+ w._qcs = !0;
1143
+ const s = h.state?._qRouterScroll;
1144
+ if (s) {
1145
+ h.scrollRestoration = "manual";
1146
+ w.scrollTo(s.x, s.y);
1147
+ }
1148
+ document.dispatchEvent(new Event("qcinit"));
1149
+ }
1150
+ })(window, history);
1151
+ }, "()=>{((w,h)=>{if(!w._qcs){w._qcs=!0;const s=h.state?._qRouterScroll;if(s){h.scrollRestoration=\"manual\";w.scrollTo(s.x,s.y);}document.dispatchEvent(new Event(\"qcinit\"));}})(window,history);}"),
1152
+ "q-d:qrouterpopstate": /*#__PURE__*/ inlinedQrl((event, _1, nav) => handleRouterPopstate(nav, event), "RouterOutlet_component_Fragment_script_q_d_qrouterpopstate_aRL6GvFyews").m(),
1153
+ "q:p": nav
1154
+ }, { "q-d:qcinit": [spa_init_default, linkPrefetchInit] }, null, 6, "Fn_1")], 1, "Fn_2");
1155
+ }
1156
+ return SkipRender;
1157
+ }, "RouterOutlet_component_QwONcWD5gIg"));
1158
+ /** @internal */ var validatorQrl = (validator) => {
1159
+ if (isServer) return { validate: validator };
1531
1160
  };
1532
- const routeAction$ = /* @__PURE__ */ implicit$FirstArg(routeActionQrl);
1533
- const globalAction$ = /* @__PURE__ */ implicit$FirstArg(globalActionQrl);
1534
- const routeLoaderQrl = (loaderQrl, ...rest) => {
1535
- const { id, validators, serializationStrategy } = getValidators(rest, loaderQrl);
1536
- function loader() {
1537
- const iCtx = _useInvokeContext();
1538
- const state = iCtx.$container$.resolveContext(iCtx.$hostElement$, RouteStateContext);
1539
- if (!(id in state)) {
1540
- throw new Error(`routeLoader$ "${loaderQrl.getSymbol()}" was invoked in a route where it was not declared.
1541
- This is because the routeLoader$ was not exported in a 'layout.tsx' or 'index.tsx' file of the existing route.
1542
- For more information check: https://qwik.dev/docs/route-loader/
1543
-
1544
- If your are managing reusable logic or a library it is essential that this function is re-exported from within 'layout.tsx' or 'index.tsx file of the existing route otherwise it will not run or throw exception.
1545
- For more information check: https://qwik.dev/docs/re-exporting-loaders/`);
1546
- }
1547
- untrack(() => state[id].value);
1548
- return state[id];
1549
- }
1550
- loader.__brand = "server_loader";
1551
- loader.__qrl = loaderQrl;
1552
- loader.__validators = validators;
1553
- loader.__id = id;
1554
- loader.__serializationStrategy = serializationStrategy;
1555
- Object.freeze(loader);
1556
- return loader;
1161
+ /** @public */ var validator$ = /*#__PURE__*/ implicit$FirstArg(validatorQrl);
1162
+ var flattenValibotIssues = (issues) => {
1163
+ return issues.reduce((acc, issue) => {
1164
+ if (issue.path) {
1165
+ if (issue.path.some((path) => path.type === "array")) {
1166
+ const keySuffix = issue.expected === "Array" ? "[]" : "";
1167
+ const key = issue.path.map((item) => item.type === "array" ? "*" : item.key).join(".").replace(/\.\*/g, "[]") + keySuffix;
1168
+ acc[key] = acc[key] || [];
1169
+ if (Array.isArray(acc[key])) acc[key].push(issue.message);
1170
+ return acc;
1171
+ } else acc[issue.path.map((item) => item.key).join(".")] = issue.message;
1172
+ }
1173
+ return acc;
1174
+ }, {});
1557
1175
  };
1558
- const routeLoader$ = /* @__PURE__ */ implicit$FirstArg(routeLoaderQrl);
1559
- const validatorQrl = (validator) => {
1560
- if (isServer) {
1561
- return {
1562
- validate: validator
1563
- };
1564
- }
1565
- return void 0;
1176
+ /** @internal */ var valibotQrl = (qrl) => {
1177
+ if (!__EXPERIMENTAL__.valibot) throw new Error("Valibot is an experimental feature and is not enabled. Please enable the feature flag by adding `experimental: [\"valibot\"]` to your qwikVite plugin options.");
1178
+ if (isServer) return {
1179
+ __brand: "valibot",
1180
+ async validate(ev, inputData) {
1181
+ const schema = await qrl.resolve().then((obj) => typeof obj === "function" ? obj(ev) : obj);
1182
+ const data = inputData ?? await ev.parseBody();
1183
+ const result = await v.safeParseAsync(schema, data);
1184
+ if (result.success) return {
1185
+ success: true,
1186
+ data: result.output
1187
+ };
1188
+ else {
1189
+ if (isDev) console.error("ERROR: Valibot validation failed", result.issues);
1190
+ return {
1191
+ success: false,
1192
+ status: 400,
1193
+ error: {
1194
+ formErrors: v.flatten(result.issues).root ?? [],
1195
+ fieldErrors: flattenValibotIssues(result.issues)
1196
+ }
1197
+ };
1198
+ }
1199
+ }
1200
+ };
1566
1201
  };
1567
- const validator$ = /* @__PURE__ */ implicit$FirstArg(validatorQrl);
1568
- const flattenValibotIssues = (issues) => {
1569
- return issues.reduce((acc, issue) => {
1570
- if (issue.path) {
1571
- const hasArrayType = issue.path.some((path) => path.type === "array");
1572
- if (hasArrayType) {
1573
- const keySuffix = issue.expected === "Array" ? "[]" : "";
1574
- const key = issue.path.map((item) => item.type === "array" ? "*" : item.key).join(".").replace(/\.\*/g, "[]") + keySuffix;
1575
- acc[key] = acc[key] || [];
1576
- if (Array.isArray(acc[key])) {
1577
- acc[key].push(issue.message);
1578
- }
1579
- return acc;
1580
- } else {
1581
- acc[issue.path.map((item) => item.key).join(".")] = issue.message;
1582
- }
1583
- }
1584
- return acc;
1585
- }, {});
1202
+ /** @beta */ var valibot$ = /*#__PURE__*/ implicit$FirstArg(valibotQrl);
1203
+ var flattenZodIssues = (issues) => {
1204
+ issues = Array.isArray(issues) ? issues : [issues];
1205
+ return issues.reduce((acc, issue) => {
1206
+ const isExpectingArray = "expected" in issue && issue.expected === "array";
1207
+ if (issue.path.some((path) => typeof path === "number") || isExpectingArray) {
1208
+ const keySuffix = "expected" in issue && issue.expected === "array" ? "[]" : "";
1209
+ const key = issue.path.map((path) => typeof path === "number" ? "*" : path).join(".").replace(/\.\*/g, "[]") + keySuffix;
1210
+ acc[key] = acc[key] || [];
1211
+ if (Array.isArray(acc[key])) acc[key].push(issue.message);
1212
+ return acc;
1213
+ } else acc[issue.path.join(".")] = issue.message;
1214
+ return acc;
1215
+ }, {});
1586
1216
  };
1587
- const valibotQrl = (qrl) => {
1588
- if (!__EXPERIMENTAL__.valibot) {
1589
- throw new Error('Valibot is an experimental feature and is not enabled. Please enable the feature flag by adding `experimental: ["valibot"]` to your qwikVite plugin options.');
1590
- }
1591
- if (isServer) {
1592
- return {
1593
- __brand: "valibot",
1594
- async validate(ev, inputData) {
1595
- const schema = await qrl.resolve().then((obj) => typeof obj === "function" ? obj(ev) : obj);
1596
- const data = inputData ?? await ev.parseBody();
1597
- const result = await safeParseAsync(schema, data);
1598
- if (result.success) {
1599
- return {
1600
- success: true,
1601
- data: result.output
1602
- };
1603
- } else {
1604
- if (isDev) {
1605
- console.error("ERROR: Valibot validation failed", result.issues);
1606
- }
1607
- return {
1608
- success: false,
1609
- status: 400,
1610
- error: {
1611
- formErrors: flatten(result.issues).root ?? [],
1612
- fieldErrors: flattenValibotIssues(result.issues)
1613
- }
1614
- };
1615
- }
1616
- }
1617
- };
1618
- }
1619
- return void 0;
1217
+ /** @internal */ var zodQrl = (qrl) => {
1218
+ if (isServer) return {
1219
+ __brand: "zod",
1220
+ async validate(ev, inputData) {
1221
+ const schema = await qrl.resolve().then((obj) => {
1222
+ if (typeof obj === "function") obj = obj(z$1, ev);
1223
+ if (obj instanceof z$1.Schema) return obj;
1224
+ else return z$1.object(obj);
1225
+ });
1226
+ const data = inputData ?? await ev.parseBody();
1227
+ const result = await withLocale(ev.locale(), () => schema.safeParseAsync(data));
1228
+ if (result.success) return result;
1229
+ else {
1230
+ if (isDev) console.error("ERROR: Zod validation failed", result.error.issues);
1231
+ return {
1232
+ success: false,
1233
+ status: 400,
1234
+ error: {
1235
+ formErrors: result.error.flatten().formErrors,
1236
+ fieldErrors: flattenZodIssues(result.error.issues)
1237
+ }
1238
+ };
1239
+ }
1240
+ }
1241
+ };
1620
1242
  };
1621
- const valibot$ = /* @__PURE__ */ implicit$FirstArg(valibotQrl);
1622
- const flattenZodIssues = (issues) => {
1623
- issues = Array.isArray(issues) ? issues : [
1624
- issues
1625
- ];
1626
- return issues.reduce((acc, issue) => {
1627
- const isExpectingArray = "expected" in issue && issue.expected === "array";
1628
- const hasArrayType = issue.path.some((path) => typeof path === "number") || isExpectingArray;
1629
- if (hasArrayType) {
1630
- const keySuffix = "expected" in issue && issue.expected === "array" ? "[]" : "";
1631
- const key = issue.path.map((path) => typeof path === "number" ? "*" : path).join(".").replace(/\.\*/g, "[]") + keySuffix;
1632
- acc[key] = acc[key] || [];
1633
- if (Array.isArray(acc[key])) {
1634
- acc[key].push(issue.message);
1635
- }
1636
- return acc;
1637
- } else {
1638
- acc[issue.path.join(".")] = issue.message;
1639
- }
1640
- return acc;
1641
- }, {});
1243
+ /** @public */ var zod$ = /*#__PURE__*/ implicit$FirstArg(zodQrl);
1244
+ var getValidators = (rest, qrl) => {
1245
+ let id;
1246
+ let invalidate;
1247
+ const validators = [];
1248
+ if (rest.length === 1) {
1249
+ const options = rest[0];
1250
+ if (options && typeof options === "object") {
1251
+ if ("validate" in options) validators.push(options);
1252
+ else {
1253
+ id = options.id;
1254
+ if (options.validation) validators.push(...options.validation);
1255
+ if (options.invalidate) invalidate = options.invalidate.map((loader) => loader.__id);
1256
+ }
1257
+ }
1258
+ } else if (rest.length > 1) validators.push(...rest.filter((v) => !!v));
1259
+ if (typeof id === "string") {
1260
+ if (isDev) {
1261
+ if (!/^[\w/.-]+$/.test(id)) throw new Error(`Invalid id: ${id}, id can only contain [a-zA-Z0-9_.-]`);
1262
+ }
1263
+ id = `id_${id}`;
1264
+ } else id = qrl.getHash();
1265
+ return {
1266
+ validators: validators.reverse(),
1267
+ id,
1268
+ invalidate
1269
+ };
1642
1270
  };
1643
- const zodQrl = (qrl) => {
1644
- if (isServer) {
1645
- return {
1646
- __brand: "zod",
1647
- async validate(ev, inputData) {
1648
- const schema = await qrl.resolve().then((obj) => {
1649
- if (typeof obj === "function") {
1650
- obj = obj(z, ev);
1651
- }
1652
- if (obj instanceof z.Schema) {
1653
- return obj;
1654
- } else {
1655
- return z.object(obj);
1656
- }
1657
- });
1658
- const data = inputData ?? await ev.parseBody();
1659
- const result = await schema.safeParseAsync(data);
1660
- if (result.success) {
1661
- return result;
1662
- } else {
1663
- if (isDev) {
1664
- console.error("ERROR: Zod validation failed", result.error.issues);
1665
- }
1666
- return {
1667
- success: false,
1668
- status: 400,
1669
- error: {
1670
- formErrors: result.error.flatten().formErrors,
1671
- fieldErrors: flattenZodIssues(result.error.issues)
1672
- }
1673
- };
1674
- }
1675
- }
1676
- };
1677
- }
1678
- return void 0;
1271
+ /** @public */ var routeActionQrl = (actionQrl, ...rest) => {
1272
+ const { id, validators, invalidate } = getValidators(rest, actionQrl);
1273
+ function action() {
1274
+ const loc = useLocation();
1275
+ const currentAction = useAction();
1276
+ const initialState = {
1277
+ actionPath: `?${QACTION_KEY}=${id}`,
1278
+ submitted: false,
1279
+ isRunning: false,
1280
+ status: void 0,
1281
+ value: void 0,
1282
+ formData: void 0
1283
+ };
1284
+ const state = useStore(() => {
1285
+ const value = currentAction.value;
1286
+ if (value && value?.id === id) {
1287
+ const data = value.data;
1288
+ if (data instanceof FormData) initialState.formData = data;
1289
+ if (value.output) {
1290
+ const { status, result } = value.output;
1291
+ initialState.status = status;
1292
+ initialState.value = result;
1293
+ }
1294
+ }
1295
+ return initialState;
1296
+ });
1297
+ initialState.submit = /* @__PURE__ */ inlinedQrl((input = {}) => {
1298
+ const currentAction = _captures[0], id = _captures[1], loc = _captures[2], state = _captures[3];
1299
+ if (isServer) throw new Error(`Actions can not be invoked within the server during SSR.
1300
+ Action.run() can only be called on the browser, for example when a user clicks a button, or submits a form.`);
1301
+ let data;
1302
+ let form;
1303
+ if (input instanceof SubmitEvent) {
1304
+ form = input.target;
1305
+ data = new FormData(form);
1306
+ if ((input.submitter instanceof HTMLInputElement || input.submitter instanceof HTMLButtonElement) && input.submitter.name) {
1307
+ if (input.submitter.name) data.append(input.submitter.name, input.submitter.value);
1308
+ }
1309
+ } else data = input;
1310
+ return new Promise((resolve) => {
1311
+ if (data instanceof FormData) state.formData = data;
1312
+ state.submitted = true;
1313
+ state.isRunning = true;
1314
+ loc.isNavigating = true;
1315
+ currentAction.value = {
1316
+ data,
1317
+ id,
1318
+ resolve: noSerialize(resolve)
1319
+ };
1320
+ }).then((_rawProps) => {
1321
+ state.isRunning = false;
1322
+ state.status = _rawProps.status;
1323
+ state.value = _rawProps.result;
1324
+ if (form) {
1325
+ if (form.getAttribute("data-spa-reset") === "true") form.reset();
1326
+ const detail = {
1327
+ status: _rawProps.status,
1328
+ value: _rawProps.result
1329
+ };
1330
+ form.dispatchEvent(new CustomEvent("submitcompleted", {
1331
+ bubbles: false,
1332
+ cancelable: false,
1333
+ composed: false,
1334
+ detail
1335
+ }));
1336
+ }
1337
+ return {
1338
+ status: _rawProps.status,
1339
+ value: _rawProps.result
1340
+ };
1341
+ });
1342
+ }, "routeActionQrl_action_submit_YuS5bpdQ360", [
1343
+ currentAction,
1344
+ id,
1345
+ loc,
1346
+ state
1347
+ ]);
1348
+ return state;
1349
+ }
1350
+ action.__brand = "server_action";
1351
+ action.__validators = validators;
1352
+ action.__qrl = actionQrl;
1353
+ action.__id = id;
1354
+ action.__invalidate = invalidate;
1355
+ Object.freeze(action);
1356
+ return action;
1679
1357
  };
1680
- const zod$ = /* @__PURE__ */ implicit$FirstArg(zodQrl);
1681
- const serverQrl = (qrl, options) => {
1682
- if (isServer) {
1683
- const captured = qrl.getCaptured();
1684
- if (captured && captured.length > 0 && !_getContextElement()) {
1685
- throw new Error("For security reasons, we cannot serialize QRLs that capture lexical scope.");
1686
- }
1687
- }
1688
- const method = options?.method?.toUpperCase?.() || "POST";
1689
- const headers = options?.headers || {};
1690
- const origin = options?.origin || "";
1691
- const fetchOptions = options?.fetchOptions || {};
1692
- function rpc() {
1693
- return $(async function(...args) {
1694
- const abortSignal = args.length > 0 && args[0] instanceof AbortSignal ? args.shift() : void 0;
1695
- if (isServer) {
1696
- let requestEvent = globalThis.qcAsyncRequestStore?.getStore();
1697
- if (!requestEvent) {
1698
- const contexts = [
1699
- useQwikRouterEnv()?.ev,
1700
- this,
1701
- _getContextEvent()
1702
- ];
1703
- requestEvent = contexts.find((v2) => v2 && Object.prototype.hasOwnProperty.call(v2, "sharedMap") && Object.prototype.hasOwnProperty.call(v2, "cookie"));
1704
- }
1705
- return qrl.apply(requestEvent, isDev ? deepFreeze(args) : args);
1706
- } else {
1707
- const ctxElm = _getContextElement();
1708
- const filteredArgs = args.map((arg) => {
1709
- if (arg instanceof SubmitEvent && arg.target instanceof HTMLFormElement) {
1710
- return new FormData(arg.target);
1711
- } else if (arg instanceof Event) {
1712
- return null;
1713
- } else if (arg instanceof Node) {
1714
- return null;
1715
- }
1716
- return arg;
1717
- });
1718
- const qrlHash = qrl.getHash();
1719
- let query = "";
1720
- const config = {
1721
- ...fetchOptions,
1722
- method,
1723
- headers: {
1724
- ...headers,
1725
- "Content-Type": "application/qwik-json",
1726
- Accept: "application/json, application/qwik-json, text/qwik-json-stream, text/plain",
1727
- // Required so we don't call accidentally
1728
- "X-QRL": qrlHash
1729
- },
1730
- signal: abortSignal
1731
- };
1732
- const body = await _serialize([
1733
- qrl,
1734
- ...filteredArgs
1735
- ]);
1736
- if (method === "GET") {
1737
- query += `&${QDATA_KEY}=${encodeURIComponent(body)}`;
1738
- } else {
1739
- config.body = body;
1740
- }
1741
- const res = await fetch(`${origin}?${QFN_KEY}=${qrlHash}${query}`, config);
1742
- const contentType = res.headers.get("Content-Type");
1743
- if (res.ok && contentType === "text/qwik-json-stream" && res.body) {
1744
- return async function* () {
1745
- try {
1746
- for await (const result of deserializeStream(res.body, ctxElm ?? document.documentElement, abortSignal)) {
1747
- yield result;
1748
- }
1749
- } finally {
1750
- if (!abortSignal?.aborted) {
1751
- await res.body.cancel();
1752
- }
1753
- }
1754
- }();
1755
- } else if (contentType === "application/qwik-json") {
1756
- const str = await res.text();
1757
- const [obj] = _deserialize(str, ctxElm ?? document.documentElement);
1758
- if (res.status >= 400) {
1759
- throw obj;
1760
- }
1761
- return obj;
1762
- } else if (contentType === "application/json") {
1763
- const obj = await res.json();
1764
- if (res.status >= 400) {
1765
- throw obj;
1766
- }
1767
- return obj;
1768
- } else if (contentType === "text/plain" || contentType === "text/html") {
1769
- const str = await res.text();
1770
- if (res.status >= 400) {
1771
- throw str;
1772
- }
1773
- return str;
1774
- }
1775
- }
1776
- });
1777
- }
1778
- return rpc();
1358
+ /** @internal */ var globalActionQrl = (actionQrl, ...rest) => {
1359
+ const action = routeActionQrl(actionQrl, ...rest);
1360
+ if (isServer) {
1361
+ if (typeof globalThis._qwikActionsMap === "undefined") globalThis._qwikActionsMap = /* @__PURE__ */ new Map();
1362
+ globalThis._qwikActionsMap.set(action.__id, action);
1363
+ }
1364
+ return action;
1779
1365
  };
1780
- const server$ = /* @__PURE__ */ implicit$FirstArg(serverQrl);
1781
- const getValidators = (rest, qrl) => {
1782
- let id;
1783
- let serializationStrategy = DEFAULT_LOADERS_SERIALIZATION_STRATEGY;
1784
- const validators = [];
1785
- if (rest.length === 1) {
1786
- const options = rest[0];
1787
- if (options && typeof options === "object") {
1788
- if ("validate" in options) {
1789
- validators.push(options);
1790
- } else {
1791
- id = options.id;
1792
- if (options.serializationStrategy) {
1793
- serializationStrategy = options.serializationStrategy;
1794
- }
1795
- if (options.validation) {
1796
- validators.push(...options.validation);
1797
- }
1798
- }
1799
- }
1800
- } else if (rest.length > 1) {
1801
- validators.push(...rest.filter((v2) => !!v2));
1802
- }
1803
- if (typeof id === "string") {
1804
- if (isDev) {
1805
- if (!/^[\w/.-]+$/.test(id)) {
1806
- throw new Error(`Invalid id: ${id}, id can only contain [a-zA-Z0-9_.-]`);
1807
- }
1808
- }
1809
- id = `id_${id}`;
1810
- } else {
1811
- id = qrl.getHash();
1812
- }
1813
- return {
1814
- validators: validators.reverse(),
1815
- id,
1816
- serializationStrategy
1817
- };
1366
+ /**
1367
+ * Define a route action that handles form submissions or programmatic invocations.
1368
+ *
1369
+ * Actions run on the server when submitted from the client. The result is returned as an
1370
+ * `ActionStore` with `.value` for success data and `.error` for errors (including validation errors
1371
+ * from `zod$`/`valibot$` where `.fieldErrors` etc. are accessible directly on `.error`).
1372
+ *
1373
+ * By default, after an action completes, ALL current route loaders are invalidated on the client
1374
+ * and re-fetched as needed (so that the browser cache is correct). This can be controlled with:
1375
+ *
1376
+ * - `invalidate: [loader1, loader2]`: Only invalidate specific loaders. The client re-fetches them
1377
+ * individually. Other loaders keep their current data.
1378
+ * - `invalidate: []`: No loaders are invalidated. The action response only contains the action
1379
+ * result. Use this when the action doesn't affect any loader data.
1380
+ *
1381
+ * The `strictLoaders` Vite plugin option applies `invalidate: []` globally for all actions that
1382
+ * don't specify an explicit `invalidate` option.
1383
+ *
1384
+ * @public
1385
+ */ var routeAction$ = /*#__PURE__*/ implicit$FirstArg(routeActionQrl);
1386
+ /** @public */ var globalAction$ = /*#__PURE__*/ implicit$FirstArg(globalActionQrl);
1387
+ var deserializeStream = async function* (stream, abortSignal) {
1388
+ const reader = stream.getReader();
1389
+ try {
1390
+ let buffer = "";
1391
+ const decoder = new TextDecoder();
1392
+ while (!abortSignal?.aborted) {
1393
+ const result = await reader.read();
1394
+ if (result.done) break;
1395
+ buffer += decoder.decode(result.value, { stream: true });
1396
+ const lines = buffer.split(/\n/);
1397
+ buffer = lines.pop();
1398
+ for (const line of lines) yield await _deserialize(line);
1399
+ }
1400
+ } finally {
1401
+ reader.releaseLock();
1402
+ }
1818
1403
  };
1819
- const deserializeStream = async function* (stream, ctxElm, abortSignal) {
1820
- const reader = stream.getReader();
1821
- try {
1822
- let buffer = "";
1823
- const decoder = new TextDecoder();
1824
- while (!abortSignal?.aborted) {
1825
- const result = await reader.read();
1826
- if (result.done) {
1827
- break;
1828
- }
1829
- buffer += decoder.decode(result.value, {
1830
- stream: true
1831
- });
1832
- const lines = buffer.split(/\n/);
1833
- buffer = lines.pop();
1834
- for (const line of lines) {
1835
- const [deserializedData] = _deserialize(line, ctxElm);
1836
- yield deserializedData;
1837
- }
1838
- }
1839
- } finally {
1840
- reader.releaseLock();
1841
- }
1404
+ /** @internal */ var serverQrl = (qrl, options) => {
1405
+ if (isServer) {
1406
+ const captured = qrl.getCaptured();
1407
+ if (captured && captured.length > 0 && !_getContextHostElement()) throw new Error("For security reasons, we cannot serialize QRLs that capture lexical scope.");
1408
+ }
1409
+ const method = options?.method?.toUpperCase?.() || "POST";
1410
+ const headers = options?.headers || {};
1411
+ const origin = options?.origin || "";
1412
+ const fetchOptions = options?.fetchOptions || {};
1413
+ return /*#__PURE__*/ inlinedQrl(async function(...args) {
1414
+ const fetchOptions = _captures[0], headers = _captures[1], method = _captures[2], origin = _captures[3], qrl = _captures[4];
1415
+ const abortSignal = args.length > 0 && args[0] instanceof AbortSignal ? args.shift() : void 0;
1416
+ if (isServer) return qrl.apply(getRequestEvent(this), args);
1417
+ else {
1418
+ let filteredArgs = args.map((arg) => {
1419
+ if (arg instanceof SubmitEvent && arg.target instanceof HTMLFormElement) return new FormData(arg.target);
1420
+ else if (arg instanceof Event) return null;
1421
+ else if (arg instanceof Node) return null;
1422
+ return arg;
1423
+ });
1424
+ if (!filteredArgs.length) filteredArgs = void 0;
1425
+ const qrlHash = qrl.getHash();
1426
+ let query = "";
1427
+ const config = {
1428
+ ...fetchOptions,
1429
+ method,
1430
+ headers: {
1431
+ ...headers,
1432
+ "Content-Type": "application/qwik-json",
1433
+ Accept: "application/json, application/qwik-json, text/qwik-json-stream, text/plain",
1434
+ "X-QRL": qrlHash
1435
+ },
1436
+ signal: abortSignal
1437
+ };
1438
+ const captured = qrl.getCaptured();
1439
+ let toSend = [filteredArgs];
1440
+ if (captured?.length) toSend = [filteredArgs, ...captured];
1441
+ else toSend = filteredArgs ? [filteredArgs] : [];
1442
+ const body = await _serialize(toSend);
1443
+ if (method === "GET") query += `&${QDATA_KEY}=${encodeURIComponent(body)}`;
1444
+ else config.body = body;
1445
+ const res = await fetch(`${origin}?${QFN_KEY}=${qrlHash}${query}`, config);
1446
+ const contentType = res.headers.get("Content-Type");
1447
+ if (res.ok && contentType === "text/qwik-json-stream" && res.body) return async function* () {
1448
+ try {
1449
+ for await (const result of deserializeStream(res.body, abortSignal)) yield result;
1450
+ } finally {
1451
+ if (!abortSignal?.aborted) await res.body.cancel();
1452
+ }
1453
+ }();
1454
+ else if (contentType === "application/qwik-json") {
1455
+ const str = await res.text();
1456
+ const obj = await _deserialize(str);
1457
+ if (res.status >= 400) throw obj;
1458
+ return obj;
1459
+ } else if (contentType === "application/json") {
1460
+ const obj = await res.json();
1461
+ if (res.status >= 400) throw obj;
1462
+ return obj;
1463
+ } else if (contentType === "text/plain" || contentType === "text/html") {
1464
+ const str = await res.text();
1465
+ if (res.status >= 400) throw str;
1466
+ return str;
1467
+ }
1468
+ }
1469
+ }, "serverQrl_w03grD0Ag68", [
1470
+ fetchOptions,
1471
+ headers,
1472
+ method,
1473
+ origin,
1474
+ qrl
1475
+ ]);
1842
1476
  };
1843
- const ServiceWorkerRegister = (props) => /* @__PURE__ */ jsx("script", {
1844
- type: "module",
1845
- dangerouslySetInnerHTML: swRegister,
1846
- nonce: props.nonce
1847
- });
1848
- const Form = ({ action, spaReset, reloadDocument, onSubmit$, ...rest }, key) => {
1849
- if (action) {
1850
- const isArrayApi = Array.isArray(onSubmit$);
1851
- if (isArrayApi) {
1852
- return jsx$1("form", {
1853
- ...rest,
1854
- action: action.actionPath,
1855
- "preventdefault:submit": !reloadDocument,
1856
- onSubmit$: [
1857
- ...onSubmit$,
1858
- // action.submit "submitcompleted" event for onSubmitCompleted$ events
1859
- !reloadDocument ? $((evt) => {
1860
- if (!action.submitted) {
1861
- return action.submit(evt);
1862
- }
1863
- }) : void 0
1864
- ],
1865
- method: "post",
1866
- ["data-spa-reset"]: spaReset ? "true" : void 0
1867
- }, key);
1868
- }
1869
- return jsx$1("form", {
1870
- ...rest,
1871
- action: action.actionPath,
1872
- "preventdefault:submit": !reloadDocument,
1873
- onSubmit$: [
1874
- // Since v2, this fires before the action is executed so it can be prevented
1875
- onSubmit$,
1876
- // action.submit "submitcompleted" event for onSubmitCompleted$ events
1877
- !reloadDocument ? action.submit : void 0
1878
- ],
1879
- method: "post",
1880
- ["data-spa-reset"]: spaReset ? "true" : void 0
1881
- }, key);
1882
- } else {
1883
- return /* @__PURE__ */ jsx(GetForm, {
1884
- spaReset,
1885
- reloadDocument,
1886
- onSubmit$,
1887
- ...rest
1888
- }, key);
1889
- }
1477
+ /** @public */ var server$ = /*#__PURE__*/ implicit$FirstArg(serverQrl);
1478
+ /**
1479
+ * Loads the service workers that are defined in the routes. Any file named `service-worker.*` (all
1480
+ * JS extensions are allowed) will be picked up, bundled into a separate file, and registered as a
1481
+ * service worker.
1482
+ *
1483
+ * Qwik 1.14.0 and above now use `<link rel="modulepreload">` by default. If you didn't add custom
1484
+ * service-worker logic, you should remove your service-worker.ts file(s) for the
1485
+ * `ServiceWorkerRegister` Component to actually unregister the service-worker.js and delete its
1486
+ * related cache. Make sure to keep the `ServiceWorkerRegister` Component in your app (without any
1487
+ * service-worker.ts file) as long as you want to unregister the service-worker.js for your users.
1488
+ *
1489
+ * @public
1490
+ */ var ServiceWorkerRegister = (props) => /*#__PURE__*/ _jsxSorted("script", { nonce: _wrapProp(props, "nonce") }, {
1491
+ type: "module",
1492
+ dangerouslySetInnerHTML: swRegister
1493
+ }, null, 3, "1x_0");
1494
+ var _hf0 = (p0) => !p0.reloadDocument;
1495
+ var _hf0_str = "!p0.reloadDocument";
1496
+ var _hf1 = (p0) => p0.spaReset ? "true" : void 0;
1497
+ var _hf1_str = "p0.spaReset?\"true\":undefined";
1498
+ var GetForm = /*#__PURE__*/ componentQrl(/*#__PURE__*/ inlinedQrl((_rawProps) => {
1499
+ const rest = _restProps(_rawProps, [
1500
+ "action",
1501
+ "spaReset",
1502
+ "reloadDocument",
1503
+ "onSubmit$"
1504
+ ]);
1505
+ const nav = useNavigate();
1506
+ return /*#__PURE__*/ _jsxSplit("form", {
1507
+ action: "get",
1508
+ "preventdefault:submit": _fnSignal(_hf0, [_rawProps], _hf0_str),
1509
+ "data-spa-reset": _fnSignal(_hf1, [_rawProps], _hf1_str),
1510
+ ..._getVarProps(rest),
1511
+ ..._getConstProps(rest),
1512
+ "q-e:submit": [
1513
+ ...Array.isArray(_rawProps.onSubmit$) ? _rawProps.onSubmit$ : [_rawProps.onSubmit$],
1514
+ /*#__PURE__*/ inlinedQrl(async (_evt, form) => {
1515
+ const nav = _captures[0];
1516
+ const formData = new FormData(form);
1517
+ const params = new URLSearchParams();
1518
+ formData.forEach((value, key) => {
1519
+ if (typeof value === "string") params.append(key, value);
1520
+ });
1521
+ await nav("?" + params.toString(), {
1522
+ type: "form",
1523
+ forceReload: true
1524
+ });
1525
+ }, "GetForm_component_form_q_e_submit_r3dkP9d2cF8", [nav]),
1526
+ /*#__PURE__*/ inlinedQrl((_evt, form) => {
1527
+ if (form.getAttribute("data-spa-reset") === "true") form.reset();
1528
+ form.dispatchEvent(new CustomEvent("submitcompleted", {
1529
+ bubbles: false,
1530
+ cancelable: false,
1531
+ composed: false,
1532
+ detail: { status: 200 }
1533
+ }));
1534
+ }, "GetForm_component_form_q_e_submit_1_cuYklZAOHrA")
1535
+ ]
1536
+ }, null, /*#__PURE__*/ _jsxSorted(Slot, null, null, null, 3, "Q4_0"), 0, "Q4_1");
1537
+ }, "GetForm_component_2U5Z2Z8ryc0"));
1538
+ /** @public */ var Form = ({ action, spaReset, reloadDocument, onSubmit$, ...rest }, key) => {
1539
+ if (action) {
1540
+ if (Array.isArray(onSubmit$)) return _jsxSplit("form", {
1541
+ ..._getVarProps(rest),
1542
+ ..._getConstProps(rest),
1543
+ action: _wrapProp(action, "actionPath"),
1544
+ "preventdefault:submit": !reloadDocument,
1545
+ "q-e:submit": [...onSubmit$, !reloadDocument ? /*#__PURE__*/ inlinedQrl((evt) => {
1546
+ const action = _captures[0];
1547
+ if (!action.submitted) return action.submit(evt);
1548
+ }, "Form_form_q_e_submit_6i0Jq5q8JFg", [action]) : void 0],
1549
+ ["data-spa-reset"]: spaReset ? "true" : void 0
1550
+ }, { method: "post" }, null, 0, key);
1551
+ return _jsxSplit("form", {
1552
+ ..._getVarProps(rest),
1553
+ ..._getConstProps(rest),
1554
+ action: _wrapProp(action, "actionPath"),
1555
+ "preventdefault:submit": !reloadDocument,
1556
+ "q-e:submit": [onSubmit$, !reloadDocument ? action.submit : void 0],
1557
+ ["data-spa-reset"]: spaReset ? "true" : void 0
1558
+ }, { method: "post" }, null, 0, key);
1559
+ } else return /*#__PURE__*/ _jsxSplit(GetForm, {
1560
+ spaReset,
1561
+ reloadDocument,
1562
+ onSubmit$,
1563
+ ...rest
1564
+ }, null, null, 0, key);
1890
1565
  };
1891
- const GetForm = component$(({ action: _0, spaReset, reloadDocument, onSubmit$, ...rest }) => {
1892
- const nav = useNavigate();
1893
- return /* @__PURE__ */ jsx("form", {
1894
- action: "get",
1895
- "preventdefault:submit": !reloadDocument,
1896
- "data-spa-reset": spaReset ? "true" : void 0,
1897
- ...rest,
1898
- onSubmit$: [
1899
- ...Array.isArray(onSubmit$) ? onSubmit$ : [
1900
- onSubmit$
1901
- ],
1902
- $(async (_evt, form) => {
1903
- const formData = new FormData(form);
1904
- const params = new URLSearchParams();
1905
- formData.forEach((value, key) => {
1906
- if (typeof value === "string") {
1907
- params.append(key, value);
1908
- }
1909
- });
1910
- await nav("?" + params.toString(), {
1911
- type: "form",
1912
- forceReload: true
1913
- });
1914
- }),
1915
- $((_evt, form) => {
1916
- if (form.getAttribute("data-spa-reset") === "true") {
1917
- form.reset();
1918
- }
1919
- form.dispatchEvent(new CustomEvent("submitcompleted", {
1920
- bubbles: false,
1921
- cancelable: false,
1922
- composed: false,
1923
- detail: {
1924
- status: 200
1925
- }
1926
- }));
1927
- })
1928
- ],
1929
- children: /* @__PURE__ */ jsx(Slot, {})
1930
- });
1931
- });
1932
- const untypedAppUrl = function appUrl(route, params, paramsPrefix = "") {
1933
- const path = route.split("/");
1934
- for (let i = 0; i < path.length; i++) {
1935
- const segment = path[i];
1936
- if (segment.startsWith("[") && segment.endsWith("]")) {
1937
- const isSpread = segment.startsWith("[...");
1938
- const key = segment.substring(segment.startsWith("[...") ? 4 : 1, segment.length - 1);
1939
- const value = params ? params[paramsPrefix + key] || params[key] : "";
1940
- path[i] = isSpread ? value : encodeURIComponent(value);
1941
- }
1942
- if (segment.startsWith("(") && segment.endsWith(")")) {
1943
- path.splice(i, 1);
1944
- }
1945
- }
1946
- let url = path.join("/");
1947
- let baseURL = "/";
1948
- if (baseURL) {
1949
- if (!baseURL.endsWith("/")) {
1950
- baseURL += "/";
1951
- }
1952
- while (url.startsWith("/")) {
1953
- url = url.substring(1);
1954
- }
1955
- url = baseURL + url;
1956
- }
1957
- return url;
1566
+ /**
1567
+ * @beta
1568
+ * @experimental
1569
+ */ var untypedAppUrl = function appUrl(route, params, paramsPrefix = "") {
1570
+ const path = route.split("/");
1571
+ for (let i = 0; i < path.length; i++) {
1572
+ const segment = path[i];
1573
+ if (segment.startsWith("[") && segment.endsWith("]")) {
1574
+ const isSpread = segment.startsWith("[...");
1575
+ const key = segment.substring(segment.startsWith("[...") ? 4 : 1, segment.length - 1);
1576
+ const value = params ? params[paramsPrefix + key] || params[key] : "";
1577
+ path[i] = isSpread ? value : encodeURIComponent(value);
1578
+ }
1579
+ if (segment.startsWith("(") && segment.endsWith(")")) path.splice(i, 1);
1580
+ }
1581
+ let url = path.join("/");
1582
+ let baseURL = "/";
1583
+ if (baseURL) {
1584
+ baseURL = ensureSlash(baseURL);
1585
+ while (url.startsWith("/")) url = url.substring(1);
1586
+ url = baseURL + url;
1587
+ }
1588
+ return url;
1958
1589
  };
1959
- function omitProps(obj, keys) {
1960
- const omittedObj = {};
1961
- for (const key in obj) {
1962
- if (!key.startsWith("param:") && !keys.includes(key)) {
1963
- omittedObj[key] = obj[key];
1964
- }
1965
- }
1966
- return omittedObj;
1590
+ /**
1591
+ * Creates a new object from `obj` by omitting a set of `keys`.
1592
+ *
1593
+ * @beta
1594
+ * @experimental
1595
+ */ function omitProps(obj, keys) {
1596
+ const omittedObj = {};
1597
+ for (const key in obj) if (!key.startsWith("param:") && !keys.includes(key)) omittedObj[key] = obj[key];
1598
+ return omittedObj;
1967
1599
  }
1968
- export {
1969
- ErrorBoundary,
1970
- Form,
1971
- Link,
1972
- QWIK_CITY_SCROLLER,
1973
- QWIK_ROUTER_SCROLLER,
1974
- QwikCityMockProvider,
1975
- QwikCityProvider,
1976
- QwikRouterMockProvider,
1977
- QwikRouterProvider,
1978
- RouterOutlet,
1979
- ServiceWorkerRegister,
1980
- globalAction$,
1981
- globalActionQrl,
1982
- omitProps,
1983
- routeAction$,
1984
- routeActionQrl,
1985
- routeLoader$,
1986
- routeLoaderQrl,
1987
- server$,
1988
- serverQrl,
1989
- untypedAppUrl,
1990
- useContent,
1991
- useDocumentHead,
1992
- useLocation,
1993
- useNavigate,
1994
- usePreventNavigate$,
1995
- usePreventNavigateQrl,
1996
- valibot$,
1997
- valibotQrl,
1998
- validator$,
1999
- validatorQrl,
2000
- z2 as z,
2001
- zod$,
2002
- zodQrl
1600
+ /**
1601
+ * Creates the `render()` function that is required by `createQwikRouter()`. It requires a function
1602
+ * that returns the `jsx` and `options` for the renderer.
1603
+ *
1604
+ * @example
1605
+ *
1606
+ * ```tsx
1607
+ * const renderer = createRenderer((opts) => {
1608
+ * if (opts.requestHeaders['x-hello'] === 'world') {
1609
+ * return { jsx: <Hello />, options: opts };
1610
+ * }
1611
+ * return { jsx: <Root />, options: {
1612
+ * ...opts,
1613
+ * serverData: {
1614
+ * ...opts.serverData,
1615
+ * documentHead: {
1616
+ * meta: [
1617
+ * { name: 'renderedAt', content: new Date().toISOString() },
1618
+ * ],
1619
+ * },
1620
+ * },
1621
+ * } };
1622
+ * });
1623
+ * ```
1624
+ *
1625
+ * @public
1626
+ */ var createRenderer = (getOptions) => {
1627
+ return (opts) => {
1628
+ const { jsx, options } = getOptions(opts);
1629
+ const renderOptions = opts.serverData.renderMode === "static" ? {
1630
+ ...options,
1631
+ streaming: {
1632
+ ...options.streaming,
1633
+ outOfOrder: false
1634
+ }
1635
+ } : options;
1636
+ return renderToStream(jsx, renderOptions);
1637
+ };
2003
1638
  };
1639
+ /**
1640
+ * This renders all the tags collected from `head`.
1641
+ *
1642
+ * You can partially override the head, for example if you want to change the title:
1643
+ *
1644
+ * ```tsx
1645
+ * import { DocumentHeadTags, useDocumentHead } from '@qwik.dev/router';
1646
+ *
1647
+ * export default component$(() => {
1648
+ * const head = useDocumentHead();
1649
+ * return <DocumentHeadTags title={`${head.title} - My App`} />;
1650
+ * });
1651
+ * ```
1652
+ *
1653
+ * You don't have to use this component, you can also do it yourself for full control. Just copy the
1654
+ * code from this component and modify it to your needs.
1655
+ *
1656
+ * Note that this component normally only runs once, during SSR. You can use Signals in your
1657
+ * `src/root.tsx` to make runtime changes to `<head>` if needed.
1658
+ *
1659
+ * @public
1660
+ */ var DocumentHeadTags = /*#__PURE__*/ componentQrl(/*#__PURE__*/ inlinedQrl((props) => {
1661
+ let head = useDocumentHead();
1662
+ if (props) head = {
1663
+ ...head,
1664
+ ...props
1665
+ };
1666
+ return /*#__PURE__*/ _jsxSorted(Fragment, null, null, [
1667
+ head.title && /*#__PURE__*/ _jsxSorted("title", null, null, head.title, 1, "r5_0"),
1668
+ head.meta.map((m) => /*#__PURE__*/ _jsxSplit("meta", { ..._getVarProps(m) }, _getConstProps(m), null, 0, "r5_1")),
1669
+ head.links.map((l) => /*#__PURE__*/ _jsxSplit("link", { ..._getVarProps(l) }, _getConstProps(l), null, 0, "r5_2")),
1670
+ head.styles.map((s) => {
1671
+ const props = s.props || s;
1672
+ return /*#__PURE__*/ createElement("style", {
1673
+ ...props,
1674
+ dangerouslySetInnerHTML: s.style || props.dangerouslySetInnerHTML,
1675
+ key: s.key
1676
+ });
1677
+ }),
1678
+ head.scripts.map((s) => {
1679
+ const props = s.props || s;
1680
+ return /*#__PURE__*/ createElement("script", {
1681
+ ...props,
1682
+ dangerouslySetInnerHTML: s.script || props.dangerouslySetInnerHTML,
1683
+ key: s.key
1684
+ });
1685
+ })
1686
+ ], 1, "r5_3");
1687
+ }, "DocumentHeadTags_component_9CrWYOoCpgY"));
1688
+ export { DocumentHeadTags, ErrorBoundary, Form, Link, QWIK_CITY_SCROLLER, QWIK_ROUTER_SCROLLER, QwikCityMockProvider, QwikCityProvider, QwikRouterMockProvider, QwikRouterProvider, RouterOutlet, ServiceWorkerRegister, createRenderer, getRequestEvent, globalAction$, globalActionQrl, omitProps, routeAction$, routeActionQrl, routeLoader$, routeLoaderQrl, server$, serverQrl, untypedAppUrl, useContent, useDocumentHead, useHttpStatus, useLocation, useNavigate, usePreventNavigate$, usePreventNavigateQrl, useQwikRouter, valibot$, valibotQrl, validator$, validatorQrl, z, zod$, zodQrl };