@gusnips/react 0.5.0 → 0.5.1
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/package.json +2 -2
- package/dist/chunk-reload.d.ts +0 -61
- package/dist/chunk-reload.d.ts.map +0 -1
- package/dist/chunk-reload.js +0 -92
- package/dist/chunk-reload.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gusnips/react",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "The headless runtime under a Vite + React SPA: typed fetch client with single-flight refresh, auth store, query defaults, route guards, error boundary, stale-tab recovery, i18n bootstrap.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"headless"
|
|
61
61
|
],
|
|
62
62
|
"scripts": {
|
|
63
|
-
"build": "tsc",
|
|
63
|
+
"build": "rm -rf dist && tsc",
|
|
64
64
|
"typecheck": "tsc --noEmit",
|
|
65
65
|
"test": "vitest run --passWithNoTests",
|
|
66
66
|
"test:watch": "vitest",
|
package/dist/chunk-reload.d.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Surviving a deploy that lands while a tab is open.
|
|
3
|
-
*
|
|
4
|
-
* Every app on this stack is a Vite SPA with `lazy()` routes, deployed on push. When a build
|
|
5
|
-
* replaces the one a tab is running, the next navigation asks for a chunk whose hashed name no
|
|
6
|
-
* longer exists — and a static host answers a missing asset with the SPA fallback, so the
|
|
7
|
-
* import gets HTML where it expected a module. React's lazy boundary throws, and the person
|
|
8
|
-
* gets a white screen on a click that worked a minute ago.
|
|
9
|
-
*
|
|
10
|
-
* One repo in the fleet had all of this. The others had none of it, and none of them knew.
|
|
11
|
-
*/
|
|
12
|
-
/**
|
|
13
|
-
* A lazy-chunk fetch failure — recoverable by reloading.
|
|
14
|
-
*
|
|
15
|
-
* Matched by message because no browser gives it a shared type. Every phrasing in the wild is
|
|
16
|
-
* here: Chrome/Edge "Failed to fetch dynamically imported module", Firefox "error loading
|
|
17
|
-
* dynamically imported module", Safari "Importing a module script failed", webpack's
|
|
18
|
-
* ChunkLoadError and "Loading chunk N failed", and the SPA-fallback "MIME type" refusal —
|
|
19
|
-
* which is the one that actually fires on a static host, because the missing `.js` is answered
|
|
20
|
-
* with `index.html`.
|
|
21
|
-
*
|
|
22
|
-
* A standalone predicate rather than a method on the boundary, so a global `unhandledrejection`
|
|
23
|
-
* handler can classify the same failure without importing a component.
|
|
24
|
-
*/
|
|
25
|
-
export declare function isChunkLoadError(error: unknown): boolean;
|
|
26
|
-
/**
|
|
27
|
-
* Hard-reload once to pick up the fresh deploy. Returns false when the guard suppressed it.
|
|
28
|
-
*
|
|
29
|
-
* The guard is the part that matters. Reloading on a chunk error is obvious; reloading on a
|
|
30
|
-
* chunk error that the reload does not fix is an infinite loop with the user inside it. At most
|
|
31
|
-
* one reload per minute, so a genuinely missing chunk degrades to the error UI — which can at
|
|
32
|
-
* least say something — instead of flickering forever.
|
|
33
|
-
*
|
|
34
|
-
* `sessionStorage` and not `localStorage`: the guard is about this tab's current predicament,
|
|
35
|
-
* and a stale flag in another tab must not suppress a reload this one needs. Wrapped because
|
|
36
|
-
* storage throws outright when site data is blocked, and a privacy setting must not be the
|
|
37
|
-
* reason somebody cannot recover.
|
|
38
|
-
*/
|
|
39
|
-
export declare function reloadOnceForChunkError(): boolean;
|
|
40
|
-
/**
|
|
41
|
-
* Vite's *hint* failure, which is not the same thing and must not be treated as one.
|
|
42
|
-
*
|
|
43
|
-
* Vite injects a speculative `<link rel="stylesheet">` for a chunk's CSS before importing it.
|
|
44
|
-
* When that link fails, the import itself still runs right after and usually succeeds — so this
|
|
45
|
-
* one is worth swallowing, where a failure of the import is the real thing
|
|
46
|
-
* {@link isChunkLoadError} matches. Vite builds this message in exactly one place and only ever
|
|
47
|
-
* for a stylesheet link; script hints never reject.
|
|
48
|
-
*/
|
|
49
|
-
export declare function isPreloadHintFailure(error: unknown): boolean;
|
|
50
|
-
/**
|
|
51
|
-
* Install the `vite:preloadError` listener. Call once, from the browser entry.
|
|
52
|
-
*
|
|
53
|
-
* Vite offers `preventDefault()` here so an app can own a failed asset itself — but taking that
|
|
54
|
-
* deal unconditionally resolves the dynamic import with `undefined` (Vite's helper ends in
|
|
55
|
-
* `baseModule().catch(handlePreloadError)`), and `React.lazy` then reads `.default` off nothing.
|
|
56
|
-
* The user gets the crash screen and the developer gets a TypeError with only React frames in
|
|
57
|
-
* it, naming no chunk. So it is taken ONLY for a failed preload hint, where the import still
|
|
58
|
-
* runs. A real module failure is left to throw, where the error boundary can recognise it.
|
|
59
|
-
*/
|
|
60
|
-
export declare function installPreloadErrorHandler(): void;
|
|
61
|
-
//# sourceMappingURL=chunk-reload.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"chunk-reload.d.ts","sourceRoot":"","sources":["../src/chunk-reload.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAKH;;;;;;;;;;;;GAYG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CASxD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,uBAAuB,IAAI,OAAO,CAWjD;AAED;;;;;;;;GAQG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAE5D;AAED;;;;;;;;;GASG;AACH,wBAAgB,0BAA0B,IAAI,IAAI,CAKjD"}
|
package/dist/chunk-reload.js
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Surviving a deploy that lands while a tab is open.
|
|
3
|
-
*
|
|
4
|
-
* Every app on this stack is a Vite SPA with `lazy()` routes, deployed on push. When a build
|
|
5
|
-
* replaces the one a tab is running, the next navigation asks for a chunk whose hashed name no
|
|
6
|
-
* longer exists — and a static host answers a missing asset with the SPA fallback, so the
|
|
7
|
-
* import gets HTML where it expected a module. React's lazy boundary throws, and the person
|
|
8
|
-
* gets a white screen on a click that worked a minute ago.
|
|
9
|
-
*
|
|
10
|
-
* One repo in the fleet had all of this. The others had none of it, and none of them knew.
|
|
11
|
-
*/
|
|
12
|
-
const RELOAD_FLAG_KEY = "frontkit:chunk-reload-at";
|
|
13
|
-
const RELOAD_WINDOW_MS = 60_000;
|
|
14
|
-
/**
|
|
15
|
-
* A lazy-chunk fetch failure — recoverable by reloading.
|
|
16
|
-
*
|
|
17
|
-
* Matched by message because no browser gives it a shared type. Every phrasing in the wild is
|
|
18
|
-
* here: Chrome/Edge "Failed to fetch dynamically imported module", Firefox "error loading
|
|
19
|
-
* dynamically imported module", Safari "Importing a module script failed", webpack's
|
|
20
|
-
* ChunkLoadError and "Loading chunk N failed", and the SPA-fallback "MIME type" refusal —
|
|
21
|
-
* which is the one that actually fires on a static host, because the missing `.js` is answered
|
|
22
|
-
* with `index.html`.
|
|
23
|
-
*
|
|
24
|
-
* A standalone predicate rather than a method on the boundary, so a global `unhandledrejection`
|
|
25
|
-
* handler can classify the same failure without importing a component.
|
|
26
|
-
*/
|
|
27
|
-
export function isChunkLoadError(error) {
|
|
28
|
-
if (error instanceof Error && error.name === "ChunkLoadError")
|
|
29
|
-
return true;
|
|
30
|
-
const message = (error instanceof Error ? error.message : String(error) || "").toLowerCase();
|
|
31
|
-
return (message.includes("dynamically imported module") ||
|
|
32
|
-
message.includes("importing a module script failed") ||
|
|
33
|
-
message.includes("mime type") ||
|
|
34
|
-
message.includes("loading chunk"));
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Hard-reload once to pick up the fresh deploy. Returns false when the guard suppressed it.
|
|
38
|
-
*
|
|
39
|
-
* The guard is the part that matters. Reloading on a chunk error is obvious; reloading on a
|
|
40
|
-
* chunk error that the reload does not fix is an infinite loop with the user inside it. At most
|
|
41
|
-
* one reload per minute, so a genuinely missing chunk degrades to the error UI — which can at
|
|
42
|
-
* least say something — instead of flickering forever.
|
|
43
|
-
*
|
|
44
|
-
* `sessionStorage` and not `localStorage`: the guard is about this tab's current predicament,
|
|
45
|
-
* and a stale flag in another tab must not suppress a reload this one needs. Wrapped because
|
|
46
|
-
* storage throws outright when site data is blocked, and a privacy setting must not be the
|
|
47
|
-
* reason somebody cannot recover.
|
|
48
|
-
*/
|
|
49
|
-
export function reloadOnceForChunkError() {
|
|
50
|
-
const now = Date.now();
|
|
51
|
-
try {
|
|
52
|
-
const last = Number(sessionStorage.getItem(RELOAD_FLAG_KEY) ?? 0);
|
|
53
|
-
if (now - last < RELOAD_WINDOW_MS)
|
|
54
|
-
return false;
|
|
55
|
-
sessionStorage.setItem(RELOAD_FLAG_KEY, String(now));
|
|
56
|
-
}
|
|
57
|
-
catch {
|
|
58
|
-
// Storage blocked — proceed with the reload rather than refusing to recover.
|
|
59
|
-
}
|
|
60
|
-
window.location.reload();
|
|
61
|
-
return true;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Vite's *hint* failure, which is not the same thing and must not be treated as one.
|
|
65
|
-
*
|
|
66
|
-
* Vite injects a speculative `<link rel="stylesheet">` for a chunk's CSS before importing it.
|
|
67
|
-
* When that link fails, the import itself still runs right after and usually succeeds — so this
|
|
68
|
-
* one is worth swallowing, where a failure of the import is the real thing
|
|
69
|
-
* {@link isChunkLoadError} matches. Vite builds this message in exactly one place and only ever
|
|
70
|
-
* for a stylesheet link; script hints never reject.
|
|
71
|
-
*/
|
|
72
|
-
export function isPreloadHintFailure(error) {
|
|
73
|
-
return error instanceof Error && error.message.startsWith("Unable to preload CSS for");
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Install the `vite:preloadError` listener. Call once, from the browser entry.
|
|
77
|
-
*
|
|
78
|
-
* Vite offers `preventDefault()` here so an app can own a failed asset itself — but taking that
|
|
79
|
-
* deal unconditionally resolves the dynamic import with `undefined` (Vite's helper ends in
|
|
80
|
-
* `baseModule().catch(handlePreloadError)`), and `React.lazy` then reads `.default` off nothing.
|
|
81
|
-
* The user gets the crash screen and the developer gets a TypeError with only React frames in
|
|
82
|
-
* it, naming no chunk. So it is taken ONLY for a failed preload hint, where the import still
|
|
83
|
-
* runs. A real module failure is left to throw, where the error boundary can recognise it.
|
|
84
|
-
*/
|
|
85
|
-
export function installPreloadErrorHandler() {
|
|
86
|
-
window.addEventListener("vite:preloadError", (event) => {
|
|
87
|
-
const payload = event.payload;
|
|
88
|
-
if (isPreloadHintFailure(payload))
|
|
89
|
-
event.preventDefault();
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
//# sourceMappingURL=chunk-reload.js.map
|
package/dist/chunk-reload.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"chunk-reload.js","sourceRoot":"","sources":["../src/chunk-reload.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,MAAM,eAAe,GAAG,0BAA0B,CAAC;AACnD,MAAM,gBAAgB,GAAG,MAAM,CAAC;AAEhC;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAc;IAC7C,IAAI,KAAK,YAAY,KAAK,IAAI,KAAK,CAAC,IAAI,KAAK,gBAAgB;QAAE,OAAO,IAAI,CAAC;IAC3E,MAAM,OAAO,GAAG,CAAC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC;IAC7F,OAAO,CACL,OAAO,CAAC,QAAQ,CAAC,6BAA6B,CAAC;QAC/C,OAAO,CAAC,QAAQ,CAAC,kCAAkC,CAAC;QACpD,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC;QAC7B,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAC,CAClC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,u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