@dharmax/state-router 4.0.1 → 4.0.2

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/dist/router.d.ts CHANGED
@@ -10,6 +10,7 @@ export declare class Router {
10
10
  add(pattern: RegExp | string | RouteHandler, handler?: RouteHandler): Router;
11
11
  onNotFound(handler: (path: string) => void): Router;
12
12
  setDecodeParams(decode: boolean): Router;
13
+ willHandle(path: string): boolean;
13
14
  getQueryParams(search?: string): Record<string, string>;
14
15
  /**
15
16
  *
package/dist/router.js CHANGED
@@ -78,6 +78,22 @@ export class Router {
78
78
  this.#decodeParams = decode;
79
79
  return this;
80
80
  }
81
+ willHandle(path) {
82
+ path = decodeURI(path);
83
+ path = this.#clearQuery(path);
84
+ path = path.replace(/^\//, '');
85
+ if (this.#root !== '/' && this.#rootCompare && path.startsWith(this.#rootCompare)) {
86
+ path = path.slice(this.#rootCompare.length);
87
+ }
88
+ path = this.#cleanPathString(path);
89
+ if (this.#isStaticFile(path))
90
+ return false;
91
+ for (const route of this.#routes) {
92
+ if (route.pattern && route.pattern.test(path))
93
+ return true;
94
+ }
95
+ return !!this.#notFoundHandler;
96
+ }
81
97
  getQueryParams(search) {
82
98
  if (!this.#isBrowser() && !search)
83
99
  return {};
@@ -192,6 +208,9 @@ export class Router {
192
208
  const pathWithQuery = url.pathname + (url.search || '');
193
209
  if (self.#isStaticFile(pathWithQuery) || self.#isStaticFile(url.pathname))
194
210
  return;
211
+ // Only intercept if we have a handler for this route
212
+ if (!self.willHandle(pathWithQuery))
213
+ return;
195
214
  event.preventDefault();
196
215
  history.pushState({ path: pathWithQuery }, '', pathWithQuery);
197
216
  handler(pathWithQuery);
@@ -82,6 +82,22 @@ class Router {
82
82
  this.#decodeParams = decode;
83
83
  return this;
84
84
  }
85
+ willHandle(path) {
86
+ path = decodeURI(path);
87
+ path = this.#clearQuery(path);
88
+ path = path.replace(/^\//, '');
89
+ if (this.#root !== '/' && this.#rootCompare && path.startsWith(this.#rootCompare)) {
90
+ path = path.slice(this.#rootCompare.length);
91
+ }
92
+ path = this.#cleanPathString(path);
93
+ if (this.#isStaticFile(path))
94
+ return false;
95
+ for (const route of this.#routes) {
96
+ if (route.pattern && route.pattern.test(path))
97
+ return true;
98
+ }
99
+ return !!this.#notFoundHandler;
100
+ }
85
101
  getQueryParams(search) {
86
102
  if (!this.#isBrowser() && !search)
87
103
  return {};
@@ -196,6 +212,9 @@ class Router {
196
212
  const pathWithQuery = url.pathname + (url.search || '');
197
213
  if (self.#isStaticFile(pathWithQuery) || self.#isStaticFile(url.pathname))
198
214
  return;
215
+ // Only intercept if we have a handler for this route
216
+ if (!self.willHandle(pathWithQuery))
217
+ return;
199
218
  event.preventDefault();
200
219
  history.pushState({ path: pathWithQuery }, '', pathWithQuery);
201
220
  handler(pathWithQuery);
@@ -10,6 +10,7 @@ export declare class Router {
10
10
  add(pattern: RegExp | string | RouteHandler, handler?: RouteHandler): Router;
11
11
  onNotFound(handler: (path: string) => void): Router;
12
12
  setDecodeParams(decode: boolean): Router;
13
+ willHandle(path: string): boolean;
13
14
  getQueryParams(search?: string): Record<string, string>;
14
15
  /**
15
16
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dharmax/state-router",
3
- "version": "4.0.1",
3
+ "version": "4.0.2",
4
4
  "description": "A cute and tight router and application state controller",
5
5
  "type": "module",
6
6
  "main": "dist-cjs/index.cjs",