@ecopages/browser-router 0.2.0-alpha.1 → 0.2.0-alpha.3
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/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.
|
|
3
|
+
"version": "0.2.0-alpha.3",
|
|
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.
|
|
35
|
+
"@ecopages/core": "0.2.0-alpha.3",
|
|
36
36
|
"morphdom": "^2.7.8"
|
|
37
37
|
},
|
|
38
38
|
"types": "./src/index.d.ts"
|
package/src/client/eco-router.js
CHANGED
|
@@ -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.
|
package/src/client/eco-router.ts
CHANGED
|
@@ -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
|
*
|