@ktjs/router 0.14.5 → 0.14.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/dist/index.d.ts CHANGED
@@ -124,10 +124,6 @@ interface RouterConfig {
124
124
  * Default is `true`
125
125
  */
126
126
  asyncGuards?: boolean;
127
- /**
128
- * Router mode: 'history' uses HTML5 history API, 'hash' uses URL hash. Default is 'hash'.
129
- */
130
- mode?: 'history' | 'hash';
131
127
  }
132
128
 
133
129
  /**
@@ -157,8 +153,6 @@ interface Router {
157
153
 
158
154
  /** Navigate forward in history */
159
155
  forward(): void;
160
-
161
- initCurrentRoute(): void;
162
156
  }
163
157
 
164
158
  interface RouteMatch {
@@ -169,15 +169,13 @@ var __ktjs_router__ = (function (exports) {
169
169
  const onNotFound = config.onNotFound ?? defaultHook;
170
170
  const onError = config.onError ?? defaultHook;
171
171
  const asyncGuards = config.asyncGuards ?? true;
172
- // default to 'hash' mode
173
- const mode = config.mode ?? 'hash';
174
172
  const baseUrl = config.baseUrl ?? '';
175
173
  // # private values
176
174
  const routes = [];
175
+ const history = [];
177
176
  let routerView = null;
178
- /**
179
- * Normalize routes by adding default guards
180
- */
177
+ let current = null;
178
+ // # methods
181
179
  const normalize = (rawRoutes, parentPath) => rawRoutes.map((route) => {
182
180
  const path = normalizePath(parentPath, route.path);
183
181
  const normalized = {
@@ -194,56 +192,6 @@ var __ktjs_router__ = (function (exports) {
194
192
  routes.push(normalized);
195
193
  return normalized;
196
194
  });
197
- // Normalize routes with default guards
198
- normalize(config.routes, '/');
199
- const { findByName, match } = createMatcher(routes);
200
- /**
201
- * Initialize current route from URL
202
- */
203
- const initCurrentRoute = () => {
204
- if (mode === 'hash') {
205
- const hash = window.location.hash.slice(1); // Remove '#'
206
- if (!hash) {
207
- return (current = null);
208
- }
209
- // Parse path and query
210
- const [path, queryString] = hash.split('?');
211
- const normalizedPath = normalizePath(path);
212
- // Match route
213
- const matched = match(normalizedPath);
214
- if (!matched) {
215
- return (current = null);
216
- }
217
- // Build route context
218
- return (current = {
219
- path: normalizedPath,
220
- name: matched.route.name,
221
- params: matched.params,
222
- query: queryString ? parseQuery(queryString) : {},
223
- meta: matched.route.meta ?? {},
224
- matched: matched.result,
225
- });
226
- }
227
- // history mode
228
- const pathWithQuery = window.location.pathname + window.location.search;
229
- const [path, queryString] = pathWithQuery.split('?');
230
- const normalizedPath = normalizePath(path);
231
- const matched = match(normalizedPath);
232
- if (!matched) {
233
- return (current = null);
234
- }
235
- return (current = {
236
- path: normalizedPath,
237
- name: matched.route.name,
238
- params: matched.params,
239
- query: queryString ? parseQuery(queryString) : {},
240
- meta: matched.route.meta ?? {},
241
- matched: matched.result,
242
- });
243
- };
244
- let current = null;
245
- const history = current ? [current] : [];
246
- // # methods
247
195
  const executeGuardsSync = (to, from, guardLevel) => {
248
196
  try {
249
197
  if (guardLevel === 0 /* GuardLevel.None */) {
@@ -387,22 +335,12 @@ var __ktjs_router__ = (function (exports) {
387
335
  }
388
336
  // Update browser history depending on mode
389
337
  const url = fullPath;
390
- if (mode === 'history') {
391
- if (replace) {
392
- window.history.replaceState({ path: to.path }, '', url);
393
- }
394
- else {
395
- window.history.pushState({ path: to.path }, '', url);
396
- }
338
+ const hashUrl = '#' + fullPath;
339
+ if (replace) {
340
+ window.location.replace(hashUrl);
397
341
  }
398
342
  else {
399
- const hashUrl = '#' + fullPath;
400
- if (replace) {
401
- window.location.replace(hashUrl);
402
- }
403
- else {
404
- window.location.hash = fullPath;
405
- }
343
+ window.location.hash = fullPath;
406
344
  }
407
345
  // Update current route in memory
408
346
  current = to;
@@ -464,23 +402,12 @@ var __ktjs_router__ = (function (exports) {
464
402
  return false;
465
403
  }
466
404
  // ---- Guards passed ----
467
- const url = fullPath;
468
- if (mode === 'history') {
469
- if (replace) {
470
- window.history.replaceState({ path: to.path }, '', url);
471
- }
472
- else {
473
- window.history.pushState({ path: to.path }, '', url);
474
- }
405
+ const hashUrl = '#' + fullPath;
406
+ if (replace) {
407
+ window.location.replace(hashUrl);
475
408
  }
476
409
  else {
477
- const hashUrl = '#' + fullPath;
478
- if (replace) {
479
- window.location.replace(hashUrl);
480
- }
481
- else {
482
- window.location.hash = fullPath;
483
- }
410
+ window.location.hash = fullPath;
484
411
  }
485
412
  current = to;
486
413
  if (replace) {
@@ -526,72 +453,63 @@ var __ktjs_router__ = (function (exports) {
526
453
  * Normalize navigation argument
527
454
  */
528
455
  const normalizeLocation = (loc) => {
529
- if (typeof loc === 'string') {
530
- // Parse path and query
531
- const [path, queryString] = loc.split('?');
532
- return {
533
- path,
534
- query: queryString ? parseQuery(queryString) : undefined,
535
- };
456
+ if (typeof loc !== 'string') {
457
+ return loc;
536
458
  }
537
- return loc;
459
+ const [path, queryString] = loc.split('?');
460
+ return {
461
+ path,
462
+ query: queryString ? parseQuery(queryString) : undefined,
463
+ };
538
464
  };
539
465
  // # register events
540
- if (mode === 'history') {
541
- // Listen to browser back/forward for history mode
542
- window.addEventListener('popstate', (event) => {
543
- if (event.state?.path) {
544
- // navigate without pushing new history entry
545
- navigate({ path: event.state.path, guardLevel: 1 /* GuardLevel.Global */, replace: true });
546
- }
547
- });
548
- }
549
- else {
550
- // hash mode: listen to hashchange
551
- window.addEventListener('hashchange', () => {
552
- const hash = window.location.hash.slice(1);
553
- const [path] = hash.split('?');
554
- const normalizedPath = normalizePath(path);
555
- if (current && current.path === normalizedPath) {
556
- return;
557
- }
558
- // render route for new hash without adding extra history entry
559
- const matched = match(normalizedPath);
560
- if (!matched) {
561
- onNotFound(normalizedPath);
562
- return;
563
- }
564
- const queryString = window.location.hash.slice(1).split('?')[1];
565
- const to = {
566
- path: normalizedPath,
567
- name: matched.route.name,
568
- params: matched.params,
569
- query: queryString ? parseQuery(queryString) : {},
570
- meta: matched.route.meta ?? {},
571
- matched: matched.result,
572
- };
573
- // apply without modifying browser history
574
- current = to;
575
- history.push(to);
576
- if (routerView && to.matched.length > 0) {
577
- const route = to.matched[to.matched.length - 1];
578
- if (route.component) {
579
- const element = route.component();
580
- if (element instanceof Promise) {
581
- element.then((el) => {
582
- routerView.innerHTML = '';
583
- routerView.appendChild(el);
584
- });
585
- }
586
- else {
466
+ // hash mode: listen to hashchange
467
+ window.addEventListener('hashchange', () => {
468
+ const hash = window.location.hash.slice(1);
469
+ const [path] = hash.split('?');
470
+ const normalizedPath = normalizePath(path);
471
+ if (current && current.path === normalizedPath) {
472
+ return;
473
+ }
474
+ // render route for new hash without adding extra history entry
475
+ const matched = match(normalizedPath);
476
+ if (!matched) {
477
+ onNotFound(normalizedPath);
478
+ return;
479
+ }
480
+ const queryString = window.location.hash.slice(1).split('?')[1];
481
+ const to = {
482
+ path: normalizedPath,
483
+ name: matched.route.name,
484
+ params: matched.params,
485
+ query: queryString ? parseQuery(queryString) : {},
486
+ meta: matched.route.meta ?? {},
487
+ matched: matched.result,
488
+ };
489
+ // apply without modifying browser history
490
+ current = to;
491
+ history.push(to);
492
+ if (routerView && to.matched.length > 0) {
493
+ const route = to.matched[to.matched.length - 1];
494
+ if (route.component) {
495
+ const element = route.component();
496
+ if (element instanceof Promise) {
497
+ element.then((el) => {
587
498
  routerView.innerHTML = '';
588
- routerView.appendChild(element);
589
- }
499
+ routerView.appendChild(el);
500
+ });
501
+ }
502
+ else {
503
+ routerView.innerHTML = '';
504
+ routerView.appendChild(element);
590
505
  }
591
506
  }
592
- executeAfterHooksSync(to, history[history.length - 2] ?? null);
593
- });
594
- }
507
+ }
508
+ executeAfterHooksSync(to, history[history.length - 2] ?? null);
509
+ });
510
+ // # initialize
511
+ normalize(config.routes, '/');
512
+ const { findByName, match } = createMatcher(routes);
595
513
  // Router instance
596
514
  return {
597
515
  get current() {
@@ -621,7 +539,6 @@ var __ktjs_router__ = (function (exports) {
621
539
  forward() {
622
540
  window.history.forward();
623
541
  },
624
- initCurrentRoute,
625
542
  };
626
543
  };
627
544
 
@@ -253,22 +253,20 @@ var __ktjs_router__ = (function (exports) {
253
253
  * Create a new router instance
254
254
  */
255
255
  var createRouter = function (config) {
256
- var _a, _b, _c, _d, _e, _f, _g;
256
+ var _a, _b, _c, _d, _e, _f;
257
257
  // # default configs
258
258
  var beforeEach = (_a = config.beforeEach) !== null && _a !== void 0 ? _a : defaultHook;
259
259
  var afterEach = (_b = config.afterEach) !== null && _b !== void 0 ? _b : defaultHook;
260
260
  var onNotFound = (_c = config.onNotFound) !== null && _c !== void 0 ? _c : defaultHook;
261
261
  var onError = (_d = config.onError) !== null && _d !== void 0 ? _d : defaultHook;
262
262
  var asyncGuards = (_e = config.asyncGuards) !== null && _e !== void 0 ? _e : true;
263
- // default to 'hash' mode
264
- var mode = (_f = config.mode) !== null && _f !== void 0 ? _f : 'hash';
265
- var baseUrl = (_g = config.baseUrl) !== null && _g !== void 0 ? _g : '';
263
+ var baseUrl = (_f = config.baseUrl) !== null && _f !== void 0 ? _f : '';
266
264
  // # private values
267
265
  var routes = [];
266
+ var history = [];
268
267
  var routerView = null;
269
- /**
270
- * Normalize routes by adding default guards
271
- */
268
+ var current = null;
269
+ // # methods
272
270
  var normalize = function (rawRoutes, parentPath) {
273
271
  return rawRoutes.map(function (route) {
274
272
  var _a, _b, _c, _d;
@@ -288,57 +286,6 @@ var __ktjs_router__ = (function (exports) {
288
286
  return normalized;
289
287
  });
290
288
  };
291
- // Normalize routes with default guards
292
- normalize(config.routes, '/');
293
- var _h = createMatcher(routes), findByName = _h.findByName, match = _h.match;
294
- /**
295
- * Initialize current route from URL
296
- */
297
- var initCurrentRoute = function () {
298
- var _a, _b;
299
- if (mode === 'hash') {
300
- var hash = window.location.hash.slice(1); // Remove '#'
301
- if (!hash) {
302
- return (current = null);
303
- }
304
- // Parse path and query
305
- var _c = hash.split('?'), path_1 = _c[0], queryString_1 = _c[1];
306
- var normalizedPath_1 = normalizePath(path_1);
307
- // Match route
308
- var matched_1 = match(normalizedPath_1);
309
- if (!matched_1) {
310
- return (current = null);
311
- }
312
- // Build route context
313
- return (current = {
314
- path: normalizedPath_1,
315
- name: matched_1.route.name,
316
- params: matched_1.params,
317
- query: queryString_1 ? parseQuery(queryString_1) : {},
318
- meta: (_a = matched_1.route.meta) !== null && _a !== void 0 ? _a : {},
319
- matched: matched_1.result,
320
- });
321
- }
322
- // history mode
323
- var pathWithQuery = window.location.pathname + window.location.search;
324
- var _d = pathWithQuery.split('?'), path = _d[0], queryString = _d[1];
325
- var normalizedPath = normalizePath(path);
326
- var matched = match(normalizedPath);
327
- if (!matched) {
328
- return (current = null);
329
- }
330
- return (current = {
331
- path: normalizedPath,
332
- name: matched.route.name,
333
- params: matched.params,
334
- query: queryString ? parseQuery(queryString) : {},
335
- meta: (_b = matched.route.meta) !== null && _b !== void 0 ? _b : {},
336
- matched: matched.result,
337
- });
338
- };
339
- var current = null;
340
- var history = current ? [current] : [];
341
- // # methods
342
289
  var executeGuardsSync = function (to, from, guardLevel) {
343
290
  try {
344
291
  if (guardLevel === 0 /* GuardLevel.None */) {
@@ -496,22 +443,12 @@ var __ktjs_router__ = (function (exports) {
496
443
  }
497
444
  // Update browser history depending on mode
498
445
  var url = fullPath;
499
- if (mode === 'history') {
500
- if (replace) {
501
- window.history.replaceState({ path: to.path }, '', url);
502
- }
503
- else {
504
- window.history.pushState({ path: to.path }, '', url);
505
- }
446
+ var hashUrl = '#' + fullPath;
447
+ if (replace) {
448
+ window.location.replace(hashUrl);
506
449
  }
507
450
  else {
508
- var hashUrl = '#' + fullPath;
509
- if (replace) {
510
- window.location.replace(hashUrl);
511
- }
512
- else {
513
- window.location.hash = fullPath;
514
- }
451
+ window.location.hash = fullPath;
515
452
  }
516
453
  // Update current route in memory
517
454
  current = to;
@@ -558,7 +495,7 @@ var __ktjs_router__ = (function (exports) {
558
495
  args_1[_i - 1] = arguments[_i];
559
496
  }
560
497
  return __awaiter(void 0, __spreadArray([options_1], args_1, true), void 0, function (options, redirectCount) {
561
- var prep, guardLevel, replace, to, fullPath, guardResult, url, hashUrl, route, element, error_2;
498
+ var prep, guardLevel, replace, to, fullPath, guardResult, hashUrl, route, element, error_2;
562
499
  var _a;
563
500
  if (redirectCount === void 0) { redirectCount = 0; }
564
501
  return __generator(this, function (_b) {
@@ -585,23 +522,12 @@ var __ktjs_router__ = (function (exports) {
585
522
  }
586
523
  return [2 /*return*/, false];
587
524
  }
588
- url = fullPath;
589
- if (mode === 'history') {
590
- if (replace) {
591
- window.history.replaceState({ path: to.path }, '', url);
592
- }
593
- else {
594
- window.history.pushState({ path: to.path }, '', url);
595
- }
525
+ hashUrl = '#' + fullPath;
526
+ if (replace) {
527
+ window.location.replace(hashUrl);
596
528
  }
597
529
  else {
598
- hashUrl = '#' + fullPath;
599
- if (replace) {
600
- window.location.replace(hashUrl);
601
- }
602
- else {
603
- window.location.hash = fullPath;
604
- }
530
+ window.location.hash = fullPath;
605
531
  }
606
532
  current = to;
607
533
  if (replace) {
@@ -662,74 +588,64 @@ var __ktjs_router__ = (function (exports) {
662
588
  * Normalize navigation argument
663
589
  */
664
590
  var normalizeLocation = function (loc) {
665
- if (typeof loc === 'string') {
666
- // Parse path and query
667
- var _a = loc.split('?'), path = _a[0], queryString = _a[1];
668
- return {
669
- path: path,
670
- query: queryString ? parseQuery(queryString) : undefined,
671
- };
591
+ if (typeof loc !== 'string') {
592
+ return loc;
672
593
  }
673
- return loc;
594
+ var _a = loc.split('?'), path = _a[0], queryString = _a[1];
595
+ return {
596
+ path: path,
597
+ query: queryString ? parseQuery(queryString) : undefined,
598
+ };
674
599
  };
675
600
  // # register events
676
- if (mode === 'history') {
677
- // Listen to browser back/forward for history mode
678
- window.addEventListener('popstate', function (event) {
679
- var _a;
680
- if ((_a = event.state) === null || _a === void 0 ? void 0 : _a.path) {
681
- // navigate without pushing new history entry
682
- navigate({ path: event.state.path, guardLevel: 1 /* GuardLevel.Global */, replace: true });
683
- }
684
- });
685
- }
686
- else {
687
- // hash mode: listen to hashchange
688
- window.addEventListener('hashchange', function () {
689
- var _a, _b;
690
- var hash = window.location.hash.slice(1);
691
- var path = hash.split('?')[0];
692
- var normalizedPath = normalizePath(path);
693
- if (current && current.path === normalizedPath) {
694
- return;
695
- }
696
- // render route for new hash without adding extra history entry
697
- var matched = match(normalizedPath);
698
- if (!matched) {
699
- onNotFound(normalizedPath);
700
- return;
701
- }
702
- var queryString = window.location.hash.slice(1).split('?')[1];
703
- var to = {
704
- path: normalizedPath,
705
- name: matched.route.name,
706
- params: matched.params,
707
- query: queryString ? parseQuery(queryString) : {},
708
- meta: (_a = matched.route.meta) !== null && _a !== void 0 ? _a : {},
709
- matched: matched.result,
710
- };
711
- // apply without modifying browser history
712
- current = to;
713
- history.push(to);
714
- if (routerView && to.matched.length > 0) {
715
- var route = to.matched[to.matched.length - 1];
716
- if (route.component) {
717
- var element = route.component();
718
- if (element instanceof Promise) {
719
- element.then(function (el) {
720
- routerView.innerHTML = '';
721
- routerView.appendChild(el);
722
- });
723
- }
724
- else {
601
+ // hash mode: listen to hashchange
602
+ window.addEventListener('hashchange', function () {
603
+ var _a, _b;
604
+ var hash = window.location.hash.slice(1);
605
+ var path = hash.split('?')[0];
606
+ var normalizedPath = normalizePath(path);
607
+ if (current && current.path === normalizedPath) {
608
+ return;
609
+ }
610
+ // render route for new hash without adding extra history entry
611
+ var matched = match(normalizedPath);
612
+ if (!matched) {
613
+ onNotFound(normalizedPath);
614
+ return;
615
+ }
616
+ var queryString = window.location.hash.slice(1).split('?')[1];
617
+ var to = {
618
+ path: normalizedPath,
619
+ name: matched.route.name,
620
+ params: matched.params,
621
+ query: queryString ? parseQuery(queryString) : {},
622
+ meta: (_a = matched.route.meta) !== null && _a !== void 0 ? _a : {},
623
+ matched: matched.result,
624
+ };
625
+ // apply without modifying browser history
626
+ current = to;
627
+ history.push(to);
628
+ if (routerView && to.matched.length > 0) {
629
+ var route = to.matched[to.matched.length - 1];
630
+ if (route.component) {
631
+ var element = route.component();
632
+ if (element instanceof Promise) {
633
+ element.then(function (el) {
725
634
  routerView.innerHTML = '';
726
- routerView.appendChild(element);
727
- }
635
+ routerView.appendChild(el);
636
+ });
637
+ }
638
+ else {
639
+ routerView.innerHTML = '';
640
+ routerView.appendChild(element);
728
641
  }
729
642
  }
730
- executeAfterHooksSync(to, (_b = history[history.length - 2]) !== null && _b !== void 0 ? _b : null);
731
- });
732
- }
643
+ }
644
+ executeAfterHooksSync(to, (_b = history[history.length - 2]) !== null && _b !== void 0 ? _b : null);
645
+ });
646
+ // # initialize
647
+ normalize(config.routes, '/');
648
+ var _g = createMatcher(routes), findByName = _g.findByName, match = _g.match;
733
649
  // Router instance
734
650
  return {
735
651
  get current() {
@@ -759,7 +675,6 @@ var __ktjs_router__ = (function (exports) {
759
675
  forward: function () {
760
676
  window.history.forward();
761
677
  },
762
- initCurrentRoute: initCurrentRoute,
763
678
  };
764
679
  };
765
680
 
package/dist/index.mjs CHANGED
@@ -166,15 +166,13 @@ const createRouter = (config) => {
166
166
  const onNotFound = config.onNotFound ?? defaultHook;
167
167
  const onError = config.onError ?? defaultHook;
168
168
  const asyncGuards = config.asyncGuards ?? true;
169
- // default to 'hash' mode
170
- const mode = config.mode ?? 'hash';
171
169
  const baseUrl = config.baseUrl ?? '';
172
170
  // # private values
173
171
  const routes = [];
172
+ const history = [];
174
173
  let routerView = null;
175
- /**
176
- * Normalize routes by adding default guards
177
- */
174
+ let current = null;
175
+ // # methods
178
176
  const normalize = (rawRoutes, parentPath) => rawRoutes.map((route) => {
179
177
  const path = normalizePath(parentPath, route.path);
180
178
  const normalized = {
@@ -191,56 +189,6 @@ const createRouter = (config) => {
191
189
  routes.push(normalized);
192
190
  return normalized;
193
191
  });
194
- // Normalize routes with default guards
195
- normalize(config.routes, '/');
196
- const { findByName, match } = createMatcher(routes);
197
- /**
198
- * Initialize current route from URL
199
- */
200
- const initCurrentRoute = () => {
201
- if (mode === 'hash') {
202
- const hash = window.location.hash.slice(1); // Remove '#'
203
- if (!hash) {
204
- return (current = null);
205
- }
206
- // Parse path and query
207
- const [path, queryString] = hash.split('?');
208
- const normalizedPath = normalizePath(path);
209
- // Match route
210
- const matched = match(normalizedPath);
211
- if (!matched) {
212
- return (current = null);
213
- }
214
- // Build route context
215
- return (current = {
216
- path: normalizedPath,
217
- name: matched.route.name,
218
- params: matched.params,
219
- query: queryString ? parseQuery(queryString) : {},
220
- meta: matched.route.meta ?? {},
221
- matched: matched.result,
222
- });
223
- }
224
- // history mode
225
- const pathWithQuery = window.location.pathname + window.location.search;
226
- const [path, queryString] = pathWithQuery.split('?');
227
- const normalizedPath = normalizePath(path);
228
- const matched = match(normalizedPath);
229
- if (!matched) {
230
- return (current = null);
231
- }
232
- return (current = {
233
- path: normalizedPath,
234
- name: matched.route.name,
235
- params: matched.params,
236
- query: queryString ? parseQuery(queryString) : {},
237
- meta: matched.route.meta ?? {},
238
- matched: matched.result,
239
- });
240
- };
241
- let current = null;
242
- const history = current ? [current] : [];
243
- // # methods
244
192
  const executeGuardsSync = (to, from, guardLevel) => {
245
193
  try {
246
194
  if (guardLevel === 0 /* GuardLevel.None */) {
@@ -384,22 +332,12 @@ const createRouter = (config) => {
384
332
  }
385
333
  // Update browser history depending on mode
386
334
  const url = fullPath;
387
- if (mode === 'history') {
388
- if (replace) {
389
- window.history.replaceState({ path: to.path }, '', url);
390
- }
391
- else {
392
- window.history.pushState({ path: to.path }, '', url);
393
- }
335
+ const hashUrl = '#' + fullPath;
336
+ if (replace) {
337
+ window.location.replace(hashUrl);
394
338
  }
395
339
  else {
396
- const hashUrl = '#' + fullPath;
397
- if (replace) {
398
- window.location.replace(hashUrl);
399
- }
400
- else {
401
- window.location.hash = fullPath;
402
- }
340
+ window.location.hash = fullPath;
403
341
  }
404
342
  // Update current route in memory
405
343
  current = to;
@@ -461,23 +399,12 @@ const createRouter = (config) => {
461
399
  return false;
462
400
  }
463
401
  // ---- Guards passed ----
464
- const url = fullPath;
465
- if (mode === 'history') {
466
- if (replace) {
467
- window.history.replaceState({ path: to.path }, '', url);
468
- }
469
- else {
470
- window.history.pushState({ path: to.path }, '', url);
471
- }
402
+ const hashUrl = '#' + fullPath;
403
+ if (replace) {
404
+ window.location.replace(hashUrl);
472
405
  }
473
406
  else {
474
- const hashUrl = '#' + fullPath;
475
- if (replace) {
476
- window.location.replace(hashUrl);
477
- }
478
- else {
479
- window.location.hash = fullPath;
480
- }
407
+ window.location.hash = fullPath;
481
408
  }
482
409
  current = to;
483
410
  if (replace) {
@@ -523,72 +450,63 @@ const createRouter = (config) => {
523
450
  * Normalize navigation argument
524
451
  */
525
452
  const normalizeLocation = (loc) => {
526
- if (typeof loc === 'string') {
527
- // Parse path and query
528
- const [path, queryString] = loc.split('?');
529
- return {
530
- path,
531
- query: queryString ? parseQuery(queryString) : undefined,
532
- };
453
+ if (typeof loc !== 'string') {
454
+ return loc;
533
455
  }
534
- return loc;
456
+ const [path, queryString] = loc.split('?');
457
+ return {
458
+ path,
459
+ query: queryString ? parseQuery(queryString) : undefined,
460
+ };
535
461
  };
536
462
  // # register events
537
- if (mode === 'history') {
538
- // Listen to browser back/forward for history mode
539
- window.addEventListener('popstate', (event) => {
540
- if (event.state?.path) {
541
- // navigate without pushing new history entry
542
- navigate({ path: event.state.path, guardLevel: 1 /* GuardLevel.Global */, replace: true });
543
- }
544
- });
545
- }
546
- else {
547
- // hash mode: listen to hashchange
548
- window.addEventListener('hashchange', () => {
549
- const hash = window.location.hash.slice(1);
550
- const [path] = hash.split('?');
551
- const normalizedPath = normalizePath(path);
552
- if (current && current.path === normalizedPath) {
553
- return;
554
- }
555
- // render route for new hash without adding extra history entry
556
- const matched = match(normalizedPath);
557
- if (!matched) {
558
- onNotFound(normalizedPath);
559
- return;
560
- }
561
- const queryString = window.location.hash.slice(1).split('?')[1];
562
- const to = {
563
- path: normalizedPath,
564
- name: matched.route.name,
565
- params: matched.params,
566
- query: queryString ? parseQuery(queryString) : {},
567
- meta: matched.route.meta ?? {},
568
- matched: matched.result,
569
- };
570
- // apply without modifying browser history
571
- current = to;
572
- history.push(to);
573
- if (routerView && to.matched.length > 0) {
574
- const route = to.matched[to.matched.length - 1];
575
- if (route.component) {
576
- const element = route.component();
577
- if (element instanceof Promise) {
578
- element.then((el) => {
579
- routerView.innerHTML = '';
580
- routerView.appendChild(el);
581
- });
582
- }
583
- else {
463
+ // hash mode: listen to hashchange
464
+ window.addEventListener('hashchange', () => {
465
+ const hash = window.location.hash.slice(1);
466
+ const [path] = hash.split('?');
467
+ const normalizedPath = normalizePath(path);
468
+ if (current && current.path === normalizedPath) {
469
+ return;
470
+ }
471
+ // render route for new hash without adding extra history entry
472
+ const matched = match(normalizedPath);
473
+ if (!matched) {
474
+ onNotFound(normalizedPath);
475
+ return;
476
+ }
477
+ const queryString = window.location.hash.slice(1).split('?')[1];
478
+ const to = {
479
+ path: normalizedPath,
480
+ name: matched.route.name,
481
+ params: matched.params,
482
+ query: queryString ? parseQuery(queryString) : {},
483
+ meta: matched.route.meta ?? {},
484
+ matched: matched.result,
485
+ };
486
+ // apply without modifying browser history
487
+ current = to;
488
+ history.push(to);
489
+ if (routerView && to.matched.length > 0) {
490
+ const route = to.matched[to.matched.length - 1];
491
+ if (route.component) {
492
+ const element = route.component();
493
+ if (element instanceof Promise) {
494
+ element.then((el) => {
584
495
  routerView.innerHTML = '';
585
- routerView.appendChild(element);
586
- }
496
+ routerView.appendChild(el);
497
+ });
498
+ }
499
+ else {
500
+ routerView.innerHTML = '';
501
+ routerView.appendChild(element);
587
502
  }
588
503
  }
589
- executeAfterHooksSync(to, history[history.length - 2] ?? null);
590
- });
591
- }
504
+ }
505
+ executeAfterHooksSync(to, history[history.length - 2] ?? null);
506
+ });
507
+ // # initialize
508
+ normalize(config.routes, '/');
509
+ const { findByName, match } = createMatcher(routes);
592
510
  // Router instance
593
511
  return {
594
512
  get current() {
@@ -618,7 +536,6 @@ const createRouter = (config) => {
618
536
  forward() {
619
537
  window.history.forward();
620
538
  },
621
- initCurrentRoute,
622
539
  };
623
540
  };
624
541
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ktjs/router",
3
- "version": "0.14.5",
3
+ "version": "0.14.7",
4
4
  "description": "Router for kt.js - client-side routing with navigation guards",
5
5
  "type": "module",
6
6
  "module": "./dist/index.mjs",