@akanjs/client 0.9.4 → 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;
@@ -74,55 +77,53 @@ class Router {
74
77
  #initNextClientRouter(options) {
75
78
  this.#instance = {
76
79
  push: (href) => {
77
- const { path, pathname } = this.#getPathInfo(href);
78
- this.#postPathChange({ path, pathname });
79
- options.router.push(pathname);
80
+ const router2 = options.router;
81
+ const pathInfo = this.#getPathInfo(href);
82
+ this.#postPathChange(pathInfo);
83
+ void router2.push(pathInfo.href);
80
84
  },
81
85
  replace: (href) => {
82
- const { path, pathname } = this.#getPathInfo(href);
83
- this.#postPathChange({ path, pathname });
84
- options.router.replace(pathname);
86
+ const router2 = options.router;
87
+ const pathInfo = this.#getPathInfo(href);
88
+ this.#postPathChange(pathInfo);
89
+ void router2.replace(pathInfo.href);
85
90
  },
86
91
  back: () => {
87
- const { path, pathname } = this.#getPathInfo(document.referrer);
88
- this.#postPathChange({ path, pathname });
89
- options.router.back();
92
+ const router2 = options.router;
93
+ const pathInfo = this.#getPathInfo(document.referrer);
94
+ this.#postPathChange(pathInfo);
95
+ router2.back();
90
96
  },
91
97
  refresh: () => {
92
- const { path, pathname } = this.#getPathInfo(location.pathname);
93
- this.#postPathChange({ path, pathname });
94
- options.router.refresh();
98
+ const router2 = options.router;
99
+ const pathInfo = this.#getPathInfo(location.pathname);
100
+ this.#postPathChange(pathInfo);
101
+ router2.reload();
95
102
  }
96
103
  };
97
104
  }
98
105
  #initCSRClientRouter(options) {
99
106
  this.#instance = {
100
- push: (href) => {
101
- const { path, pathname } = this.#getPathInfo(href);
102
- if (location.pathname === pathname)
103
- return;
104
- this.#postPathChange({ path, pathname });
105
- options.router.push(pathname);
107
+ push: (href, routeOptions) => {
108
+ const { path, pathname, hash } = this.#getPathInfo(href);
109
+ this.#postPathChange({ path, pathname, hash });
110
+ options.router.push(`${pathname}${hash ? `#${hash}` : ""}`, routeOptions);
106
111
  },
107
- replace: (href) => {
108
- const { path, pathname } = this.#getPathInfo(href);
109
- if (location.pathname === pathname)
110
- return;
111
- this.#postPathChange({ path, pathname });
112
+ replace: (href, routeOptions) => {
113
+ const { path, pathname, hash } = this.#getPathInfo(href);
114
+ this.#postPathChange({ path, pathname, hash });
112
115
  setTimeout(() => {
113
- options.router.replace(pathname);
116
+ options.router.replace(`${pathname}${hash ? `#${hash}` : ""}`, routeOptions);
114
117
  }, 0);
115
118
  },
116
- back: () => {
117
- const { path, pathname } = this.#getPathInfo(document.referrer);
118
- if (location.pathname === pathname)
119
- return;
120
- this.#postPathChange({ path, pathname });
121
- options.router.back();
119
+ back: (routeOptions) => {
120
+ const { path, pathname, hash } = this.#getPathInfo(document.referrer);
121
+ this.#postPathChange({ path, pathname, hash });
122
+ options.router.back(routeOptions);
122
123
  },
123
124
  refresh: () => {
124
- const { path, pathname } = this.#getPathInfo(location.pathname);
125
- this.#postPathChange({ path, pathname });
125
+ const { path, pathname, hash } = this.#getPathInfo(location.pathname);
126
+ this.#postPathChange({ path, pathname, hash });
126
127
  options.router.refresh();
127
128
  }
128
129
  };
@@ -134,25 +135,25 @@ class Router {
134
135
  #getPathInfo(href, prefix = this.#prefix) {
135
136
  return getPathInfo(href, this.#lang, prefix);
136
137
  }
137
- #postPathChange({ path, pathname }) {
138
- import_common.Logger.log(`pathChange-start:${path}`);
139
- 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 }, "*");
140
141
  }
141
- push(href) {
142
+ push(href, routeOptions) {
142
143
  this.#checkInitialized();
143
- this.#instance.push(href);
144
+ this.#instance.push(href, routeOptions);
144
145
  return void 0;
145
146
  }
146
- replace(href) {
147
+ replace(href, routeOptions) {
147
148
  this.#checkInitialized();
148
- this.#instance.replace(href);
149
+ this.#instance.replace(href, routeOptions);
149
150
  return void 0;
150
151
  }
151
- back() {
152
+ back(routeOptions) {
152
153
  if (import_base.baseClientEnv.side === "server")
153
154
  throw new Error("back is only available in client side");
154
155
  this.#checkInitialized();
155
- this.#instance.back();
156
+ this.#instance.back(routeOptions);
156
157
  return void 0;
157
158
  }
158
159
  refresh() {
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;
@@ -51,55 +54,53 @@ class Router {
51
54
  #initNextClientRouter(options) {
52
55
  this.#instance = {
53
56
  push: (href) => {
54
- const { path, pathname } = this.#getPathInfo(href);
55
- this.#postPathChange({ path, pathname });
56
- options.router.push(pathname);
57
+ const router2 = options.router;
58
+ const pathInfo = this.#getPathInfo(href);
59
+ this.#postPathChange(pathInfo);
60
+ void router2.push(pathInfo.href);
57
61
  },
58
62
  replace: (href) => {
59
- const { path, pathname } = this.#getPathInfo(href);
60
- this.#postPathChange({ path, pathname });
61
- options.router.replace(pathname);
63
+ const router2 = options.router;
64
+ const pathInfo = this.#getPathInfo(href);
65
+ this.#postPathChange(pathInfo);
66
+ void router2.replace(pathInfo.href);
62
67
  },
63
68
  back: () => {
64
- const { path, pathname } = this.#getPathInfo(document.referrer);
65
- this.#postPathChange({ path, pathname });
66
- options.router.back();
69
+ const router2 = options.router;
70
+ const pathInfo = this.#getPathInfo(document.referrer);
71
+ this.#postPathChange(pathInfo);
72
+ router2.back();
67
73
  },
68
74
  refresh: () => {
69
- const { path, pathname } = this.#getPathInfo(location.pathname);
70
- this.#postPathChange({ path, pathname });
71
- options.router.refresh();
75
+ const router2 = options.router;
76
+ const pathInfo = this.#getPathInfo(location.pathname);
77
+ this.#postPathChange(pathInfo);
78
+ router2.reload();
72
79
  }
73
80
  };
74
81
  }
75
82
  #initCSRClientRouter(options) {
76
83
  this.#instance = {
77
- push: (href) => {
78
- const { path, pathname } = this.#getPathInfo(href);
79
- if (location.pathname === pathname)
80
- return;
81
- this.#postPathChange({ path, pathname });
82
- options.router.push(pathname);
84
+ push: (href, routeOptions) => {
85
+ const { path, pathname, hash } = this.#getPathInfo(href);
86
+ this.#postPathChange({ path, pathname, hash });
87
+ options.router.push(`${pathname}${hash ? `#${hash}` : ""}`, routeOptions);
83
88
  },
84
- replace: (href) => {
85
- const { path, pathname } = this.#getPathInfo(href);
86
- if (location.pathname === pathname)
87
- return;
88
- this.#postPathChange({ path, pathname });
89
+ replace: (href, routeOptions) => {
90
+ const { path, pathname, hash } = this.#getPathInfo(href);
91
+ this.#postPathChange({ path, pathname, hash });
89
92
  setTimeout(() => {
90
- options.router.replace(pathname);
93
+ options.router.replace(`${pathname}${hash ? `#${hash}` : ""}`, routeOptions);
91
94
  }, 0);
92
95
  },
93
- back: () => {
94
- const { path, pathname } = this.#getPathInfo(document.referrer);
95
- if (location.pathname === pathname)
96
- return;
97
- this.#postPathChange({ path, pathname });
98
- options.router.back();
96
+ back: (routeOptions) => {
97
+ const { path, pathname, hash } = this.#getPathInfo(document.referrer);
98
+ this.#postPathChange({ path, pathname, hash });
99
+ options.router.back(routeOptions);
99
100
  },
100
101
  refresh: () => {
101
- const { path, pathname } = this.#getPathInfo(location.pathname);
102
- this.#postPathChange({ path, pathname });
102
+ const { path, pathname, hash } = this.#getPathInfo(location.pathname);
103
+ this.#postPathChange({ path, pathname, hash });
103
104
  options.router.refresh();
104
105
  }
105
106
  };
@@ -111,25 +112,25 @@ class Router {
111
112
  #getPathInfo(href, prefix = this.#prefix) {
112
113
  return getPathInfo(href, this.#lang, prefix);
113
114
  }
114
- #postPathChange({ path, pathname }) {
115
- Logger.log(`pathChange-start:${path}`);
116
- 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 }, "*");
117
118
  }
118
- push(href) {
119
+ push(href, routeOptions) {
119
120
  this.#checkInitialized();
120
- this.#instance.push(href);
121
+ this.#instance.push(href, routeOptions);
121
122
  return void 0;
122
123
  }
123
- replace(href) {
124
+ replace(href, routeOptions) {
124
125
  this.#checkInitialized();
125
- this.#instance.replace(href);
126
+ this.#instance.replace(href, routeOptions);
126
127
  return void 0;
127
128
  }
128
- back() {
129
+ back(routeOptions) {
129
130
  if (baseClientEnv.side === "server")
130
131
  throw new Error("back is only available in client side");
131
132
  this.#checkInitialized();
132
- this.#instance.back();
133
+ this.#instance.back(routeOptions);
133
134
  return void 0;
134
135
  }
135
136
  refresh() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@akanjs/client",
3
- "version": "0.9.4",
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
@@ -1,7 +1,10 @@
1
+ export interface RouteOptions {
2
+ scrollToTop?: boolean;
3
+ }
1
4
  export interface RouterInstance {
2
- push: (href: string) => void;
3
- replace: (href: string) => void;
4
- back: () => void;
5
+ push: (href: string, routeOptions?: RouteOptions) => void;
6
+ replace: (href: string, routeOptions?: RouteOptions) => void;
7
+ back: (routeOptions?: RouteOptions) => void;
5
8
  refresh: () => void;
6
9
  }
7
10
  interface RouterOptions {
@@ -21,17 +24,20 @@ interface CSRClientRouterOption extends RouterOptions {
21
24
  type: "csr";
22
25
  router: RouterInstance;
23
26
  }
24
- export declare const getPathInfo: (href: string, lang: string, prefix: string) => {
27
+ export declare const getPathInfo: (requestUrl: string, lang: string, prefix: string) => {
25
28
  path: string;
26
29
  pathname: string;
30
+ hash: string;
31
+ search: string;
32
+ href: string;
27
33
  };
28
34
  declare class Router {
29
35
  #private;
30
36
  isInitialized: boolean;
31
37
  init(options: NextClientRouterOption | NextServerRouterOption | CSRClientRouterOption): void;
32
- push(href: string): never;
33
- replace(href: string): never;
34
- back(): never;
38
+ push(href: string, routeOptions?: RouteOptions): never;
39
+ replace(href: string, routeOptions?: RouteOptions): never;
40
+ back(routeOptions?: RouteOptions): never;
35
41
  refresh(): never;
36
42
  redirect(href: string): Promise<never>;
37
43
  notFound(): never;