@akanjs/client 0.9.5 → 0.9.6

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 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 = (href, lang, prefix) => {
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 = href === `/${lang}` ? "/" : href.startsWith(`/${lang}/`) ? href.slice(langLength) : href;
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
- return { path, pathname };
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 { path, pathname } = this.#getPathInfo(href);
79
- this.#postPathChange({ path, pathname });
80
- void router2.push(pathname);
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 { path, pathname } = this.#getPathInfo(href);
85
- this.#postPathChange({ path, pathname });
86
- void router2.replace(pathname);
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 { path, pathname } = this.#getPathInfo(document.referrer);
91
- this.#postPathChange({ path, pathname });
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 { path, pathname } = this.#getPathInfo(location.pathname);
97
- this.#postPathChange({ path, pathname });
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
- if (location.pathname === pathname)
107
- return;
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
- if (location.pathname === pathname)
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, routeOptions);
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
- if (location.pathname === pathname)
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 = (href, lang, prefix) => {
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 = href === `/${lang}` ? "/" : href.startsWith(`/${lang}/`) ? href.slice(langLength) : href;
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
- return { path, pathname };
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 { path, pathname } = this.#getPathInfo(href);
56
- this.#postPathChange({ path, pathname });
57
- void router2.push(pathname);
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 { path, pathname } = this.#getPathInfo(href);
62
- this.#postPathChange({ path, pathname });
63
- void router2.replace(pathname);
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 { path, pathname } = this.#getPathInfo(document.referrer);
68
- this.#postPathChange({ path, pathname });
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 { path, pathname } = this.#getPathInfo(location.pathname);
74
- this.#postPathChange({ path, pathname });
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
- if (location.pathname === pathname)
84
- return;
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
- if (location.pathname === pathname)
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, routeOptions);
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
- if (location.pathname === pathname)
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@akanjs/client",
3
- "version": "0.9.5",
3
+ "version": "0.9.6",
4
4
  "sourceType": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"
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: (href: string, lang: string, prefix: string) => {
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;