@@ -187,6 +187,83 @@ export const handlers = define.handlers({
187187});
188188```
189189
190+ ## Subdomain routing
191+
192+ Use middleware with ` URLPattern ` to route based on subdomains:
193+
194+ ``` ts routes/_middleware.ts
195+ const SUBDOMAIN_PATTERN = new URLPattern ({ hostname: " :sub.example.com" });
196+
197+ export default async function handler(ctx ) {
198+ const match = SUBDOMAIN_PATTERN .exec (ctx .req .url );
199+ if (match ) {
200+ const sub = match .hostname .groups [" sub" ];
201+ ctx .state .subdomain = sub ;
202+
203+ // Route to different handlers based on subdomain
204+ if (sub === " api" ) {
205+ return ctx .next (); // Let API routes handle it
206+ }
207+ if (sub !== " www" ) {
208+ // Tenant-specific logic
209+ ctx .state .tenant = await getTenant (sub );
210+ }
211+ }
212+ return ctx .next ();
213+ }
214+ ```
215+
216+ ## Proxying requests
217+
218+ Forward requests to an upstream server from a route handler:
219+
220+ ``` ts routes/api/[...path].ts
221+ import { define } from " @/utils.ts" ;
222+
223+ const UPSTREAM = " https://api.example.com" ;
224+
225+ export const handlers = define .handlers ({
226+ async GET(ctx ) {
227+ const url = new URL (ctx .params .path , UPSTREAM );
228+ url .search = ctx .url .search ;
229+
230+ const response = await fetch (url , {
231+ headers: ctx .req .headers ,
232+ });
233+
234+ return new Response (response .body , {
235+ status: response .status ,
236+ headers: response .headers ,
237+ });
238+ },
239+ });
240+ ```
241+
242+ This is useful for proxying to backend services or working around CORS
243+ restrictions during development.
244+
245+ ## Lazy-loading island content
246+
247+ Use Preact's ` lazy() ` and ` <Suspense> ` to code-split heavy components inside
248+ an island, so their JavaScript is only loaded when needed:
249+
250+ ``` tsx islands/HeavyFeature.tsx
251+ import { lazy , Suspense } from " preact/compat" ;
252+
253+ const Chart = lazy (() => import (" ../components/Chart.tsx" ));
254+
255+ export function HeavyFeature() {
256+ return (
257+ <Suspense fallback = { <p >Loading chart...</p >} >
258+ <Chart />
259+ </Suspense >
260+ );
261+ }
262+ ```
263+
264+ The ` Chart ` component's code is split into a separate chunk and only fetched
265+ when ` HeavyFeature ` renders in the browser.
266+
190267## Timing middleware
191268
192269Measure how long request processing takes:
0 commit comments