From 8aa1b250b8592af68bb2bf85bf602c461948ba02 Mon Sep 17 00:00:00 2001 From: Ohkubo KOHEI Date: Mon, 14 Mar 2022 04:36:21 +0900 Subject: [PATCH] fix: `routes/[...path]` catches requests for `_frsh/refresh.`js (#130) --- src/server/context.ts | 72 +++++++++++++------------- tests/error_test.ts | 12 +++++ tests/fixture_error/fresh.gen.ts | 2 + tests/fixture_error/routes/[...all].ts | 7 +++ 4 files changed, 57 insertions(+), 36 deletions(-) create mode 100644 tests/fixture_error/routes/[...all].ts diff --git a/src/server/context.ts b/src/server/context.ts index 59a96056..09a170d7 100644 --- a/src/server/context.ts +++ b/src/server/context.ts @@ -282,6 +282,42 @@ export class ServerContext { #routes(): [router.Routes, RequestHandler, router.ErrorHandler] { const routes: router.Routes = {}; + routes[`${INTERNAL_PREFIX}${JS_PREFIX}/${BUILD_ID}/:path*`] = this + .#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", + }, + }); + }; + } + // Add the static file routes. for ( const { localUrl, path, size, contentType, etag } of this.#staticFiles @@ -398,42 +434,6 @@ export class ServerContext { ); }; - routes[`${INTERNAL_PREFIX}${JS_PREFIX}/${BUILD_ID}/:path*`] = this - .#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, unknownHandler, errorHandler]; } diff --git a/tests/error_test.ts b/tests/error_test.ts index 340ab332..fa3a8cf2 100644 --- a/tests/error_test.ts +++ b/tests/error_test.ts @@ -1,4 +1,5 @@ import { ServerContext } from "../server.ts"; +import { REFRESH_JS_URL } from "../src/server/constants.ts"; import { assert, assertEquals, assertStringIncludes } from "./deps.ts"; import manifest from "./fixture_error/fresh.gen.ts"; @@ -31,3 +32,14 @@ Deno.test("error page rendered", async () => { assertStringIncludes(body, `Error: boom!`); assertStringIncludes(body, `at render`); }); +Deno.test("refresh.js rendered", async () => { + const resp = await router( + new Request("https://fresh.deno.dev" + REFRESH_JS_URL), + ); + assert(resp); + assertEquals(resp.status, 200); + assertEquals( + resp.headers.get("content-type"), + "application/javascript; charset=utf-8", + ); +}); diff --git a/tests/fixture_error/fresh.gen.ts b/tests/fixture_error/fresh.gen.ts index 602e65c5..69ddc1fa 100644 --- a/tests/fixture_error/fresh.gen.ts +++ b/tests/fixture_error/fresh.gen.ts @@ -3,10 +3,12 @@ // To update this file, run `fresh manifest`. import * as $0 from "./routes/index.tsx"; +import * as $1 from "./routes/[...all].ts"; const manifest = { routes: { "./routes/index.tsx": $0, + "./routes/[...all].ts": $1, }, islands: {}, baseUrl: import.meta.url, diff --git a/tests/fixture_error/routes/[...all].ts b/tests/fixture_error/routes/[...all].ts new file mode 100644 index 00000000..dd4355b9 --- /dev/null +++ b/tests/fixture_error/routes/[...all].ts @@ -0,0 +1,7 @@ +import { Handlers } from "../../../server.ts"; + +export const handler: Handlers = { + GET(_req, ctx) { + return new Response(ctx.params.all); + }, +};