33//
44// On the production domain (`executor.sh`), marketing paths and the
55// unauthenticated landing page are served by the separate `executor-marketing`
6- // worker (bound as `env.MARKETING`). In local dev that worker isn't running, so
7- // unauthenticated visits fall through to the cloud app's routes (the sign-in
8- // page).
6+ // worker. This module deliberately has no TanStack Start or cloud application
7+ // imports: the Worker entry calls it before loading the Start server graph.
98// ---------------------------------------------------------------------------
109
11- import { env } from "cloudflare:workers" ;
12- import { createMiddleware } from "@tanstack/react-start" ;
13-
1410import { parseCookie } from "../auth/cookies" ;
1511
1612const MARKETING_PATHS = [
@@ -28,33 +24,25 @@ const MARKETING_PATHS = [
2824 "/pattern-graph-paper.svg" ,
2925] ;
3026
31- export const isMarketingPath = ( pathname : string ) =>
32- MARKETING_PATHS . some ( ( p ) => pathname === p || pathname . startsWith ( `${ p } /` ) ) ;
33-
34- const getMarketingWorker = ( ) => env . MARKETING as { fetch : typeof fetch } | undefined ;
35-
36- export const marketingMiddleware = createMiddleware ( { type : "request" } ) . server (
37- async ( { pathname, request, next } ) => {
38- // Only proxy to the marketing worker on the production domain. In local
39- // dev we don't run `executor-marketing`, so unauthenticated visits fall
40- // through to the cloud app's routes (which show the sign-in page).
41- const host = new URL ( request . url ) . hostname ;
42- if ( host !== "executor.sh" ) return next ( ) ;
27+ const SESSION_COOKIE = "wos-session" ;
4328
44- const shouldProxyToMarketing =
45- isMarketingPath ( pathname ) ||
46- ( pathname === "/" && ! parseCookie ( request . headers . get ( "cookie" ) , "wos-session" ) ) ;
47-
48- if ( ! shouldProxyToMarketing ) return next ( ) ;
49-
50- const marketing = getMarketingWorker ( ) ;
51- if ( ! marketing ) return next ( ) ;
29+ /** Whether an exact pathname belongs to the public marketing worker. */
30+ export const isMarketingPath = ( pathname : string ) : boolean =>
31+ MARKETING_PATHS . some ( ( p ) => pathname === p || pathname . startsWith ( `${ p } /` ) ) ;
5232
53- const url = new URL ( request . url ) ;
54- // Rewrite /home to / so marketing worker serves its homepage
55- if ( pathname === "/home" ) {
56- url . pathname = "/" ;
57- }
58- return marketing . fetch ( new Request ( url , request ) ) ;
59- } ,
60- ) ;
33+ /**
34+ * Project a production request onto the marketing service-binding request.
35+ * Returns `null` when the cloud application owns the request instead.
36+ */
37+ export const marketingProxyRequest = ( request : Request ) : Request | null => {
38+ const url = new URL ( request . url ) ;
39+ if ( url . hostname !== "executor.sh" ) return null ;
40+
41+ const shouldProxy =
42+ isMarketingPath ( url . pathname ) ||
43+ ( url . pathname === "/" && ! parseCookie ( request . headers . get ( "cookie" ) , SESSION_COOKIE ) ) ;
44+ if ( ! shouldProxy ) return null ;
45+
46+ if ( url . pathname === "/home" ) url . pathname = "/" ;
47+ return new Request ( url , request ) ;
48+ } ;
0 commit comments