From d7e8a9d5924466db407d3afd662966ccf1c31ebf Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Thu, 5 Oct 2023 14:55:26 +0800 Subject: [PATCH] chore: add deno-static entry --- .github/workflows/deploy-deno.yml | 8 ++++-- client/web/scripts/deno-static-entry.ts | 34 +++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 client/web/scripts/deno-static-entry.ts diff --git a/.github/workflows/deploy-deno.yml b/.github/workflows/deploy-deno.yml index 3290168f77c..ed1e6e07ceb 100644 --- a/.github/workflows/deploy-deno.yml +++ b/.github/workflows/deploy-deno.yml @@ -48,13 +48,17 @@ jobs: run: pnpm install --frozen-lockfile - name: Build step - run: "cd client/web && pnpm build" # 📝 Update the build command(s) if necessary + run: cd client/web && pnpm build env: SERVICE_URL: https://tailchat-nightly.moonrailgun.com + - name: Copy Deno Entry + run: cd client/web && cp ./scripts/deno-static-entry.ts ./dist/deno-static-entry.ts + - name: Upload to Deno Deploy uses: denoland/deployctl@v1 with: project: "tailchat-nightly" - entrypoint: https://deno.land/std@0.202.0/http/file_server.ts + # entrypoint: https://deno.land/std@0.202.0/http/file_server.ts + entrypoint: deno-static-entry.ts root: "./client/web/dist" diff --git a/client/web/scripts/deno-static-entry.ts b/client/web/scripts/deno-static-entry.ts new file mode 100644 index 00000000000..7ac0227b267 --- /dev/null +++ b/client/web/scripts/deno-static-entry.ts @@ -0,0 +1,34 @@ +// @ts-nocheck +import { serve } from 'https://deno.land/std@0.120.0/http/server.ts'; +import { serveFile } from 'https://deno.land/std@0.120.0/http/file_server.ts'; +import { fromFileUrl } from 'https://deno.land/std@0.120.0/path/mod.ts'; + +const clientRoot = new URL('./', import.meta.url); +serve(async (req) => { + const url = new URL(req.url); + const localPath = new URL('./' + url.pathname, clientRoot); + const filePath = fromFileUrl(localPath); + + const fileExists = await checkFileExists(filePath); + if (fileExists) { + return serveFile(req, filePath); + } else { + return await serveFile( + req, + fromFileUrl(new URL('./index.html', clientRoot)) + ); + } +}); + +async function checkFileExists(filePath: string): Promise { + try { + await Deno.stat(filePath); + return true; + } catch (error) { + if (error instanceof Deno.errors.NotFound) { + return false; + } else { + throw error; + } + } +}