@akanjs/client 0.9.5 → 0.9.7
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/cjs/src/router.js +29 -32
- package/esm/src/router.js +29 -32
- package/package.json +1 -1
- package/src/csrTypes.d.ts +2 -0
- package/src/router.d.ts +4 -1
package/cjs/src/router.js
CHANGED
|
@@ -24,14 +24,17 @@ module.exports = __toCommonJS(router_exports);
|
|
|
24
24
|
var import_base = require("@akanjs/base");
|
|
25
25
|
var import_common = require("@akanjs/common");
|
|
26
26
|
var import_navigation = require("next/navigation");
|
|
27
|
-
const getPathInfo = (
|
|
27
|
+
const getPathInfo = (requestUrl, lang, prefix) => {
|
|
28
|
+
const [urlWithoutHash, hash = ""] = requestUrl.split("#");
|
|
29
|
+
const [url, search = ""] = urlWithoutHash.split("?");
|
|
28
30
|
const langLength = lang.length + 1;
|
|
29
|
-
const pathWithSubRoute =
|
|
31
|
+
const pathWithSubRoute = url === `/${lang}` ? "/" : url.startsWith(`/${lang}/`) ? url.slice(langLength) : url;
|
|
30
32
|
const prefixLength = prefix ? prefix.length + 1 : 0;
|
|
31
33
|
const path = !prefixLength ? pathWithSubRoute : pathWithSubRoute === `/${prefix}` ? "/" : pathWithSubRoute.startsWith(`/${prefix}`) ? pathWithSubRoute.slice(prefixLength) : pathWithSubRoute;
|
|
32
34
|
const subRoute = prefix ? `/${prefix}` : "";
|
|
33
35
|
const pathname = path.startsWith("http") ? path : path === "/" ? `/${lang}${subRoute}` : `/${lang}${subRoute}${path}`;
|
|
34
|
-
|
|
36
|
+
const href = `${pathname}${search ? `?${search}` : ""}${hash ? `#${hash}` : ""}`;
|
|
37
|
+
return { path, pathname, hash, search, href };
|
|
35
38
|
};
|
|
36
39
|
class Router {
|
|
37
40
|
isInitialized = false;
|
|
@@ -75,26 +78,26 @@ class Router {
|
|
|
75
78
|
this.#instance = {
|
|
76
79
|
push: (href) => {
|
|
77
80
|
const router2 = options.router;
|
|
78
|
-
const
|
|
79
|
-
this.#postPathChange(
|
|
80
|
-
void router2.push(
|
|
81
|
+
const pathInfo = this.#getPathInfo(href);
|
|
82
|
+
this.#postPathChange(pathInfo);
|
|
83
|
+
void router2.push(pathInfo.href);
|
|
81
84
|
},
|
|
82
85
|
replace: (href) => {
|
|
83
86
|
const router2 = options.router;
|
|
84
|
-
const
|
|
85
|
-
this.#postPathChange(
|
|
86
|
-
void router2.replace(
|
|
87
|
+
const pathInfo = this.#getPathInfo(href);
|
|
88
|
+
this.#postPathChange(pathInfo);
|
|
89
|
+
void router2.replace(pathInfo.href);
|
|
87
90
|
},
|
|
88
91
|
back: () => {
|
|
89
92
|
const router2 = options.router;
|
|
90
|
-
const
|
|
91
|
-
this.#postPathChange(
|
|
93
|
+
const pathInfo = this.#getPathInfo(document.referrer);
|
|
94
|
+
this.#postPathChange(pathInfo);
|
|
92
95
|
router2.back();
|
|
93
96
|
},
|
|
94
97
|
refresh: () => {
|
|
95
98
|
const router2 = options.router;
|
|
96
|
-
const
|
|
97
|
-
this.#postPathChange(
|
|
99
|
+
const pathInfo = this.#getPathInfo(location.pathname);
|
|
100
|
+
this.#postPathChange(pathInfo);
|
|
98
101
|
router2.reload();
|
|
99
102
|
}
|
|
100
103
|
};
|
|
@@ -102,31 +105,25 @@ class Router {
|
|
|
102
105
|
#initCSRClientRouter(options) {
|
|
103
106
|
this.#instance = {
|
|
104
107
|
push: (href, routeOptions) => {
|
|
105
|
-
const { path, pathname } = this.#getPathInfo(href);
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
this.#postPathChange({ path, pathname });
|
|
109
|
-
options.router.push(pathname, routeOptions);
|
|
108
|
+
const { path, pathname, hash } = this.#getPathInfo(href);
|
|
109
|
+
this.#postPathChange({ path, pathname, hash });
|
|
110
|
+
options.router.push(`${pathname}${hash ? `#${hash}` : ""}`, routeOptions);
|
|
110
111
|
},
|
|
111
112
|
replace: (href, routeOptions) => {
|
|
112
|
-
const { path, pathname } = this.#getPathInfo(href);
|
|
113
|
-
|
|
114
|
-
return;
|
|
115
|
-
this.#postPathChange({ path, pathname });
|
|
113
|
+
const { path, pathname, hash } = this.#getPathInfo(href);
|
|
114
|
+
this.#postPathChange({ path, pathname, hash });
|
|
116
115
|
setTimeout(() => {
|
|
117
|
-
options.router.replace(pathname
|
|
116
|
+
options.router.replace(`${pathname}${hash ? `#${hash}` : ""}`, routeOptions);
|
|
118
117
|
}, 0);
|
|
119
118
|
},
|
|
120
119
|
back: (routeOptions) => {
|
|
121
|
-
const { path, pathname } = this.#getPathInfo(document.referrer);
|
|
122
|
-
|
|
123
|
-
return;
|
|
124
|
-
this.#postPathChange({ path, pathname });
|
|
120
|
+
const { path, pathname, hash } = this.#getPathInfo(document.referrer);
|
|
121
|
+
this.#postPathChange({ path, pathname, hash });
|
|
125
122
|
options.router.back(routeOptions);
|
|
126
123
|
},
|
|
127
124
|
refresh: () => {
|
|
128
|
-
const { path, pathname } = this.#getPathInfo(location.pathname);
|
|
129
|
-
this.#postPathChange({ path, pathname });
|
|
125
|
+
const { path, pathname, hash } = this.#getPathInfo(location.pathname);
|
|
126
|
+
this.#postPathChange({ path, pathname, hash });
|
|
130
127
|
options.router.refresh();
|
|
131
128
|
}
|
|
132
129
|
};
|
|
@@ -138,9 +135,9 @@ class Router {
|
|
|
138
135
|
#getPathInfo(href, prefix = this.#prefix) {
|
|
139
136
|
return getPathInfo(href, this.#lang, prefix);
|
|
140
137
|
}
|
|
141
|
-
#postPathChange({ path, pathname }) {
|
|
142
|
-
import_common.Logger.log(`pathChange-start:${path}`);
|
|
143
|
-
window.parent.postMessage({ type: "pathChange", path, pathname }, "*");
|
|
138
|
+
#postPathChange({ path, pathname, hash }) {
|
|
139
|
+
import_common.Logger.log(`pathChange-start:${path}${hash ? `#${hash}` : ""}`);
|
|
140
|
+
window.parent.postMessage({ type: "pathChange", path, pathname, hash }, "*");
|
|
144
141
|
}
|
|
145
142
|
push(href, routeOptions) {
|
|
146
143
|
this.#checkInitialized();
|
package/esm/src/router.js
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import { baseClientEnv } from "@akanjs/base";
|
|
2
2
|
import { Logger } from "@akanjs/common";
|
|
3
3
|
import { notFound, redirect } from "next/navigation";
|
|
4
|
-
const getPathInfo = (
|
|
4
|
+
const getPathInfo = (requestUrl, lang, prefix) => {
|
|
5
|
+
const [urlWithoutHash, hash = ""] = requestUrl.split("#");
|
|
6
|
+
const [url, search = ""] = urlWithoutHash.split("?");
|
|
5
7
|
const langLength = lang.length + 1;
|
|
6
|
-
const pathWithSubRoute =
|
|
8
|
+
const pathWithSubRoute = url === `/${lang}` ? "/" : url.startsWith(`/${lang}/`) ? url.slice(langLength) : url;
|
|
7
9
|
const prefixLength = prefix ? prefix.length + 1 : 0;
|
|
8
10
|
const path = !prefixLength ? pathWithSubRoute : pathWithSubRoute === `/${prefix}` ? "/" : pathWithSubRoute.startsWith(`/${prefix}`) ? pathWithSubRoute.slice(prefixLength) : pathWithSubRoute;
|
|
9
11
|
const subRoute = prefix ? `/${prefix}` : "";
|
|
10
12
|
const pathname = path.startsWith("http") ? path : path === "/" ? `/${lang}${subRoute}` : `/${lang}${subRoute}${path}`;
|
|
11
|
-
|
|
13
|
+
const href = `${pathname}${search ? `?${search}` : ""}${hash ? `#${hash}` : ""}`;
|
|
14
|
+
return { path, pathname, hash, search, href };
|
|
12
15
|
};
|
|
13
16
|
class Router {
|
|
14
17
|
isInitialized = false;
|
|
@@ -52,26 +55,26 @@ class Router {
|
|
|
52
55
|
this.#instance = {
|
|
53
56
|
push: (href) => {
|
|
54
57
|
const router2 = options.router;
|
|
55
|
-
const
|
|
56
|
-
this.#postPathChange(
|
|
57
|
-
void router2.push(
|
|
58
|
+
const pathInfo = this.#getPathInfo(href);
|
|
59
|
+
this.#postPathChange(pathInfo);
|
|
60
|
+
void router2.push(pathInfo.href);
|
|
58
61
|
},
|
|
59
62
|
replace: (href) => {
|
|
60
63
|
const router2 = options.router;
|
|
61
|
-
const
|
|
62
|
-
this.#postPathChange(
|
|
63
|
-
void router2.replace(
|
|
64
|
+
const pathInfo = this.#getPathInfo(href);
|
|
65
|
+
this.#postPathChange(pathInfo);
|
|
66
|
+
void router2.replace(pathInfo.href);
|
|
64
67
|
},
|
|
65
68
|
back: () => {
|
|
66
69
|
const router2 = options.router;
|
|
67
|
-
const
|
|
68
|
-
this.#postPathChange(
|
|
70
|
+
const pathInfo = this.#getPathInfo(document.referrer);
|
|
71
|
+
this.#postPathChange(pathInfo);
|
|
69
72
|
router2.back();
|
|
70
73
|
},
|
|
71
74
|
refresh: () => {
|
|
72
75
|
const router2 = options.router;
|
|
73
|
-
const
|
|
74
|
-
this.#postPathChange(
|
|
76
|
+
const pathInfo = this.#getPathInfo(location.pathname);
|
|
77
|
+
this.#postPathChange(pathInfo);
|
|
75
78
|
router2.reload();
|
|
76
79
|
}
|
|
77
80
|
};
|
|
@@ -79,31 +82,25 @@ class Router {
|
|
|
79
82
|
#initCSRClientRouter(options) {
|
|
80
83
|
this.#instance = {
|
|
81
84
|
push: (href, routeOptions) => {
|
|
82
|
-
const { path, pathname } = this.#getPathInfo(href);
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
this.#postPathChange({ path, pathname });
|
|
86
|
-
options.router.push(pathname, routeOptions);
|
|
85
|
+
const { path, pathname, hash } = this.#getPathInfo(href);
|
|
86
|
+
this.#postPathChange({ path, pathname, hash });
|
|
87
|
+
options.router.push(`${pathname}${hash ? `#${hash}` : ""}`, routeOptions);
|
|
87
88
|
},
|
|
88
89
|
replace: (href, routeOptions) => {
|
|
89
|
-
const { path, pathname } = this.#getPathInfo(href);
|
|
90
|
-
|
|
91
|
-
return;
|
|
92
|
-
this.#postPathChange({ path, pathname });
|
|
90
|
+
const { path, pathname, hash } = this.#getPathInfo(href);
|
|
91
|
+
this.#postPathChange({ path, pathname, hash });
|
|
93
92
|
setTimeout(() => {
|
|
94
|
-
options.router.replace(pathname
|
|
93
|
+
options.router.replace(`${pathname}${hash ? `#${hash}` : ""}`, routeOptions);
|
|
95
94
|
}, 0);
|
|
96
95
|
},
|
|
97
96
|
back: (routeOptions) => {
|
|
98
|
-
const { path, pathname } = this.#getPathInfo(document.referrer);
|
|
99
|
-
|
|
100
|
-
return;
|
|
101
|
-
this.#postPathChange({ path, pathname });
|
|
97
|
+
const { path, pathname, hash } = this.#getPathInfo(document.referrer);
|
|
98
|
+
this.#postPathChange({ path, pathname, hash });
|
|
102
99
|
options.router.back(routeOptions);
|
|
103
100
|
},
|
|
104
101
|
refresh: () => {
|
|
105
|
-
const { path, pathname } = this.#getPathInfo(location.pathname);
|
|
106
|
-
this.#postPathChange({ path, pathname });
|
|
102
|
+
const { path, pathname, hash } = this.#getPathInfo(location.pathname);
|
|
103
|
+
this.#postPathChange({ path, pathname, hash });
|
|
107
104
|
options.router.refresh();
|
|
108
105
|
}
|
|
109
106
|
};
|
|
@@ -115,9 +112,9 @@ class Router {
|
|
|
115
112
|
#getPathInfo(href, prefix = this.#prefix) {
|
|
116
113
|
return getPathInfo(href, this.#lang, prefix);
|
|
117
114
|
}
|
|
118
|
-
#postPathChange({ path, pathname }) {
|
|
119
|
-
Logger.log(`pathChange-start:${path}`);
|
|
120
|
-
window.parent.postMessage({ type: "pathChange", path, pathname }, "*");
|
|
115
|
+
#postPathChange({ path, pathname, hash }) {
|
|
116
|
+
Logger.log(`pathChange-start:${path}${hash ? `#${hash}` : ""}`);
|
|
117
|
+
window.parent.postMessage({ type: "pathChange", path, pathname, hash }, "*");
|
|
121
118
|
}
|
|
122
119
|
push(href, routeOptions) {
|
|
123
120
|
this.#checkInitialized();
|
package/package.json
CHANGED
package/src/csrTypes.d.ts
CHANGED
|
@@ -74,6 +74,7 @@ export type PageState = CsrState & {
|
|
|
74
74
|
};
|
|
75
75
|
export declare const defaultPageState: PageState;
|
|
76
76
|
export interface Location {
|
|
77
|
+
href: string;
|
|
77
78
|
pathname: string;
|
|
78
79
|
search: string;
|
|
79
80
|
params: {
|
|
@@ -83,6 +84,7 @@ export interface Location {
|
|
|
83
84
|
[key: string]: string | string[];
|
|
84
85
|
};
|
|
85
86
|
pathRoute: PathRoute;
|
|
87
|
+
hash: string;
|
|
86
88
|
}
|
|
87
89
|
export interface LocationState {
|
|
88
90
|
location: Location;
|
package/src/router.d.ts
CHANGED
|
@@ -24,9 +24,12 @@ interface CSRClientRouterOption extends RouterOptions {
|
|
|
24
24
|
type: "csr";
|
|
25
25
|
router: RouterInstance;
|
|
26
26
|
}
|
|
27
|
-
export declare const getPathInfo: (
|
|
27
|
+
export declare const getPathInfo: (requestUrl: string, lang: string, prefix: string) => {
|
|
28
28
|
path: string;
|
|
29
29
|
pathname: string;
|
|
30
|
+
hash: string;
|
|
31
|
+
search: string;
|
|
32
|
+
href: string;
|
|
30
33
|
};
|
|
31
34
|
declare class Router {
|
|
32
35
|
#private;
|