From 151855316c793c123375cc59e26c192d319537b3 Mon Sep 17 00:00:00 2001 From: Ben Vinegar <2153+benvinegar@users.noreply.github.com> Date: Thu, 16 Jan 2025 15:42:00 -0500 Subject: [PATCH 1/2] Migrate back to Cloudflare Workers --- .github/workflows/cd.yaml | 2 +- packages/server/package.json | 4 ++-- packages/server/workers/app.ts | 38 ++++++++++++++++++++++++++++++++++ packages/server/wrangler.toml | 5 ++++- 4 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 packages/server/workers/app.ts diff --git a/.github/workflows/cd.yaml b/.github/workflows/cd.yaml index af0f3048..f8e15c7e 100644 --- a/.github/workflows/cd.yaml +++ b/.github/workflows/cd.yaml @@ -18,5 +18,5 @@ jobs: - uses: cloudflare/wrangler-action@v3 with: apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} - command: pages deploy ./build/client + command: deploy ./build/client workingDirectory: "packages/server" diff --git a/packages/server/package.json b/packages/server/package.json index 96c6248a..bda6cdc9 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -10,8 +10,8 @@ "scripts": { "dev": "react-router dev", "build": "react-router build", - "preview": "wrangler pages dev ./build/client", - "deploy": "wrangler pages deploy ./build/client", + "preview": "wrangler dev", + "deploy": "wrangler deploy", "lint": "eslint --ignore-path ../../.gitignore --ignore-pattern public/tracker.js --cache --cache-location ./node_modules/.cache/eslint .", "test": "TZ=EST vitest run", "test-ci": "TZ=EST vitest run --coverage", diff --git a/packages/server/workers/app.ts b/packages/server/workers/app.ts new file mode 100644 index 00000000..645f027b --- /dev/null +++ b/packages/server/workers/app.ts @@ -0,0 +1,38 @@ +import type { ExportedHandler } from "@cloudflare/workers-types"; +import { createRequestHandler, type ServerBuild } from "react-router"; + +import { getLoadContext } from "~/load-context"; + +import * as build from "../build/server"; + +const requestHandler = createRequestHandler(build as unknown as ServerBuild); + +export default { + // @ts-expect-error TODO figure out types here + async fetch(request: any, env: any, ctx: any) { + try { + const loadContext = getLoadContext({ + request, + context: { + cloudflare: { + ctx: { + waitUntil: ctx.waitUntil.bind(ctx), + passThroughOnException: + ctx.passThroughOnException.bind(ctx), + }, + cf: request.cf as never, + // @ts-expect-error TODO: figure out how to get this type to work + caches, + env, + }, + }, + }); + return await requestHandler(request, loadContext); + } catch (error) { + console.log(error); + return new Response("An unexpected error occurred", { + status: 500, + }); + } + }, +} satisfies ExportedHandler; diff --git a/packages/server/wrangler.toml b/packages/server/wrangler.toml index 53dbb527..6d99d43b 100644 --- a/packages/server/wrangler.toml +++ b/packages/server/wrangler.toml @@ -1,9 +1,12 @@ # default deploy name name = "counterscale" -pages_build_output_dir = "build/client" +main = "./workers/app.ts" + # https://developers.cloudflare.com/workers/platform/compatibility-dates compatibility_date = "2024-12-13" +assets = { directory = "build/client" } + [[analytics_engine_datasets]] binding = "WEB_COUNTER_AE" dataset = "metricsDataset" \ No newline at end of file From ea8f75904a49b89b3e386d4ad5c6032d4e92253c Mon Sep 17 00:00:00 2001 From: Ben Vinegar <2153+benvinegar@users.noreply.github.com> Date: Thu, 16 Jan 2025 16:05:24 -0500 Subject: [PATCH 2/2] Delete deprecated CF pages files --- packages/server/functions/[[page]].ts | 11 ----------- packages/server/functions/collect.ts | 22 ---------------------- 2 files changed, 33 deletions(-) delete mode 100644 packages/server/functions/[[page]].ts delete mode 100644 packages/server/functions/collect.ts diff --git a/packages/server/functions/[[page]].ts b/packages/server/functions/[[page]].ts deleted file mode 100644 index 53ca3b51..00000000 --- a/packages/server/functions/[[page]].ts +++ /dev/null @@ -1,11 +0,0 @@ -import { createPagesFunctionHandler } from "@react-router/cloudflare"; - -import * as build from "../build/server"; - -import { getLoadContext } from "../app/load-context"; - -export const onRequest = createPagesFunctionHandler({ - // @ts-expect-error - the server build file is generated by `remix vite:build` - build, - getLoadContext, -}); diff --git a/packages/server/functions/collect.ts b/packages/server/functions/collect.ts deleted file mode 100644 index 46d4bf86..00000000 --- a/packages/server/functions/collect.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { collectRequestHandler } from "~/analytics/collect"; -import type { - PagesFunction, - Response as CfResponse, -} from "@cloudflare/workers-types"; - -export const onRequest: PagesFunction = async ({ request, env }) => { - // Convert Cloudflare Request object to regular Request object - const _request = request as unknown as Request; - - // Similarly, convert Response to Cloudflare Response type by adding - // webSocket property - const response = collectRequestHandler( - _request, - env, - ) as unknown as CfResponse; - - return { - ...response, - webSocket: null, - } as CfResponse; -};