@@ -48,6 +48,36 @@ If you need to always run your Worker script before serving static assets (for e
4848
4949</WranglerConfig >
5050
51+ <TypeScriptExample filename = " ./worker/index.ts" >
52+
53+ ``` ts
54+ import { WorkerEntrypoint } from " cloudflare:workers" ;
55+
56+ export default class extends WorkerEntrypoint <Env > {
57+ async fetch(request : Request ) {
58+ // You can perform checks before fetching assets
59+ const user = await checkIfRequestIsAuthenticated (request );
60+
61+ if (! user ) {
62+ return new Response (" Unauthorized" , { status: 401 });
63+ }
64+
65+ // You can then just fetch the assets as normal, or you could pass in a custom Request object here if you wanted to fetch some other specific asset
66+ const assetResponse = await this .env .ASSETS .fetch (request );
67+
68+ // You can return static asset response as-is, or you can transform them with something like HTMLRewriter
69+ return new HTMLRewriter ()
70+ .on (" #user" , {
71+ element(element ) {
72+ element .setInnerContent (JSON .stringify ({ name: user .name }));
73+ },
74+ })
75+ .transform (assetResponse );
76+ }
77+ }
78+ ```
79+ </TypeScriptExample >
80+
5181### Run Worker first for selective paths
5282
5383You can also configure selective Worker-first routing using an array of route patterns, often paired with the [ ` single-page-application ` setting] ( /workers/static-assets/routing/single-page-application/#advanced-routing-control ) . This allows you to run the Worker first only for specific routes while letting other requests follow the default asset-first behavior:
@@ -63,9 +93,37 @@ You can also configure selective Worker-first routing using an array of route pa
6393 "directory" : " ./dist/" ,
6494 "not_found_handling" : " single-page-application" ,
6595 "binding" : " ASSETS" ,
66- "run_worker_first" : [" /api/* " , " !/api/docs/* " ]
96+ "run_worker_first" : [" /oauth/callback " ]
6797 }
6898}
6999```
70100
71101</WranglerConfig >
102+
103+ <TypeScriptExample filename = " ./worker/index.ts" >
104+
105+ ``` ts
106+ import { WorkerEntrypoint } from " cloudflare:workers" ;
107+
108+ export default class extends WorkerEntrypoint <Env > {
109+ async fetch(request : Request ) {
110+ // The only thing this Worker script does is handle an OAuth callback.
111+ // All other requests either serve an asset that matches or serve the index.html fallback, without ever hitting this code.
112+ const url = new URL (request .url );
113+ const code = url .searchParams .get (" code" );
114+ const state = url .searchParams .get (" state" );
115+
116+ const accessToken = await exchangeCodeForToken (code , state );
117+ const sessionIdentifier = await storeTokenAndGenerateSession (accessToken );
118+
119+ // Redirect back to the index, but set a cookie that the front-end will use.
120+ return new Response (null , {
121+ headers: {
122+ " Location" : " /" ,
123+ " Set-Cookie" : ` session_token=${sessionIdentifier }; HttpOnly; Secure; SameSite=Lax; Path=/ `
124+ }
125+ });
126+ }
127+ }
128+ ```
129+ </TypeScriptExample >
0 commit comments