Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate back to Cloudflare Workers #147

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Loading