fix(dev): server stalls when clicking quickly between pages (#1146)
Close #1079
This commit is contained in:
+14
-3
@@ -423,9 +423,7 @@ export class ServerContext {
|
|||||||
if (this.#dev) {
|
if (this.#dev) {
|
||||||
internalRoutes[REFRESH_JS_URL] = {
|
internalRoutes[REFRESH_JS_URL] = {
|
||||||
default: () => {
|
default: () => {
|
||||||
const js =
|
return new Response(refreshJs(ALIVE_URL, BUILD_ID), {
|
||||||
`new EventSource("${ALIVE_URL}").addEventListener("message", function listener(e) { if (e.data !== "${BUILD_ID}") { this.removeEventListener('message', listener); location.reload(); } });`;
|
|
||||||
return new Response(js, {
|
|
||||||
headers: {
|
headers: {
|
||||||
"content-type": "application/javascript; charset=utf-8",
|
"content-type": "application/javascript; charset=utf-8",
|
||||||
},
|
},
|
||||||
@@ -825,3 +823,16 @@ export function middlewarePathToPattern(baseRoute: string) {
|
|||||||
const compiledPattern = new URLPattern({ pathname: pattern });
|
const compiledPattern = new URLPattern({ pathname: pattern });
|
||||||
return { pattern, compiledPattern };
|
return { pattern, compiledPattern };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function refreshJs(aliveUrl: string, buildId: string) {
|
||||||
|
return `let es = new EventSource("${aliveUrl}");
|
||||||
|
window.addEventListener("beforeunload", (event) => {
|
||||||
|
es.close();
|
||||||
|
});
|
||||||
|
es.addEventListener("message", function listener(e) {
|
||||||
|
if (e.data !== "${buildId}") {
|
||||||
|
this.removeEventListener("message", listener);
|
||||||
|
location.reload();
|
||||||
|
}
|
||||||
|
});`;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user