History

Drop astro:middleware dependency in marketing types

CI runs typecheck on Node 20, but `astro sync` (which generates the virtual `astro:middleware` module's types into .astro/types.d.ts) requires Node 22+. Use the public MiddlewareHandler type from `astro` instead and add a small env.d.ts that references astro/client so `import.meta.env` is typed without needing astro sync.

Rhys Sullivan committed 6 months ago

Commit f00160d1 parent 5bd6f903 files · +12 −5Browse files

Unified · Split

3 files
  1. apps/marketing/package.json +2 −2
  2. apps/marketing/src/env.d.ts +7 −0
  3. apps/marketing/src/middleware.ts +3 −3
apps/marketing/package.json+2−2
@@ -10,8 +10,8 @@ · 9 unmodified lines
10 "preview": "astro preview",10 "preview": "astro preview",
11 "deploy": "astro build && npx wrangler deploy --config dist/server/wrangler.json",11 "deploy": "astro build && npx wrangler deploy --config dist/server/wrangler.json",
12 "astro": "astro",12 "astro": "astro",
13 "typecheck": "astro sync && tsgo --noEmit",13 "typecheck": "tsgo --noEmit",
14 "typecheck:slow": "astro sync && tsc --noEmit"14 "typecheck:slow": "tsc --noEmit"
15 },15 },
16 "dependencies": {16 "dependencies": {
17 "@astrojs/cloudflare": "^13.0.0",17 "@astrojs/cloudflare": "^13.0.0",
apps/marketing/src/env.d.ts+7−0
@@ -0,0 +1,7 @@
1/// <reference types="astro/client" />
2
3interface ImportMetaEnv {
4 readonly PUBLIC_POSTHOG_KEY?: string;
5 readonly PUBLIC_POSTHOG_HOST?: string;
6 readonly PUBLIC_ANALYTICS_PATH?: string;
7}
apps/marketing/src/middleware.ts+3−3
@@ -1,4 +1,4 @@
1import { defineMiddleware } from "astro:middleware";1import type { MiddlewareHandler } from "astro";
2 2
3// PostHog reverse proxy — the browser SDK targets a build-randomized3// PostHog reverse proxy — the browser SDK targets a build-randomized
4// first-party path and we forward to PostHog's ingest + asset hosts. Keeps4// first-party path and we forward to PostHog's ingest + asset hosts. Keeps
@@ -12,7 +12,7 @@ const POSTHOG_PROXY_PATH = `/api/${(import.meta.env.PUBLIC_ANALYTICS_PATH ?? "a" · 7 unmodified lines
12 "",12 "",
13)}`;13)}`;
14 14
15export const onRequest = defineMiddleware(async (context, next) => {15export const onRequest: MiddlewareHandler = async (context, next) => {
16 const { pathname } = new URL(context.request.url);16 const { pathname } = new URL(context.request.url);
17 if (pathname !== POSTHOG_PROXY_PATH && !pathname.startsWith(`${POSTHOG_PROXY_PATH}/`)) {17 if (pathname !== POSTHOG_PROXY_PATH && !pathname.startsWith(`${POSTHOG_PROXY_PATH}/`)) {
18 return next();18 return next();
@@ -29,4 +29,4 @@ export const onRequest = defineMiddleware(async (context, next) => { · 10 unmodified lines
29 const upstream = new Request(url, context.request);29 const upstream = new Request(url, context.request);
30 upstream.headers.delete("cookie");30 upstream.headers.delete("cookie");
31 return fetch(upstream);31 return fetch(upstream);
32});32};