@lolyjs/core 0.2.0-alpha.2 → 0.2.0-alpha.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENCE.md +9 -0
- package/dist/{bootstrap-BiCQmSkx.d.mts → bootstrap-DgvWWDim.d.mts} +7 -0
- package/dist/{bootstrap-BiCQmSkx.d.ts → bootstrap-DgvWWDim.d.ts} +7 -0
- package/dist/cli.cjs +34 -2
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +34 -2
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +110 -10
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +106 -6
- package/dist/index.js.map +1 -1
- package/dist/react/hooks.cjs +210 -2
- package/dist/react/hooks.cjs.map +1 -1
- package/dist/react/hooks.d.mts +84 -1
- package/dist/react/hooks.d.ts +84 -1
- package/dist/react/hooks.js +208 -1
- package/dist/react/hooks.js.map +1 -1
- package/dist/runtime.cjs +81 -9
- package/dist/runtime.cjs.map +1 -1
- package/dist/runtime.d.mts +2 -2
- package/dist/runtime.d.ts +2 -2
- package/dist/runtime.js +77 -5
- package/dist/runtime.js.map +1 -1
- package/package.json +4 -2
package/dist/react/hooks.cjs
CHANGED
|
@@ -21,7 +21,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var hooks_exports = {};
|
|
22
22
|
__export(hooks_exports, {
|
|
23
23
|
useBroadcastChannel: () => useBroadcastChannel,
|
|
24
|
-
usePageProps: () => usePageProps
|
|
24
|
+
usePageProps: () => usePageProps,
|
|
25
|
+
useRouter: () => useRouter
|
|
25
26
|
});
|
|
26
27
|
module.exports = __toCommonJS(hooks_exports);
|
|
27
28
|
|
|
@@ -78,9 +79,216 @@ function usePageProps() {
|
|
|
78
79
|
}, []);
|
|
79
80
|
return { params: state.params, props: state.props };
|
|
80
81
|
}
|
|
82
|
+
|
|
83
|
+
// modules/react/hooks/useRouter/index.ts
|
|
84
|
+
var import_react4 = require("react");
|
|
85
|
+
|
|
86
|
+
// modules/runtime/client/RouterContext.tsx
|
|
87
|
+
var import_react3 = require("react");
|
|
88
|
+
var RouterContext = (0, import_react3.createContext)(null);
|
|
89
|
+
|
|
90
|
+
// modules/runtime/client/constants.ts
|
|
91
|
+
var WINDOW_DATA_KEY = "__FW_DATA__";
|
|
92
|
+
var ROUTER_DATA_KEY = "__LOLY_ROUTER_DATA__";
|
|
93
|
+
var ROUTER_NAVIGATE_KEY = "__LOLY_ROUTER_NAVIGATE__";
|
|
94
|
+
|
|
95
|
+
// modules/runtime/client/window-data.ts
|
|
96
|
+
function getWindowData() {
|
|
97
|
+
if (typeof window === "undefined") {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
return window[WINDOW_DATA_KEY] ?? null;
|
|
101
|
+
}
|
|
102
|
+
function getRouterData() {
|
|
103
|
+
if (typeof window === "undefined") {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
return window[ROUTER_DATA_KEY] ?? null;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// modules/react/hooks/useRouter/index.ts
|
|
110
|
+
function useRouter() {
|
|
111
|
+
const context = (0, import_react4.useContext)(RouterContext);
|
|
112
|
+
const navigate = context?.navigate;
|
|
113
|
+
const navigateRef = (0, import_react4.useRef)(navigate);
|
|
114
|
+
(0, import_react4.useEffect)(() => {
|
|
115
|
+
navigateRef.current = navigate;
|
|
116
|
+
}, [navigate]);
|
|
117
|
+
const [routeData, setRouteData] = (0, import_react4.useState)(() => {
|
|
118
|
+
if (typeof window === "undefined") {
|
|
119
|
+
return {
|
|
120
|
+
pathname: "",
|
|
121
|
+
query: {},
|
|
122
|
+
searchParams: {},
|
|
123
|
+
params: {}
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
const data = getWindowData();
|
|
127
|
+
const routerData = getRouterData();
|
|
128
|
+
const searchParams = routerData?.searchParams || parseQueryString(window.location.search);
|
|
129
|
+
return {
|
|
130
|
+
pathname: routerData?.pathname || data?.pathname || window.location.pathname,
|
|
131
|
+
query: searchParams,
|
|
132
|
+
// For backward compatibility
|
|
133
|
+
searchParams,
|
|
134
|
+
params: routerData?.params || data?.params || {}
|
|
135
|
+
};
|
|
136
|
+
});
|
|
137
|
+
(0, import_react4.useEffect)(() => {
|
|
138
|
+
if (typeof window === "undefined") return;
|
|
139
|
+
const handleDataRefresh = () => {
|
|
140
|
+
const data = getWindowData();
|
|
141
|
+
const routerData = getRouterData();
|
|
142
|
+
const currentPathname = window.location.pathname;
|
|
143
|
+
const currentSearch = window.location.search;
|
|
144
|
+
const searchParams = routerData?.searchParams || parseQueryString(currentSearch);
|
|
145
|
+
setRouteData({
|
|
146
|
+
pathname: routerData?.pathname || data?.pathname || currentPathname,
|
|
147
|
+
query: searchParams,
|
|
148
|
+
// For backward compatibility
|
|
149
|
+
searchParams,
|
|
150
|
+
params: routerData?.params || data?.params || {}
|
|
151
|
+
});
|
|
152
|
+
};
|
|
153
|
+
window.addEventListener("fw-data-refresh", handleDataRefresh);
|
|
154
|
+
window.addEventListener("fw-router-data-refresh", handleDataRefresh);
|
|
155
|
+
const handlePopState = () => {
|
|
156
|
+
handleDataRefresh();
|
|
157
|
+
};
|
|
158
|
+
window.addEventListener("popstate", handlePopState);
|
|
159
|
+
return () => {
|
|
160
|
+
window.removeEventListener("fw-data-refresh", handleDataRefresh);
|
|
161
|
+
window.removeEventListener("fw-router-data-refresh", handleDataRefresh);
|
|
162
|
+
window.removeEventListener("popstate", handlePopState);
|
|
163
|
+
};
|
|
164
|
+
}, []);
|
|
165
|
+
const push = (0, import_react4.useCallback)(
|
|
166
|
+
async (url, options) => {
|
|
167
|
+
const fullUrl = url.startsWith("/") ? url : `/${url}`;
|
|
168
|
+
const getCurrentNavigate = () => {
|
|
169
|
+
if (navigateRef.current) return navigateRef.current;
|
|
170
|
+
if (navigate) return navigate;
|
|
171
|
+
if (typeof window !== "undefined" && window[ROUTER_NAVIGATE_KEY]) {
|
|
172
|
+
return window[ROUTER_NAVIGATE_KEY];
|
|
173
|
+
}
|
|
174
|
+
return null;
|
|
175
|
+
};
|
|
176
|
+
let currentNavigate = getCurrentNavigate();
|
|
177
|
+
if (typeof window === "undefined") {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
if (!currentNavigate) {
|
|
181
|
+
for (let i = 0; i < 10; i++) {
|
|
182
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
183
|
+
currentNavigate = getCurrentNavigate();
|
|
184
|
+
if (currentNavigate) break;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (!currentNavigate) {
|
|
188
|
+
window.location.href = fullUrl;
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
const currentUrl = window.location.pathname + window.location.search;
|
|
192
|
+
if (fullUrl === currentUrl) {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
window.history.pushState({}, "", fullUrl);
|
|
196
|
+
await currentNavigate(fullUrl, options);
|
|
197
|
+
},
|
|
198
|
+
[navigate]
|
|
199
|
+
// Include navigate in dependencies so it updates when context becomes available
|
|
200
|
+
);
|
|
201
|
+
const replace = (0, import_react4.useCallback)(
|
|
202
|
+
async (url, options) => {
|
|
203
|
+
const fullUrl = url.startsWith("/") ? url : `/${url}`;
|
|
204
|
+
const getCurrentNavigate = () => {
|
|
205
|
+
if (navigateRef.current) return navigateRef.current;
|
|
206
|
+
if (navigate) return navigate;
|
|
207
|
+
if (typeof window !== "undefined" && window[ROUTER_NAVIGATE_KEY]) {
|
|
208
|
+
return window[ROUTER_NAVIGATE_KEY];
|
|
209
|
+
}
|
|
210
|
+
return null;
|
|
211
|
+
};
|
|
212
|
+
let currentNavigate = getCurrentNavigate();
|
|
213
|
+
if (typeof window === "undefined") {
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
if (!currentNavigate) {
|
|
217
|
+
for (let i = 0; i < 10; i++) {
|
|
218
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
219
|
+
currentNavigate = getCurrentNavigate();
|
|
220
|
+
if (currentNavigate) break;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
if (!currentNavigate) {
|
|
224
|
+
window.location.replace(fullUrl);
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
window.history.replaceState({}, "", fullUrl);
|
|
228
|
+
await currentNavigate(fullUrl, options);
|
|
229
|
+
},
|
|
230
|
+
[navigate]
|
|
231
|
+
);
|
|
232
|
+
const back = (0, import_react4.useCallback)(() => {
|
|
233
|
+
if (typeof window !== "undefined") {
|
|
234
|
+
window.history.back();
|
|
235
|
+
}
|
|
236
|
+
}, []);
|
|
237
|
+
const refresh = (0, import_react4.useCallback)(async () => {
|
|
238
|
+
const currentUrl = typeof window !== "undefined" ? window.location.pathname + window.location.search : routeData.pathname;
|
|
239
|
+
const getCurrentNavigate = () => {
|
|
240
|
+
if (navigateRef.current) return navigateRef.current;
|
|
241
|
+
if (navigate) return navigate;
|
|
242
|
+
if (typeof window !== "undefined" && window[ROUTER_NAVIGATE_KEY]) {
|
|
243
|
+
return window[ROUTER_NAVIGATE_KEY];
|
|
244
|
+
}
|
|
245
|
+
return null;
|
|
246
|
+
};
|
|
247
|
+
let currentNavigate = getCurrentNavigate();
|
|
248
|
+
if (typeof window === "undefined") {
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
if (!currentNavigate) {
|
|
252
|
+
for (let i = 0; i < 10; i++) {
|
|
253
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
254
|
+
currentNavigate = getCurrentNavigate();
|
|
255
|
+
if (currentNavigate) break;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
if (!currentNavigate) {
|
|
259
|
+
window.location.reload();
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
await currentNavigate(currentUrl, { revalidate: true });
|
|
263
|
+
}, [navigate, routeData.pathname]);
|
|
264
|
+
return {
|
|
265
|
+
push,
|
|
266
|
+
replace,
|
|
267
|
+
back,
|
|
268
|
+
refresh,
|
|
269
|
+
pathname: routeData.pathname,
|
|
270
|
+
query: routeData.query,
|
|
271
|
+
searchParams: routeData.searchParams,
|
|
272
|
+
params: routeData.params
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
function parseQueryString(search) {
|
|
276
|
+
const params = {};
|
|
277
|
+
if (!search || search.length === 0) return params;
|
|
278
|
+
const queryString = search.startsWith("?") ? search.slice(1) : search;
|
|
279
|
+
const pairs = queryString.split("&");
|
|
280
|
+
for (const pair of pairs) {
|
|
281
|
+
const [key, value] = pair.split("=");
|
|
282
|
+
if (key) {
|
|
283
|
+
params[decodeURIComponent(key)] = value ? decodeURIComponent(value) : "";
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
return params;
|
|
287
|
+
}
|
|
81
288
|
// Annotate the CommonJS export names for ESM import in node:
|
|
82
289
|
0 && (module.exports = {
|
|
83
290
|
useBroadcastChannel,
|
|
84
|
-
usePageProps
|
|
291
|
+
usePageProps,
|
|
292
|
+
useRouter
|
|
85
293
|
});
|
|
86
294
|
//# sourceMappingURL=hooks.cjs.map
|
package/dist/react/hooks.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../modules/react/hooks/index.ts","../../modules/react/hooks/useBroadcastChannel/index.tsx","../../modules/react/hooks/usePageProps/index.ts"],"sourcesContent":["export { useBroadcastChannel } from \"./useBroadcastChannel\";\r\nexport { usePageProps } from \"./usePageProps\";\r\n","import React, { useEffect, useState } from \"react\";\r\n\r\nexport const useBroadcastChannel = (channelName: string) => {\r\n const [message, setMessage] = useState(null);\r\n const channel = new BroadcastChannel(channelName);\r\n\r\n useEffect(() => {\r\n const handleMessage = (event: MessageEvent) => {\r\n setMessage(event.data);\r\n };\r\n\r\n channel.onmessage = handleMessage;\r\n\r\n // Clean up the channel when the component unmounts\r\n return () => {\r\n channel.close();\r\n };\r\n }, [channel]);\r\n\r\n const sendMessage = (msg: unknown) => {\r\n channel.postMessage(msg);\r\n };\r\n\r\n return { message, sendMessage };\r\n};\r\n","import React, { useEffect, useState } from \"react\";\r\n\r\n/**\r\n * Hook to access page props and route parameters.\r\n *\r\n * Reads initial data from window.__FW_DATA__ set during SSR.\r\n * Automatically updates when `revalidate()` is called.\r\n *\r\n * @template P - Type for page props (default: any)\r\n * @template T - Type for route params (default: any)\r\n * @returns Object containing params and props\r\n *\r\n * @example\r\n * // With props type only\r\n * const { props } = usePageProps<{ title: string }>();\r\n *\r\n * @example\r\n * // With both props and params types\r\n * const { props, params } = usePageProps<{ title: string }, { id: string }>();\r\n */\r\nexport function usePageProps<P = any, T = any>(): { params: T, props: P } {\r\n const [state, setState] = useState<{ params: T, props: P }>(() => {\r\n // Initialize with current data if available\r\n if (typeof window !== \"undefined\" && (window as any)?.__FW_DATA__) {\r\n const data = (window as any).__FW_DATA__;\r\n return {\r\n params: data.params || {} as T,\r\n props: data.props || {} as P,\r\n };\r\n }\r\n return {\r\n params: {} as T,\r\n props: {} as P,\r\n };\r\n });\r\n\r\n useEffect(() => {\r\n // Listen for data refresh events (from revalidate() or navigation)\r\n const handleDataRefresh = () => {\r\n if ((window as any)?.__FW_DATA__) {\r\n const data = (window as any).__FW_DATA__;\r\n setState({\r\n params: data.params || {} as T,\r\n props: data.props || {} as P,\r\n });\r\n }\r\n };\r\n\r\n window.addEventListener(\"fw-data-refresh\", handleDataRefresh);\r\n\r\n return () => {\r\n window.removeEventListener(\"fw-data-refresh\", handleDataRefresh);\r\n };\r\n }, []);\r\n\r\n return { params: state.params as T, props: state.props as P };\r\n};\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAA2C;AAEpC,IAAM,sBAAsB,CAAC,gBAAwB;AAC1D,QAAM,CAAC,SAAS,UAAU,QAAI,uBAAS,IAAI;AAC3C,QAAM,UAAU,IAAI,iBAAiB,WAAW;AAEhD,8BAAU,MAAM;AACd,UAAM,gBAAgB,CAAC,UAAwB;AAC7C,iBAAW,MAAM,IAAI;AAAA,IACvB;AAEA,YAAQ,YAAY;AAGpB,WAAO,MAAM;AACX,cAAQ,MAAM;AAAA,IAChB;AAAA,EACF,GAAG,CAAC,OAAO,CAAC;AAEZ,QAAM,cAAc,CAAC,QAAiB;AACpC,YAAQ,YAAY,GAAG;AAAA,EACzB;AAEA,SAAO,EAAE,SAAS,YAAY;AAChC;;;ACxBA,IAAAA,gBAA2C;AAoBpC,SAAS,eAA0D;AACxE,QAAM,CAAC,OAAO,QAAQ,QAAI,wBAAkC,MAAM;AAEhE,QAAI,OAAO,WAAW,eAAgB,QAAgB,aAAa;AACjE,YAAM,OAAQ,OAAe;AAC7B,aAAO;AAAA,QACL,QAAQ,KAAK,UAAU,CAAC;AAAA,QACxB,OAAO,KAAK,SAAS,CAAC;AAAA,MACxB;AAAA,IACF;AACA,WAAO;AAAA,MACL,QAAQ,CAAC;AAAA,MACT,OAAO,CAAC;AAAA,IACV;AAAA,EACF,CAAC;AAED,+BAAU,MAAM;AAEd,UAAM,oBAAoB,MAAM;AAC9B,UAAK,QAAgB,aAAa;AAChC,cAAM,OAAQ,OAAe;AAC7B,iBAAS;AAAA,UACP,QAAQ,KAAK,UAAU,CAAC;AAAA,UACxB,OAAO,KAAK,SAAS,CAAC;AAAA,QACxB,CAAC;AAAA,MACH;AAAA,IACF;AAEA,WAAO,iBAAiB,mBAAmB,iBAAiB;AAE5D,WAAO,MAAM;AACX,aAAO,oBAAoB,mBAAmB,iBAAiB;AAAA,IACjE;AAAA,EACF,GAAG,CAAC,CAAC;AAEL,SAAO,EAAE,QAAQ,MAAM,QAAa,OAAO,MAAM,MAAW;AAC9D;","names":["import_react"]}
|
|
1
|
+
{"version":3,"sources":["../../modules/react/hooks/index.ts","../../modules/react/hooks/useBroadcastChannel/index.tsx","../../modules/react/hooks/usePageProps/index.ts","../../modules/react/hooks/useRouter/index.ts","../../modules/runtime/client/RouterContext.tsx","../../modules/runtime/client/constants.ts","../../modules/runtime/client/window-data.ts"],"sourcesContent":["export { useBroadcastChannel } from \"./useBroadcastChannel\";\r\nexport { usePageProps } from \"./usePageProps\";\r\nexport { useRouter } from \"./useRouter\";\r\nexport type { Router } from \"./useRouter\";\r\n","import React, { useEffect, useState } from \"react\";\r\n\r\nexport const useBroadcastChannel = (channelName: string) => {\r\n const [message, setMessage] = useState(null);\r\n const channel = new BroadcastChannel(channelName);\r\n\r\n useEffect(() => {\r\n const handleMessage = (event: MessageEvent) => {\r\n setMessage(event.data);\r\n };\r\n\r\n channel.onmessage = handleMessage;\r\n\r\n // Clean up the channel when the component unmounts\r\n return () => {\r\n channel.close();\r\n };\r\n }, [channel]);\r\n\r\n const sendMessage = (msg: unknown) => {\r\n channel.postMessage(msg);\r\n };\r\n\r\n return { message, sendMessage };\r\n};\r\n","import React, { useEffect, useState } from \"react\";\r\n\r\n/**\r\n * Hook to access page props and route parameters.\r\n *\r\n * Reads initial data from window.__FW_DATA__ set during SSR.\r\n * Automatically updates when `revalidate()` is called.\r\n *\r\n * @template P - Type for page props (default: any)\r\n * @template T - Type for route params (default: any)\r\n * @returns Object containing params and props\r\n *\r\n * @example\r\n * // With props type only\r\n * const { props } = usePageProps<{ title: string }>();\r\n *\r\n * @example\r\n * // With both props and params types\r\n * const { props, params } = usePageProps<{ title: string }, { id: string }>();\r\n */\r\nexport function usePageProps<P = any, T = any>(): { params: T, props: P } {\r\n const [state, setState] = useState<{ params: T, props: P }>(() => {\r\n // Initialize with current data if available\r\n if (typeof window !== \"undefined\" && (window as any)?.__FW_DATA__) {\r\n const data = (window as any).__FW_DATA__;\r\n return {\r\n params: data.params || {} as T,\r\n props: data.props || {} as P,\r\n };\r\n }\r\n return {\r\n params: {} as T,\r\n props: {} as P,\r\n };\r\n });\r\n\r\n useEffect(() => {\r\n // Listen for data refresh events (from revalidate() or navigation)\r\n const handleDataRefresh = () => {\r\n if ((window as any)?.__FW_DATA__) {\r\n const data = (window as any).__FW_DATA__;\r\n setState({\r\n params: data.params || {} as T,\r\n props: data.props || {} as P,\r\n });\r\n }\r\n };\r\n\r\n window.addEventListener(\"fw-data-refresh\", handleDataRefresh);\r\n\r\n return () => {\r\n window.removeEventListener(\"fw-data-refresh\", handleDataRefresh);\r\n };\r\n }, []);\r\n\r\n return { params: state.params as T, props: state.props as P };\r\n};\r\n","import { useState, useEffect, useCallback, useContext, useRef } from \"react\";\r\nimport { RouterContext } from \"../../../runtime/client/RouterContext\";\r\nimport { getWindowData, getRouterData } from \"../../../runtime/client/window-data\";\r\nimport { ROUTER_NAVIGATE_KEY } from \"../../../runtime/client/constants\";\r\n\r\nexport interface Router {\r\n /**\r\n * Navigate to a new route.\r\n * @param url - The URL to navigate to (e.g., \"/about\" or \"/blog/[slug]\" with params)\r\n * @param options - Navigation options\r\n */\r\n push: (url: string, options?: { revalidate?: boolean }) => Promise<void>;\r\n \r\n /**\r\n * Replace the current route without adding to history.\r\n * @param url - The URL to navigate to\r\n * @param options - Navigation options\r\n */\r\n replace: (url: string, options?: { revalidate?: boolean }) => Promise<void>;\r\n \r\n /**\r\n * Go back in the browser history.\r\n */\r\n back: () => void;\r\n \r\n /**\r\n * Refresh the current route data by revalidating.\r\n */\r\n refresh: () => Promise<void>;\r\n \r\n /**\r\n * Current pathname (e.g., \"/blog/my-post\")\r\n */\r\n pathname: string;\r\n \r\n /**\r\n * Query parameters from the URL (e.g., ?id=123&name=test)\r\n * Alias for searchParams for backward compatibility\r\n */\r\n query: Record<string, string>;\r\n \r\n /**\r\n * Search parameters from the URL (e.g., ?id=123&name=test)\r\n */\r\n searchParams: Record<string, unknown>;\r\n \r\n /**\r\n * Dynamic route parameters (e.g., { slug: \"my-post\" } for /blog/[slug])\r\n */\r\n params: Record<string, string>;\r\n}\r\n\r\n/**\r\n * Hook to access router functionality and current route information.\r\n * \r\n * Provides methods to navigate programmatically and access current route data.\r\n * \r\n * @returns Router object with navigation methods and route information\r\n * \r\n * @example\r\n * ```tsx\r\n * function MyComponent() {\r\n * const router = useRouter();\r\n * \r\n * const handleClick = () => {\r\n * router.push(\"/about\");\r\n * };\r\n * \r\n * return (\r\n * <div>\r\n * <p>Current path: {router.pathname}</p>\r\n * <p>Params: {JSON.stringify(router.params)}</p>\r\n * <button onClick={handleClick}>Go to About</button>\r\n * </div>\r\n * );\r\n * }\r\n * ```\r\n * \r\n * @example\r\n * ```tsx\r\n * // Navigate with dynamic params\r\n * router.push(\"/blog/my-post\");\r\n * \r\n * // Replace current route\r\n * router.replace(\"/login\");\r\n * \r\n * // Refresh current route data\r\n * await router.refresh();\r\n * ```\r\n */\r\nexport function useRouter(): Router {\r\n // Try to get context, but don't throw if it's not available (SSR)\r\n const context = useContext(RouterContext);\r\n const navigate = context?.navigate;\r\n \r\n // Use a ref to store navigate so we can access it in callbacks even if context updates\r\n // Initialize with current navigate value\r\n const navigateRef = useRef(navigate);\r\n \r\n // Update ref when navigate changes (this ensures we always have the latest value)\r\n useEffect(() => {\r\n navigateRef.current = navigate;\r\n }, [navigate]);\r\n \r\n const [routeData, setRouteData] = useState(() => {\r\n // During SSR, return empty/default values\r\n if (typeof window === \"undefined\") {\r\n return {\r\n pathname: \"\",\r\n query: {},\r\n searchParams: {},\r\n params: {},\r\n };\r\n }\r\n \r\n // On client, get data from window\r\n const data = getWindowData();\r\n const routerData = getRouterData();\r\n \r\n // Parse search params from URL if routerData is not available\r\n const searchParams = routerData?.searchParams || parseQueryString(window.location.search);\r\n \r\n return {\r\n pathname: routerData?.pathname || data?.pathname || window.location.pathname,\r\n query: searchParams as Record<string, string>, // For backward compatibility\r\n searchParams: searchParams,\r\n params: routerData?.params || data?.params || {},\r\n };\r\n });\r\n\r\n // Listen for route changes (only on client)\r\n useEffect(() => {\r\n if (typeof window === \"undefined\") return;\r\n \r\n const handleDataRefresh = () => {\r\n const data = getWindowData();\r\n const routerData = getRouterData();\r\n const currentPathname = window.location.pathname;\r\n const currentSearch = window.location.search;\r\n \r\n const searchParams = routerData?.searchParams || parseQueryString(currentSearch);\r\n \r\n setRouteData({\r\n pathname: routerData?.pathname || data?.pathname || currentPathname,\r\n query: searchParams as Record<string, string>, // For backward compatibility\r\n searchParams: searchParams,\r\n params: routerData?.params || data?.params || {},\r\n });\r\n };\r\n\r\n // Listen for navigation events\r\n window.addEventListener(\"fw-data-refresh\", handleDataRefresh);\r\n window.addEventListener(\"fw-router-data-refresh\", handleDataRefresh);\r\n \r\n // Also listen for popstate (browser back/forward)\r\n const handlePopState = () => {\r\n handleDataRefresh();\r\n };\r\n window.addEventListener(\"popstate\", handlePopState);\r\n\r\n return () => {\r\n window.removeEventListener(\"fw-data-refresh\", handleDataRefresh);\r\n window.removeEventListener(\"fw-router-data-refresh\", handleDataRefresh);\r\n window.removeEventListener(\"popstate\", handlePopState);\r\n };\r\n }, []);\r\n\r\n const push = useCallback(\r\n async (url: string, options?: { revalidate?: boolean }) => {\r\n const fullUrl = url.startsWith(\"/\") ? url : `/${url}`;\r\n \r\n /**\r\n * SOLUTION: Multi-source navigate function resolution\r\n * \r\n * During React hydration, RouterContext may not be available immediately.\r\n * We try three sources in order:\r\n * 1. navigateRef.current - Most up-to-date, updated via useEffect\r\n * 2. navigate from context - Direct context access\r\n * 3. window.__LOLY_ROUTER_NAVIGATE__ - Global fallback exposed by AppShell\r\n * \r\n * This ensures SPA navigation works even during hydration timing issues.\r\n */\r\n const getCurrentNavigate = () => {\r\n if (navigateRef.current) return navigateRef.current;\r\n if (navigate) return navigate;\r\n if (typeof window !== \"undefined\" && (window as any)[ROUTER_NAVIGATE_KEY]) {\r\n return (window as any)[ROUTER_NAVIGATE_KEY];\r\n }\r\n return null;\r\n };\r\n \r\n let currentNavigate = getCurrentNavigate();\r\n \r\n if (typeof window === \"undefined\") {\r\n return; // SSR\r\n }\r\n \r\n // Wait for context during hydration (up to 100ms)\r\n if (!currentNavigate) {\r\n for (let i = 0; i < 10; i++) {\r\n await new Promise(resolve => setTimeout(resolve, 10));\r\n currentNavigate = getCurrentNavigate();\r\n if (currentNavigate) break;\r\n }\r\n }\r\n \r\n // Final fallback: full page reload if navigate is still unavailable\r\n if (!currentNavigate) {\r\n window.location.href = fullUrl;\r\n return;\r\n }\r\n \r\n // Check if we're already on this URL (same as link handler)\r\n const currentUrl = window.location.pathname + window.location.search;\r\n if (fullUrl === currentUrl) {\r\n return; // Already on this route, no need to navigate\r\n }\r\n \r\n // Update URL in browser history (same as link handler does)\r\n // This is done BEFORE navigation to match link behavior\r\n window.history.pushState({}, \"\", fullUrl);\r\n \r\n // Navigate using SPA navigation (same as link handler)\r\n // If navigation fails, navigate() will handle the reload internally\r\n await currentNavigate(fullUrl, options);\r\n },\r\n [navigate] // Include navigate in dependencies so it updates when context becomes available\r\n );\r\n\r\n const replace = useCallback(\r\n async (url: string, options?: { revalidate?: boolean }) => {\r\n const fullUrl = url.startsWith(\"/\") ? url : `/${url}`;\r\n \r\n const getCurrentNavigate = () => {\r\n if (navigateRef.current) return navigateRef.current;\r\n if (navigate) return navigate;\r\n if (typeof window !== \"undefined\" && (window as any)[ROUTER_NAVIGATE_KEY]) {\r\n return (window as any)[ROUTER_NAVIGATE_KEY];\r\n }\r\n return null;\r\n };\r\n \r\n let currentNavigate = getCurrentNavigate();\r\n \r\n if (typeof window === \"undefined\") {\r\n return;\r\n }\r\n \r\n if (!currentNavigate) {\r\n for (let i = 0; i < 10; i++) {\r\n await new Promise(resolve => setTimeout(resolve, 10));\r\n currentNavigate = getCurrentNavigate();\r\n if (currentNavigate) break;\r\n }\r\n }\r\n \r\n if (!currentNavigate) {\r\n window.location.replace(fullUrl);\r\n return;\r\n }\r\n \r\n // Update URL in browser history using replace (doesn't add to history)\r\n window.history.replaceState({}, \"\", fullUrl);\r\n \r\n // Navigate using SPA navigation\r\n await currentNavigate(fullUrl, options);\r\n },\r\n [navigate]\r\n );\r\n\r\n const back = useCallback(() => {\r\n if (typeof window !== \"undefined\") {\r\n window.history.back();\r\n }\r\n }, []);\r\n\r\n const refresh = useCallback(async () => {\r\n const currentUrl = typeof window !== \"undefined\" \r\n ? window.location.pathname + window.location.search \r\n : routeData.pathname;\r\n \r\n const getCurrentNavigate = () => {\r\n if (navigateRef.current) return navigateRef.current;\r\n if (navigate) return navigate;\r\n if (typeof window !== \"undefined\" && (window as any)[ROUTER_NAVIGATE_KEY]) {\r\n return (window as any)[ROUTER_NAVIGATE_KEY];\r\n }\r\n return null;\r\n };\r\n \r\n let currentNavigate = getCurrentNavigate();\r\n \r\n if (typeof window === \"undefined\") {\r\n return;\r\n }\r\n \r\n if (!currentNavigate) {\r\n for (let i = 0; i < 10; i++) {\r\n await new Promise(resolve => setTimeout(resolve, 10));\r\n currentNavigate = getCurrentNavigate();\r\n if (currentNavigate) break;\r\n }\r\n }\r\n \r\n if (!currentNavigate) {\r\n window.location.reload();\r\n return;\r\n }\r\n \r\n await currentNavigate(currentUrl, { revalidate: true });\r\n }, [navigate, routeData.pathname]);\r\n\r\n return {\r\n push,\r\n replace,\r\n back,\r\n refresh,\r\n pathname: routeData.pathname,\r\n query: routeData.query,\r\n searchParams: routeData.searchParams,\r\n params: routeData.params,\r\n };\r\n}\r\n\r\n/**\r\n * Parse query string into an object.\r\n * @param search - Query string (e.g., \"?id=123&name=test\")\r\n * @returns Object with query parameters\r\n */\r\nfunction parseQueryString(search: string): Record<string, string> {\r\n const params: Record<string, string> = {};\r\n if (!search || search.length === 0) return params;\r\n \r\n const queryString = search.startsWith(\"?\") ? search.slice(1) : search;\r\n const pairs = queryString.split(\"&\");\r\n \r\n for (const pair of pairs) {\r\n const [key, value] = pair.split(\"=\");\r\n if (key) {\r\n params[decodeURIComponent(key)] = value ? decodeURIComponent(value) : \"\";\r\n }\r\n }\r\n \r\n return params;\r\n}\r\n","import { createContext, useContext } from \"react\";\r\n\r\nexport type NavigateFunction = (\r\n url: string,\r\n options?: { revalidate?: boolean; replace?: boolean }\r\n) => Promise<void>;\r\n\r\nexport interface RouterContextValue {\r\n navigate: NavigateFunction;\r\n}\r\n\r\nexport const RouterContext = createContext<RouterContextValue | null>(null);\r\n\r\nexport function useRouterContext(): RouterContextValue {\r\n const context = useContext(RouterContext);\r\n if (!context) {\r\n throw new Error(\r\n \"useRouter must be used within a RouterProvider. Make sure you're using it inside a Loly app.\"\r\n );\r\n }\r\n return context;\r\n}\r\n","// Client-side constants (hardcoded to avoid alias resolution issues in Rspack)\r\nexport const WINDOW_DATA_KEY = \"__FW_DATA__\";\r\nexport const ROUTER_DATA_KEY = \"__LOLY_ROUTER_DATA__\";\r\nexport const APP_CONTAINER_ID = \"__app\";\r\n// Global key for navigate function fallback (exposed by AppShell for hydration timing issues)\r\nexport const ROUTER_NAVIGATE_KEY = \"__LOLY_ROUTER_NAVIGATE__\";\r\n\r\n","import { WINDOW_DATA_KEY, ROUTER_DATA_KEY } from \"./constants\";\r\nimport type { InitialData, RouterData } from \"./types\";\r\n\r\nexport function getWindowData(): InitialData | null {\r\n if (typeof window === \"undefined\") {\r\n return null;\r\n }\r\n return ((window as any)[WINDOW_DATA_KEY] as InitialData | undefined) ?? null;\r\n}\r\n\r\nexport function getRouterData(): RouterData | null {\r\n if (typeof window === \"undefined\") {\r\n return null;\r\n }\r\n return ((window as any)[ROUTER_DATA_KEY] as RouterData | undefined) ?? null;\r\n}\r\n\r\nexport function setWindowData(data: InitialData): void {\r\n (window as any)[WINDOW_DATA_KEY] = data;\r\n \r\n // Dispatch event for components to listen to (e.g., usePageProps, ThemeProvider)\r\n // This ensures components update when navigating in SPA mode\r\n if (typeof window !== \"undefined\") {\r\n window.dispatchEvent(\r\n new CustomEvent(\"fw-data-refresh\", {\r\n detail: { data },\r\n })\r\n );\r\n }\r\n}\r\n\r\nexport function setRouterData(data: RouterData): void {\r\n (window as any)[ROUTER_DATA_KEY] = data;\r\n \r\n // Dispatch event for router data updates\r\n if (typeof window !== \"undefined\") {\r\n window.dispatchEvent(\r\n new CustomEvent(\"fw-router-data-refresh\", {\r\n detail: { data },\r\n })\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KAAK,MAAM,GAAG;AACnC,QAAI,KAAK;AACP,aAAO,mBAAmB,GAAG,CAAC,IAAI,QAAQ,mBAAmB,KAAK,IAAI;AAAA,IACxE;AAAA,EACF;AAEA,SAAO;AACT;","names":["import_react","import_react","import_react"]}
|
package/dist/react/hooks.d.mts
CHANGED
|
@@ -26,4 +26,87 @@ declare function usePageProps<P = any, T = any>(): {
|
|
|
26
26
|
props: P;
|
|
27
27
|
};
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
interface Router {
|
|
30
|
+
/**
|
|
31
|
+
* Navigate to a new route.
|
|
32
|
+
* @param url - The URL to navigate to (e.g., "/about" or "/blog/[slug]" with params)
|
|
33
|
+
* @param options - Navigation options
|
|
34
|
+
*/
|
|
35
|
+
push: (url: string, options?: {
|
|
36
|
+
revalidate?: boolean;
|
|
37
|
+
}) => Promise<void>;
|
|
38
|
+
/**
|
|
39
|
+
* Replace the current route without adding to history.
|
|
40
|
+
* @param url - The URL to navigate to
|
|
41
|
+
* @param options - Navigation options
|
|
42
|
+
*/
|
|
43
|
+
replace: (url: string, options?: {
|
|
44
|
+
revalidate?: boolean;
|
|
45
|
+
}) => Promise<void>;
|
|
46
|
+
/**
|
|
47
|
+
* Go back in the browser history.
|
|
48
|
+
*/
|
|
49
|
+
back: () => void;
|
|
50
|
+
/**
|
|
51
|
+
* Refresh the current route data by revalidating.
|
|
52
|
+
*/
|
|
53
|
+
refresh: () => Promise<void>;
|
|
54
|
+
/**
|
|
55
|
+
* Current pathname (e.g., "/blog/my-post")
|
|
56
|
+
*/
|
|
57
|
+
pathname: string;
|
|
58
|
+
/**
|
|
59
|
+
* Query parameters from the URL (e.g., ?id=123&name=test)
|
|
60
|
+
* Alias for searchParams for backward compatibility
|
|
61
|
+
*/
|
|
62
|
+
query: Record<string, string>;
|
|
63
|
+
/**
|
|
64
|
+
* Search parameters from the URL (e.g., ?id=123&name=test)
|
|
65
|
+
*/
|
|
66
|
+
searchParams: Record<string, unknown>;
|
|
67
|
+
/**
|
|
68
|
+
* Dynamic route parameters (e.g., { slug: "my-post" } for /blog/[slug])
|
|
69
|
+
*/
|
|
70
|
+
params: Record<string, string>;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Hook to access router functionality and current route information.
|
|
74
|
+
*
|
|
75
|
+
* Provides methods to navigate programmatically and access current route data.
|
|
76
|
+
*
|
|
77
|
+
* @returns Router object with navigation methods and route information
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```tsx
|
|
81
|
+
* function MyComponent() {
|
|
82
|
+
* const router = useRouter();
|
|
83
|
+
*
|
|
84
|
+
* const handleClick = () => {
|
|
85
|
+
* router.push("/about");
|
|
86
|
+
* };
|
|
87
|
+
*
|
|
88
|
+
* return (
|
|
89
|
+
* <div>
|
|
90
|
+
* <p>Current path: {router.pathname}</p>
|
|
91
|
+
* <p>Params: {JSON.stringify(router.params)}</p>
|
|
92
|
+
* <button onClick={handleClick}>Go to About</button>
|
|
93
|
+
* </div>
|
|
94
|
+
* );
|
|
95
|
+
* }
|
|
96
|
+
* ```
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```tsx
|
|
100
|
+
* // Navigate with dynamic params
|
|
101
|
+
* router.push("/blog/my-post");
|
|
102
|
+
*
|
|
103
|
+
* // Replace current route
|
|
104
|
+
* router.replace("/login");
|
|
105
|
+
*
|
|
106
|
+
* // Refresh current route data
|
|
107
|
+
* await router.refresh();
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
declare function useRouter(): Router;
|
|
111
|
+
|
|
112
|
+
export { type Router, useBroadcastChannel, usePageProps, useRouter };
|
package/dist/react/hooks.d.ts
CHANGED
|
@@ -26,4 +26,87 @@ declare function usePageProps<P = any, T = any>(): {
|
|
|
26
26
|
props: P;
|
|
27
27
|
};
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
interface Router {
|
|
30
|
+
/**
|
|
31
|
+
* Navigate to a new route.
|
|
32
|
+
* @param url - The URL to navigate to (e.g., "/about" or "/blog/[slug]" with params)
|
|
33
|
+
* @param options - Navigation options
|
|
34
|
+
*/
|
|
35
|
+
push: (url: string, options?: {
|
|
36
|
+
revalidate?: boolean;
|
|
37
|
+
}) => Promise<void>;
|
|
38
|
+
/**
|
|
39
|
+
* Replace the current route without adding to history.
|
|
40
|
+
* @param url - The URL to navigate to
|
|
41
|
+
* @param options - Navigation options
|
|
42
|
+
*/
|
|
43
|
+
replace: (url: string, options?: {
|
|
44
|
+
revalidate?: boolean;
|
|
45
|
+
}) => Promise<void>;
|
|
46
|
+
/**
|
|
47
|
+
* Go back in the browser history.
|
|
48
|
+
*/
|
|
49
|
+
back: () => void;
|
|
50
|
+
/**
|
|
51
|
+
* Refresh the current route data by revalidating.
|
|
52
|
+
*/
|
|
53
|
+
refresh: () => Promise<void>;
|
|
54
|
+
/**
|
|
55
|
+
* Current pathname (e.g., "/blog/my-post")
|
|
56
|
+
*/
|
|
57
|
+
pathname: string;
|
|
58
|
+
/**
|
|
59
|
+
* Query parameters from the URL (e.g., ?id=123&name=test)
|
|
60
|
+
* Alias for searchParams for backward compatibility
|
|
61
|
+
*/
|
|
62
|
+
query: Record<string, string>;
|
|
63
|
+
/**
|
|
64
|
+
* Search parameters from the URL (e.g., ?id=123&name=test)
|
|
65
|
+
*/
|
|
66
|
+
searchParams: Record<string, unknown>;
|
|
67
|
+
/**
|
|
68
|
+
* Dynamic route parameters (e.g., { slug: "my-post" } for /blog/[slug])
|
|
69
|
+
*/
|
|
70
|
+
params: Record<string, string>;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Hook to access router functionality and current route information.
|
|
74
|
+
*
|
|
75
|
+
* Provides methods to navigate programmatically and access current route data.
|
|
76
|
+
*
|
|
77
|
+
* @returns Router object with navigation methods and route information
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```tsx
|
|
81
|
+
* function MyComponent() {
|
|
82
|
+
* const router = useRouter();
|
|
83
|
+
*
|
|
84
|
+
* const handleClick = () => {
|
|
85
|
+
* router.push("/about");
|
|
86
|
+
* };
|
|
87
|
+
*
|
|
88
|
+
* return (
|
|
89
|
+
* <div>
|
|
90
|
+
* <p>Current path: {router.pathname}</p>
|
|
91
|
+
* <p>Params: {JSON.stringify(router.params)}</p>
|
|
92
|
+
* <button onClick={handleClick}>Go to About</button>
|
|
93
|
+
* </div>
|
|
94
|
+
* );
|
|
95
|
+
* }
|
|
96
|
+
* ```
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```tsx
|
|
100
|
+
* // Navigate with dynamic params
|
|
101
|
+
* router.push("/blog/my-post");
|
|
102
|
+
*
|
|
103
|
+
* // Replace current route
|
|
104
|
+
* router.replace("/login");
|
|
105
|
+
*
|
|
106
|
+
* // Refresh current route data
|
|
107
|
+
* await router.refresh();
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
declare function useRouter(): Router;
|
|
111
|
+
|
|
112
|
+
export { type Router, useBroadcastChannel, usePageProps, useRouter };
|
package/dist/react/hooks.js
CHANGED
|
@@ -51,8 +51,215 @@ function usePageProps() {
|
|
|
51
51
|
}, []);
|
|
52
52
|
return { params: state.params, props: state.props };
|
|
53
53
|
}
|
|
54
|
+
|
|
55
|
+
// modules/react/hooks/useRouter/index.ts
|
|
56
|
+
import { useState as useState3, useEffect as useEffect3, useCallback, useContext as useContext2, useRef } from "react";
|
|
57
|
+
|
|
58
|
+
// modules/runtime/client/RouterContext.tsx
|
|
59
|
+
import { createContext, useContext } from "react";
|
|
60
|
+
var RouterContext = createContext(null);
|
|
61
|
+
|
|
62
|
+
// modules/runtime/client/constants.ts
|
|
63
|
+
var WINDOW_DATA_KEY = "__FW_DATA__";
|
|
64
|
+
var ROUTER_DATA_KEY = "__LOLY_ROUTER_DATA__";
|
|
65
|
+
var ROUTER_NAVIGATE_KEY = "__LOLY_ROUTER_NAVIGATE__";
|
|
66
|
+
|
|
67
|
+
// modules/runtime/client/window-data.ts
|
|
68
|
+
function getWindowData() {
|
|
69
|
+
if (typeof window === "undefined") {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
return window[WINDOW_DATA_KEY] ?? null;
|
|
73
|
+
}
|
|
74
|
+
function getRouterData() {
|
|
75
|
+
if (typeof window === "undefined") {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
return window[ROUTER_DATA_KEY] ?? null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// modules/react/hooks/useRouter/index.ts
|
|
82
|
+
function useRouter() {
|
|
83
|
+
const context = useContext2(RouterContext);
|
|
84
|
+
const navigate = context?.navigate;
|
|
85
|
+
const navigateRef = useRef(navigate);
|
|
86
|
+
useEffect3(() => {
|
|
87
|
+
navigateRef.current = navigate;
|
|
88
|
+
}, [navigate]);
|
|
89
|
+
const [routeData, setRouteData] = useState3(() => {
|
|
90
|
+
if (typeof window === "undefined") {
|
|
91
|
+
return {
|
|
92
|
+
pathname: "",
|
|
93
|
+
query: {},
|
|
94
|
+
searchParams: {},
|
|
95
|
+
params: {}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
const data = getWindowData();
|
|
99
|
+
const routerData = getRouterData();
|
|
100
|
+
const searchParams = routerData?.searchParams || parseQueryString(window.location.search);
|
|
101
|
+
return {
|
|
102
|
+
pathname: routerData?.pathname || data?.pathname || window.location.pathname,
|
|
103
|
+
query: searchParams,
|
|
104
|
+
// For backward compatibility
|
|
105
|
+
searchParams,
|
|
106
|
+
params: routerData?.params || data?.params || {}
|
|
107
|
+
};
|
|
108
|
+
});
|
|
109
|
+
useEffect3(() => {
|
|
110
|
+
if (typeof window === "undefined") return;
|
|
111
|
+
const handleDataRefresh = () => {
|
|
112
|
+
const data = getWindowData();
|
|
113
|
+
const routerData = getRouterData();
|
|
114
|
+
const currentPathname = window.location.pathname;
|
|
115
|
+
const currentSearch = window.location.search;
|
|
116
|
+
const searchParams = routerData?.searchParams || parseQueryString(currentSearch);
|
|
117
|
+
setRouteData({
|
|
118
|
+
pathname: routerData?.pathname || data?.pathname || currentPathname,
|
|
119
|
+
query: searchParams,
|
|
120
|
+
// For backward compatibility
|
|
121
|
+
searchParams,
|
|
122
|
+
params: routerData?.params || data?.params || {}
|
|
123
|
+
});
|
|
124
|
+
};
|
|
125
|
+
window.addEventListener("fw-data-refresh", handleDataRefresh);
|
|
126
|
+
window.addEventListener("fw-router-data-refresh", handleDataRefresh);
|
|
127
|
+
const handlePopState = () => {
|
|
128
|
+
handleDataRefresh();
|
|
129
|
+
};
|
|
130
|
+
window.addEventListener("popstate", handlePopState);
|
|
131
|
+
return () => {
|
|
132
|
+
window.removeEventListener("fw-data-refresh", handleDataRefresh);
|
|
133
|
+
window.removeEventListener("fw-router-data-refresh", handleDataRefresh);
|
|
134
|
+
window.removeEventListener("popstate", handlePopState);
|
|
135
|
+
};
|
|
136
|
+
}, []);
|
|
137
|
+
const push = useCallback(
|
|
138
|
+
async (url, options) => {
|
|
139
|
+
const fullUrl = url.startsWith("/") ? url : `/${url}`;
|
|
140
|
+
const getCurrentNavigate = () => {
|
|
141
|
+
if (navigateRef.current) return navigateRef.current;
|
|
142
|
+
if (navigate) return navigate;
|
|
143
|
+
if (typeof window !== "undefined" && window[ROUTER_NAVIGATE_KEY]) {
|
|
144
|
+
return window[ROUTER_NAVIGATE_KEY];
|
|
145
|
+
}
|
|
146
|
+
return null;
|
|
147
|
+
};
|
|
148
|
+
let currentNavigate = getCurrentNavigate();
|
|
149
|
+
if (typeof window === "undefined") {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (!currentNavigate) {
|
|
153
|
+
for (let i = 0; i < 10; i++) {
|
|
154
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
155
|
+
currentNavigate = getCurrentNavigate();
|
|
156
|
+
if (currentNavigate) break;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
if (!currentNavigate) {
|
|
160
|
+
window.location.href = fullUrl;
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const currentUrl = window.location.pathname + window.location.search;
|
|
164
|
+
if (fullUrl === currentUrl) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
window.history.pushState({}, "", fullUrl);
|
|
168
|
+
await currentNavigate(fullUrl, options);
|
|
169
|
+
},
|
|
170
|
+
[navigate]
|
|
171
|
+
// Include navigate in dependencies so it updates when context becomes available
|
|
172
|
+
);
|
|
173
|
+
const replace = useCallback(
|
|
174
|
+
async (url, options) => {
|
|
175
|
+
const fullUrl = url.startsWith("/") ? url : `/${url}`;
|
|
176
|
+
const getCurrentNavigate = () => {
|
|
177
|
+
if (navigateRef.current) return navigateRef.current;
|
|
178
|
+
if (navigate) return navigate;
|
|
179
|
+
if (typeof window !== "undefined" && window[ROUTER_NAVIGATE_KEY]) {
|
|
180
|
+
return window[ROUTER_NAVIGATE_KEY];
|
|
181
|
+
}
|
|
182
|
+
return null;
|
|
183
|
+
};
|
|
184
|
+
let currentNavigate = getCurrentNavigate();
|
|
185
|
+
if (typeof window === "undefined") {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
if (!currentNavigate) {
|
|
189
|
+
for (let i = 0; i < 10; i++) {
|
|
190
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
191
|
+
currentNavigate = getCurrentNavigate();
|
|
192
|
+
if (currentNavigate) break;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if (!currentNavigate) {
|
|
196
|
+
window.location.replace(fullUrl);
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
window.history.replaceState({}, "", fullUrl);
|
|
200
|
+
await currentNavigate(fullUrl, options);
|
|
201
|
+
},
|
|
202
|
+
[navigate]
|
|
203
|
+
);
|
|
204
|
+
const back = useCallback(() => {
|
|
205
|
+
if (typeof window !== "undefined") {
|
|
206
|
+
window.history.back();
|
|
207
|
+
}
|
|
208
|
+
}, []);
|
|
209
|
+
const refresh = useCallback(async () => {
|
|
210
|
+
const currentUrl = typeof window !== "undefined" ? window.location.pathname + window.location.search : routeData.pathname;
|
|
211
|
+
const getCurrentNavigate = () => {
|
|
212
|
+
if (navigateRef.current) return navigateRef.current;
|
|
213
|
+
if (navigate) return navigate;
|
|
214
|
+
if (typeof window !== "undefined" && window[ROUTER_NAVIGATE_KEY]) {
|
|
215
|
+
return window[ROUTER_NAVIGATE_KEY];
|
|
216
|
+
}
|
|
217
|
+
return null;
|
|
218
|
+
};
|
|
219
|
+
let currentNavigate = getCurrentNavigate();
|
|
220
|
+
if (typeof window === "undefined") {
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
if (!currentNavigate) {
|
|
224
|
+
for (let i = 0; i < 10; i++) {
|
|
225
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
226
|
+
currentNavigate = getCurrentNavigate();
|
|
227
|
+
if (currentNavigate) break;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
if (!currentNavigate) {
|
|
231
|
+
window.location.reload();
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
await currentNavigate(currentUrl, { revalidate: true });
|
|
235
|
+
}, [navigate, routeData.pathname]);
|
|
236
|
+
return {
|
|
237
|
+
push,
|
|
238
|
+
replace,
|
|
239
|
+
back,
|
|
240
|
+
refresh,
|
|
241
|
+
pathname: routeData.pathname,
|
|
242
|
+
query: routeData.query,
|
|
243
|
+
searchParams: routeData.searchParams,
|
|
244
|
+
params: routeData.params
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
function parseQueryString(search) {
|
|
248
|
+
const params = {};
|
|
249
|
+
if (!search || search.length === 0) return params;
|
|
250
|
+
const queryString = search.startsWith("?") ? search.slice(1) : search;
|
|
251
|
+
const pairs = queryString.split("&");
|
|
252
|
+
for (const pair of pairs) {
|
|
253
|
+
const [key, value] = pair.split("=");
|
|
254
|
+
if (key) {
|
|
255
|
+
params[decodeURIComponent(key)] = value ? decodeURIComponent(value) : "";
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return params;
|
|
259
|
+
}
|
|
54
260
|
export {
|
|
55
261
|
useBroadcastChannel,
|
|
56
|
-
usePageProps
|
|
262
|
+
usePageProps,
|
|
263
|
+
useRouter
|
|
57
264
|
};
|
|
58
265
|
//# sourceMappingURL=hooks.js.map
|