Skip to content

Commit

Permalink
Migrate back to Cloudflare Workers (#147)
Browse files Browse the repository at this point in the history
* Migrate back to Cloudflare Workers

* Delete deprecated CF pages files
  • Loading branch information
benvinegar authored Jan 16, 2025
1 parent 7c28341 commit e2d5578
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
11 changes: 0 additions & 11 deletions packages/server/functions/[[page]].ts

This file was deleted.

22 changes: 0 additions & 22 deletions packages/server/functions/collect.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
38 changes: 38 additions & 0 deletions packages/server/workers/app.ts
Original file line number Diff line number Diff line change
@@ -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<Env>;
5 changes: 4 additions & 1 deletion packages/server/wrangler.toml
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit e2d5578

Please sign in to comment.