cloudflare/vinext

Public

mirrored from https://github.com/cloudflare/vinextAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.0.9

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

examples/hackernews/worker/index.ts

42lines · modecode

1/**
2 * Cloudflare Worker entry point for the Hacker News example.
3 *
4 * Based on vercel/next-react-server-components — a Hacker News clone
5 * using React Server Components, now running on vinext + Cloudflare Workers.
6 */
7
8export default {
9 async fetch(request: Request): Promise<Response> {
10 try {
11 const url = new URL(request.url);
12 const { pathname } = url;
13
14 // Block protocol-relative URL open redirect attacks.
15 if (pathname.startsWith("//")) {
16 return new Response("404 Not Found", { status: 404 });
17 }
18
19 // Load the RSC handler from the RSC environment.
20 // @ts-expect-error — import.meta.viteRsc is injected by @vitejs/plugin-rsc
21 const rscModule = await import.meta.viteRsc.loadModule("rsc", "index");
22
23 const result = await rscModule.default(request);
24
25 if (result instanceof Response) {
26 return result;
27 }
28
29 if (result === null || result === undefined) {
30 return new Response("Not Found", { status: 404 });
31 }
32
33 return new Response(String(result), { status: 200 });
34 } catch (error) {
35 console.error("[vinext] Worker error:", error);
36 return new Response(
37 `Internal Server Error: ${error instanceof Error ? error.message : String(error)}`,
38 { status: 500 },
39 );
40 }
41 },
42};
43