fix(dev): server stalls when clicking quickly between pages (#1146)

Close #1079
This commit is contained in:
hashrock
2023-05-03 21:16:50 +02:00
committed by GitHub
parent f481c1a03f
commit 2d124aacf4
+14 -3
View File
@@ -423,9 +423,7 @@ export class ServerContext {
if (this.#dev) {
internalRoutes[REFRESH_JS_URL] = {
default: () => {
const js =
`new EventSource("${ALIVE_URL}").addEventListener("message", function listener(e) { if (e.data !== "${BUILD_ID}") { this.removeEventListener('message', listener); location.reload(); } });`;
return new Response(js, {
return new Response(refreshJs(ALIVE_URL, BUILD_ID), {
headers: {
"content-type": "application/javascript; charset=utf-8",
},
@@ -825,3 +823,16 @@ export function middlewarePathToPattern(baseRoute: string) {
const compiledPattern = new URLPattern({ pathname: pattern });
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();
}
});`;
}