@ecopages/browser-router 0.2.0-alpha.2 → 0.2.0-alpha.4

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.
Files changed (45) hide show
  1. package/CHANGELOG.md +1 -0
  2. package/package.json +2 -2
  3. package/src/client/eco-router.js +10 -0
  4. package/src/client/eco-router.ts +20 -0
  5. package/src/client/services/prefetch-manager.d.ts +1 -0
  6. package/src/client/services/prefetch-manager.js +5 -0
  7. package/src/client/services/prefetch-manager.ts +6 -0
  8. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Initialization-should-create-router-instance-1.png +0 -0
  9. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Initialization-should-start-and-stop-without-errors-1.png +0 -0
  10. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Lifecycle-Events-should-dispatch-eco-before-swap--eco-after-swap--and-eco-page-load-events-1.png +0 -0
  11. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Lifecycle-Events-should-dispatch-eco-page-load-event-after-animation-frame-1.png +0 -0
  12. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Lifecycle-Events-should-provide-event-details-with-url-and-direction-1.png +0 -0
  13. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Custom-Link-Selector-should-work-with-data-attribute-selector-1.png +0 -0
  14. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Custom-Reload-Attribute-should-intercept-links-with-default-reload-attribute-when-custom-is-set-1.png +0 -0
  15. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Custom-link-selector-should-only-intercept-links-matching-custom-selector-1.png +0 -0
  16. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-External-Links--should-NOT-intercept--should-NOT-intercept-external-links-1.png +0 -0
  17. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Internal-Links-should-intercept-clicks-on-relative-path-links-1.png +0 -0
  18. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Internal-Links-should-intercept-clicks-on-same-origin-absolute-URLs-1.png +0 -0
  19. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Link-Attributes--should-NOT-intercept--should-NOT-intercept-links-with-download-attribute-1.png +0 -0
  20. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Link-Attributes--should-NOT-intercept--should-intercept-links-with-target---self--1.png +0 -0
  21. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Modifier-keys-should-NOT-intercept-alt-click-1.png +0 -0
  22. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Modifier-keys-should-NOT-intercept-ctrl-click-1.png +0 -0
  23. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Modifier-keys-should-NOT-intercept-meta-click--cmd-on-Mac--1.png +0 -0
  24. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Modifier-keys-should-NOT-intercept-middle-mouse-button-click-1.png +0 -0
  25. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Modifier-keys-should-NOT-intercept-right-mouse-button-click-1.png +0 -0
  26. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Modifier-keys-should-NOT-intercept-shift-click-1.png +0 -0
  27. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Should-NOT-intercept-external-links--different-origin--1.png +0 -0
  28. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Should-NOT-intercept-hash-only-links-1.png +0 -0
  29. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Should-NOT-intercept-javascript--links-1.png +0 -0
  30. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Should-NOT-intercept-links-with-custom-reload-attribute-1.png +0 -0
  31. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Should-NOT-intercept-links-with-data-eco-reload-attribute-1.png +0 -0
  32. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Should-NOT-intercept-links-with-download-attribute-1.png +0 -0
  33. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Should-NOT-intercept-links-with-empty-href-1.png +0 -0
  34. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Should-NOT-intercept-links-with-target---blank--1.png +0 -0
  35. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Should-NOT-intercept-links-with-target---parent--1.png +0 -0
  36. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Should-NOT-intercept-links-without-href-attribute-1.png +0 -0
  37. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Should-intercept-internal-links-with-absolute-same-origin-paths-1.png +0 -0
  38. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Should-intercept-internal-links-with-relative-paths-1.png +0 -0
  39. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Should-intercept-nested-elements-inside-links-1.png +0 -0
  40. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-should-NOT-intercept-external-links-1.png +0 -0
  41. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Navigation-Abort-should-abort-previous-navigation-when-new-one-starts-1.png +0 -0
  42. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Programmatic-Navigation-should-navigate-and-update-history-with-pushState-1.png +0 -0
  43. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Programmatic-Navigation-should-use-replaceState-when-replace-option-is-true-1.png +0 -0
  44. package/src/client/__screenshots__/eco-router.test.ts/EcoRouter-Error-Handling-should-fall-back-to-full-page-navigation-on-fetch-error-1.png +0 -0
  45. package/src/client/__screenshots__/eco-router.test.ts/EcoRouter-Error-Handling-should-log-error-and-attempt-fallback-navigation-on-fetch-error-1.png +0 -0
package/CHANGELOG.md CHANGED
@@ -15,6 +15,7 @@ All notable changes to `@ecopages/browser-router` are documented here.
15
15
  ### Bug Fixes
16
16
 
17
17
  - Published npm package metadata now includes validated declaration exports for generated dist entrypoints.
18
+ - Registered a current-page HMR refresh hook that invalidates cached HTML before re-fetching the active route.
18
19
 
19
20
  ### Refactoring
20
21
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ecopages/browser-router",
3
- "version": "0.2.0-alpha.2",
3
+ "version": "0.2.0-alpha.4",
4
4
  "description": "Client-side router for Ecopages with view transitions support",
5
5
  "keywords": [
6
6
  "ecopages",
@@ -32,7 +32,7 @@
32
32
  "directory": "packages/browser-router"
33
33
  },
34
34
  "dependencies": {
35
- "@ecopages/core": "0.2.0-alpha.2",
35
+ "@ecopages/core": "0.2.0-alpha.4",
36
36
  "morphdom": "^2.7.8"
37
37
  },
38
38
  "types": "./src/index.d.ts"
@@ -31,6 +31,14 @@ class EcoRouter {
31
31
  document.addEventListener("click", this.handleClick);
32
32
  window.addEventListener("popstate", this.handlePopState);
33
33
  this.prefetchManager?.start();
34
+ const windowWithHmr = window;
35
+ windowWithHmr.__ecopages_reload_current_page__ = async (options) => {
36
+ const currentUrl = window.location.pathname + window.location.search;
37
+ if (options.clearCache) {
38
+ this.prefetchManager?.invalidate(currentUrl);
39
+ }
40
+ await this.performNavigation(new URL(currentUrl, window.location.origin), "replace");
41
+ };
34
42
  const initialHtml = document.documentElement.outerHTML;
35
43
  this.prefetchManager?.cacheVisitedPage(window.location.href, initialHtml);
36
44
  }
@@ -42,6 +50,8 @@ class EcoRouter {
42
50
  document.removeEventListener("click", this.handleClick);
43
51
  window.removeEventListener("popstate", this.handlePopState);
44
52
  this.prefetchManager?.stop();
53
+ const windowWithHmr = window;
54
+ windowWithHmr.__ecopages_reload_current_page__ = void 0;
45
55
  }
46
56
  /**
47
57
  * Programmatic navigation.
@@ -49,6 +49,20 @@ export class EcoRouter {
49
49
  window.addEventListener('popstate', this.handlePopState);
50
50
  this.prefetchManager?.start();
51
51
 
52
+ const windowWithHmr = window as typeof window & {
53
+ __ecopages_reload_current_page__?: (options: { clearCache: boolean }) => Promise<void>;
54
+ };
55
+
56
+ windowWithHmr.__ecopages_reload_current_page__ = async (options: { clearCache: boolean }) => {
57
+ const currentUrl = window.location.pathname + window.location.search;
58
+
59
+ if (options.clearCache) {
60
+ this.prefetchManager?.invalidate(currentUrl);
61
+ }
62
+
63
+ await this.performNavigation(new URL(currentUrl, window.location.origin), 'replace');
64
+ };
65
+
52
66
  // Cache the initial page for instant back-navigation
53
67
  const initialHtml = document.documentElement.outerHTML;
54
68
  this.prefetchManager?.cacheVisitedPage(window.location.href, initialHtml);
@@ -62,6 +76,12 @@ export class EcoRouter {
62
76
  document.removeEventListener('click', this.handleClick);
63
77
  window.removeEventListener('popstate', this.handlePopState);
64
78
  this.prefetchManager?.stop();
79
+
80
+ const windowWithHmr = window as typeof window & {
81
+ __ecopages_reload_current_page__?: (options: { clearCache: boolean }) => Promise<void>;
82
+ };
83
+
84
+ windowWithHmr.__ecopages_reload_current_page__ = undefined;
65
85
  }
66
86
 
67
87
  /**
@@ -52,6 +52,7 @@ export declare class PrefetchManager {
52
52
  * @returns The cached HTML string, or null if not cached
53
53
  */
54
54
  getCachedHtml(href: string): string | null;
55
+ invalidate(href: string): void;
55
56
  /**
56
57
  * Caches HTML content for a visited page and triggers background revalidation.
57
58
  *
@@ -89,6 +89,11 @@ class PrefetchManager {
89
89
  const url = new URL(href, window.location.origin);
90
90
  return this.htmlCache.get(url.href) ?? null;
91
91
  }
92
+ invalidate(href) {
93
+ const url = new URL(href, window.location.origin);
94
+ this.htmlCache.delete(url.href);
95
+ this.prefetched.delete(url.href);
96
+ }
92
97
  /**
93
98
  * Caches HTML content for a visited page and triggers background revalidation.
94
99
  *
@@ -122,6 +122,12 @@ export class PrefetchManager {
122
122
  return this.htmlCache.get(url.href) ?? null;
123
123
  }
124
124
 
125
+ invalidate(href: string): void {
126
+ const url = new URL(href, window.location.origin);
127
+ this.htmlCache.delete(url.href);
128
+ this.prefetched.delete(url.href);
129
+ }
130
+
125
131
  /**
126
132
  * Caches HTML content for a visited page and triggers background revalidation.
127
133
  *