feat: add auto reload on restart (#70)
This commit adds support for auto reloading open pages when fresh restarts. This is particularly useful when `deno run` is used with `--watch`.
This commit is contained in:
@@ -1,4 +1,6 @@
|
|||||||
export const INTERNAL_PREFIX = "/_frsh";
|
export const INTERNAL_PREFIX = "/_frsh";
|
||||||
|
export const REFRESH_JS_URL = `${INTERNAL_PREFIX}/refresh.js`;
|
||||||
|
export const ALIVE_URL = `${INTERNAL_PREFIX}/alive`;
|
||||||
export const BUILD_ID = Deno.env.get("DENO_DEPLOYMENT_ID") ||
|
export const BUILD_ID = Deno.env.get("DENO_DEPLOYMENT_ID") ||
|
||||||
crypto.randomUUID();
|
crypto.randomUUID();
|
||||||
export const JS_PREFIX = `/js`;
|
export const JS_PREFIX = `/js`;
|
||||||
|
|||||||
+39
-1
@@ -8,7 +8,7 @@ import {
|
|||||||
} from "./deps.ts";
|
} from "./deps.ts";
|
||||||
import { Routes } from "./mod.ts";
|
import { Routes } from "./mod.ts";
|
||||||
import { Bundler } from "./bundle.ts";
|
import { Bundler } from "./bundle.ts";
|
||||||
import { INTERNAL_PREFIX } from "./constants.ts";
|
import { ALIVE_URL, INTERNAL_PREFIX, REFRESH_JS_URL } from "./constants.ts";
|
||||||
import { JS_PREFIX } from "./constants.ts";
|
import { JS_PREFIX } from "./constants.ts";
|
||||||
import { BUILD_ID } from "./constants.ts";
|
import { BUILD_ID } from "./constants.ts";
|
||||||
import {
|
import {
|
||||||
@@ -21,6 +21,7 @@ import {
|
|||||||
import { render as internalRender } from "./render.tsx";
|
import { render as internalRender } from "./render.tsx";
|
||||||
|
|
||||||
export class ServerContext {
|
export class ServerContext {
|
||||||
|
#dev: boolean;
|
||||||
#pages: Page[];
|
#pages: Page[];
|
||||||
#staticFiles: [URL, string][];
|
#staticFiles: [URL, string][];
|
||||||
#bundler: Bundler;
|
#bundler: Bundler;
|
||||||
@@ -35,6 +36,7 @@ export class ServerContext {
|
|||||||
this.#staticFiles = staticFiles;
|
this.#staticFiles = staticFiles;
|
||||||
this.#renderer = renderer;
|
this.#renderer = renderer;
|
||||||
this.#bundler = new Bundler(pages);
|
this.#bundler = new Bundler(pages);
|
||||||
|
this.#dev = typeof Deno.env.get("DENO_DEPLOYMENT_ID") !== "string"; // Env var is only set in prod (on Deploy).
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -139,6 +141,9 @@ export class ServerContext {
|
|||||||
for (const page of this.#pages) {
|
for (const page of this.#pages) {
|
||||||
const bundlePath = `/${page.name}.js`;
|
const bundlePath = `/${page.name}.js`;
|
||||||
const imports = page.runtimeJS ? [bundleAssetUrl(bundlePath)] : [];
|
const imports = page.runtimeJS ? [bundleAssetUrl(bundlePath)] : [];
|
||||||
|
if (this.#dev) {
|
||||||
|
imports.push(REFRESH_JS_URL);
|
||||||
|
}
|
||||||
const createRender = (
|
const createRender = (
|
||||||
req: Request,
|
req: Request,
|
||||||
params: Record<string, string | string[]>,
|
params: Record<string, string | string[]>,
|
||||||
@@ -200,6 +205,39 @@ export class ServerContext {
|
|||||||
routes[`${INTERNAL_PREFIX}${JS_PREFIX}/${BUILD_ID}/:path*`] = this
|
routes[`${INTERNAL_PREFIX}${JS_PREFIX}/${BUILD_ID}/:path*`] = this
|
||||||
.#bundleAssetRoute();
|
.#bundleAssetRoute();
|
||||||
|
|
||||||
|
if (this.#dev) {
|
||||||
|
routes[REFRESH_JS_URL] = () => {
|
||||||
|
const js =
|
||||||
|
`const buildId = "${BUILD_ID}"; new EventSource("${ALIVE_URL}").addEventListener("message", (e) => { if (e.data !== buildId) { location.reload(); } });`;
|
||||||
|
return new Response(new TextEncoder().encode(js), {
|
||||||
|
headers: {
|
||||||
|
"content-type": "application/javascript; charset=utf-8",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
routes[ALIVE_URL] = () => {
|
||||||
|
let timerId: number | undefined = undefined;
|
||||||
|
const body = new ReadableStream({
|
||||||
|
start(controller) {
|
||||||
|
controller.enqueue(`data: ${BUILD_ID}\nretry: 100\n\n`);
|
||||||
|
timerId = setInterval(() => {
|
||||||
|
controller.enqueue(`data: ${BUILD_ID}\n\n`);
|
||||||
|
}, 1000);
|
||||||
|
},
|
||||||
|
cancel() {
|
||||||
|
if (timerId !== undefined) {
|
||||||
|
clearInterval(timerId);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return new Response(body.pipeThrough(new TextEncoderStream()), {
|
||||||
|
headers: {
|
||||||
|
"content-type": "text/event-stream",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
return routes;
|
return routes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import { Page, Renderer } from "./types.ts";
|
|||||||
import { PageProps } from "../runtime/types.ts";
|
import { PageProps } from "../runtime/types.ts";
|
||||||
import { SUSPENSE_CONTEXT } from "../runtime/suspense.ts";
|
import { SUSPENSE_CONTEXT } from "../runtime/suspense.ts";
|
||||||
import { HEAD_CONTEXT } from "../runtime/head.ts";
|
import { HEAD_CONTEXT } from "../runtime/head.ts";
|
||||||
|
import { REFRESH_JS_URL } from "./constants.ts";
|
||||||
|
|
||||||
export interface RenderOptions {
|
export interface RenderOptions {
|
||||||
page: Page;
|
page: Page;
|
||||||
@@ -172,7 +173,10 @@ export async function* render(opts: RenderOptions): AsyncIterable<string> {
|
|||||||
|
|
||||||
// If this is a static render (runtimeJS is false), then we don't need to
|
// If this is a static render (runtimeJS is false), then we don't need to
|
||||||
// render the props into the template.
|
// render the props into the template.
|
||||||
if (opts.imports.length === 0) {
|
if (
|
||||||
|
opts.imports.length === 0 ||
|
||||||
|
(opts.imports.length === 1 && opts.imports[0] === REFRESH_JS_URL)
|
||||||
|
) {
|
||||||
templateProps = undefined;
|
templateProps = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -120,7 +120,6 @@ Deno.test("/static page prerender", async () => {
|
|||||||
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
|
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
|
||||||
const body = await resp.text();
|
const body = await resp.text();
|
||||||
assert(!body.includes(`static.js`));
|
assert(!body.includes(`static.js`));
|
||||||
assert(!body.includes(`</script>`));
|
|
||||||
assertStringIncludes(body, "<p>This is a static page.</p>");
|
assertStringIncludes(body, "<p>This is a static page.</p>");
|
||||||
assert(!body.includes("__FRSH_PROPS"));
|
assert(!body.includes("__FRSH_PROPS"));
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user