Skip to content

Commit 7256d6b

Browse files
committed
fix: try to fix env vars access in prod worker
1 parent 0b20232 commit 7256d6b

File tree

1 file changed

+44
-26
lines changed

1 file changed

+44
-26
lines changed

src/index.ts

+44-26
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,55 @@ import { type Toucan as Sentry } from "toucan-js";
44
import { Hono, type HonoRequest, type Context } from "hono";
55
import { literal, union, safeParse } from "valibot";
66

7+
// Add Cloudflare Worker types
8+
interface ExecutionContext {
9+
waitUntil(promise: Promise<any>): void;
10+
passThroughOnException(): void;
11+
}
12+
13+
// Add global declarations for environment variables
14+
declare global {
15+
var UMAMI_SITE_ID: string | undefined;
16+
var UMAMI_HOST_URL: string | undefined;
17+
var SENTRY_DSN: string | undefined;
18+
}
19+
720
type Redirect = Context["redirect"];
8-
export interface Bindings {
21+
export interface Env {
922
UMAMI_SITE_ID: string;
1023
UMAMI_HOST_URL: string;
1124
SENTRY_DSN: string;
1225
}
1326

14-
const app = new Hono<{ Bindings: Bindings }>();
15-
// Initialize sentry with explicit DSN from env
16-
app.use("*", async (c, next) => {
17-
// Initialize sentry with explicit config
18-
if (c.env.SENTRY_DSN) {
19-
return sentry({
20-
dsn: c.env.SENTRY_DSN,
21-
})(c, next);
22-
}
23-
return next();
24-
});
27+
const app = new Hono<{ Bindings: Env }>();
28+
app.use("*", sentry());
2529

2630
app.use("*", async (c, next) => {
27-
// Debug entire env object
31+
console.log("Global env access:", {
32+
UMAMI_SITE_ID: globalThis.UMAMI_SITE_ID,
33+
UMAMI_HOST_URL: globalThis.UMAMI_HOST_URL,
34+
SENTRY_DSN: globalThis.SENTRY_DSN,
35+
});
36+
37+
// Debug env object and context
2838
console.log("ENV KEYS:", Object.keys(c.env || {}));
39+
console.log("env stringified", JSON.stringify(c.env, null, 2));
2940

30-
// Debug individual variables
31-
console.log("UMAMI_SITE_ID (direct):", c.env.UMAMI_SITE_ID);
32-
console.log("UMAMI_HOST_URL (direct):", c.env.UMAMI_HOST_URL);
33-
console.log("SENTRY_DSN (direct):", c.env.SENTRY_DSN);
41+
// Try to initialize from globals if context env is undefined
42+
const umamiSiteId = c.env?.UMAMI_SITE_ID || globalThis.UMAMI_SITE_ID;
43+
const umamiHostUrl = c.env?.UMAMI_HOST_URL || globalThis.UMAMI_HOST_URL;
3444

35-
// Original logs
36-
console.log("UMAMI_SITE_ID", c.env?.UMAMI_SITE_ID);
37-
console.log("UMAMI_HOST_URL", c.env?.UMAMI_HOST_URL);
38-
if (c.env?.UMAMI_SITE_ID && c.env?.UMAMI_HOST_URL) {
39-
console.log("Initializing umami");
45+
if (umamiSiteId && umamiHostUrl) {
46+
console.log("Initializing umami with:", { umamiSiteId, umamiHostUrl });
4047
umami.init({
41-
websiteId: c.env.UMAMI_SITE_ID,
42-
hostUrl: c.env.UMAMI_HOST_URL,
48+
websiteId: umamiSiteId,
49+
hostUrl: umamiHostUrl,
4350
});
4451

4552
if (!c.req.url.includes("healthcheck")) {
4653
console.log("Sending umami event");
4754
const res = await umami.send({
48-
website: c.env.UMAMI_SITE_ID,
55+
website: umamiSiteId,
4956
hostname: "arpit.im",
5057
referrer: c.req.header("Referer"),
5158
url: c.req.url,
@@ -290,4 +297,15 @@ app.get("/*", ({ req, redirect, get }) => {
290297
);
291298
});
292299

293-
export default app;
300+
// Export the worker handler
301+
export default {
302+
fetch: (request: Request, env: Env, ctx: ExecutionContext) => {
303+
// Make env variables available globally
304+
globalThis.UMAMI_SITE_ID = env.UMAMI_SITE_ID;
305+
globalThis.UMAMI_HOST_URL = env.UMAMI_HOST_URL;
306+
globalThis.SENTRY_DSN = env.SENTRY_DSN;
307+
308+
// Pass request to Hono app with the environment
309+
return app.fetch(request, env, ctx);
310+
},
311+
};

0 commit comments

Comments
 (0)