arcway 0.4.19 → 0.4.20
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/client/router.js +52 -18
- package/package.json +1 -1
package/client/router.js
CHANGED
|
@@ -45,6 +45,29 @@ function normalizeNavigateTarget(to) {
|
|
|
45
45
|
return qs ? `${pathname}?${qs}` : pathname;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
+
function safeNextTarget(value, fallback) {
|
|
49
|
+
if (
|
|
50
|
+
typeof value !== 'string' ||
|
|
51
|
+
!value.startsWith('/') ||
|
|
52
|
+
value.startsWith('//') ||
|
|
53
|
+
/\\|[\u0000-\u001f\u007f]/.test(value)
|
|
54
|
+
) {
|
|
55
|
+
return fallback;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
let decoded;
|
|
59
|
+
try {
|
|
60
|
+
decoded = decodeURIComponent(value);
|
|
61
|
+
} catch {
|
|
62
|
+
return fallback;
|
|
63
|
+
}
|
|
64
|
+
if (/\\|[\u0000-\u001f\u007f]/.test(decoded)) return fallback;
|
|
65
|
+
|
|
66
|
+
const url = new URL(value, 'http://arcway.local');
|
|
67
|
+
if (url.origin !== 'http://arcway.local') return fallback;
|
|
68
|
+
return `${url.pathname}${url.search}${url.hash}`;
|
|
69
|
+
}
|
|
70
|
+
|
|
48
71
|
function useRouter() {
|
|
49
72
|
const ctx = useContext(RouterContext);
|
|
50
73
|
if (!ctx) {
|
|
@@ -52,24 +75,35 @@ function useRouter() {
|
|
|
52
75
|
}
|
|
53
76
|
|
|
54
77
|
return useMemo(
|
|
55
|
-
() =>
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
ctx.
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
78
|
+
() => {
|
|
79
|
+
const query = typeof window !== 'undefined' ? parseQuery(window.location.search) : {};
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
pathname: ctx.pathname,
|
|
83
|
+
params: ctx.params,
|
|
84
|
+
query,
|
|
85
|
+
push: (to, options) =>
|
|
86
|
+
ctx.navigate(normalizeNavigateTarget(to), { ...options, replace: false }),
|
|
87
|
+
replace: (to, options) =>
|
|
88
|
+
ctx.navigate(normalizeNavigateTarget(to), { ...options, replace: true }),
|
|
89
|
+
next: (fallback, options) => {
|
|
90
|
+
const fallbackTarget = normalizeNavigateTarget(fallback);
|
|
91
|
+
return ctx.navigate(safeNextTarget(query.next, fallbackTarget), {
|
|
92
|
+
...options,
|
|
93
|
+
replace: options?.replace === true,
|
|
94
|
+
});
|
|
95
|
+
},
|
|
96
|
+
back: () => {
|
|
97
|
+
if (typeof window !== 'undefined') window.history.back();
|
|
98
|
+
},
|
|
99
|
+
forward: () => {
|
|
100
|
+
if (typeof window !== 'undefined') window.history.forward();
|
|
101
|
+
},
|
|
102
|
+
refresh: () => {
|
|
103
|
+
if (typeof window !== 'undefined') window.location.reload();
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
},
|
|
73
107
|
// queryVersion bumps on every query-only navigation or same-path popstate,
|
|
74
108
|
// so memoised consumers re-read the URL.
|
|
75
109
|
[ctx.pathname, ctx.params, ctx.navigate, ctx.queryVersion],
|