|
| 1 | +import { setServerPlatform } from "@qwik.dev/core/server"; |
| 2 | +import type { |
| 3 | + ClientConn, |
| 4 | + ServerRenderOptions, |
| 5 | + ServerRequestEvent, |
| 6 | +} from "@qwik.dev/router/middleware/request-handler"; |
| 7 | +import { |
| 8 | + getNotFound, |
| 9 | + isStaticPath, |
| 10 | + mergeHeadersCookies, |
| 11 | + requestHandler, |
| 12 | +} from "@qwik.dev/router/middleware/request-handler"; |
| 13 | + |
| 14 | +/** @public */ |
| 15 | +export interface NetAddr { |
| 16 | + transport: "tcp" | "udp"; |
| 17 | + hostname: string; |
| 18 | + port: number; |
| 19 | +} |
| 20 | + |
| 21 | +/** @public */ |
| 22 | +export function createQwikRouter(opts: QwikRouterFetchOptions) { |
| 23 | + if (opts.manifest) { |
| 24 | + setServerPlatform(opts.manifest); |
| 25 | + } |
| 26 | + async function router(request: Request) { |
| 27 | + try { |
| 28 | + const url = new URL(request.url); |
| 29 | + |
| 30 | + const serverRequestEv: ServerRequestEvent<Response> = { |
| 31 | + mode: "server", |
| 32 | + locale: undefined, |
| 33 | + url, |
| 34 | + env: { get: (p: string) => process.env[p] }, |
| 35 | + request, |
| 36 | + getWritableStream: (status, headers, cookies, resolve) => { |
| 37 | + const { readable, writable } = new TransformStream<Uint8Array>(); |
| 38 | + const response = new Response(readable, { |
| 39 | + status, |
| 40 | + headers: mergeHeadersCookies(headers, cookies), |
| 41 | + }); |
| 42 | + resolve(response); |
| 43 | + return writable; |
| 44 | + }, |
| 45 | + platform: { |
| 46 | + ssr: true, |
| 47 | + }, |
| 48 | + getClientConn: () => { |
| 49 | + return opts.getClientConn ? opts.getClientConn(request) : {}; |
| 50 | + }, |
| 51 | + }; |
| 52 | + |
| 53 | + // send request to qwik router request handler |
| 54 | + const handledResponse = await requestHandler(serverRequestEv, opts); |
| 55 | + if (handledResponse) { |
| 56 | + handledResponse.completion.then((v) => { |
| 57 | + if (v) { |
| 58 | + console.error(v); |
| 59 | + } |
| 60 | + }); |
| 61 | + const response = await handledResponse.response; |
| 62 | + if (response) { |
| 63 | + return response; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // qwik router did not have a route for this request |
| 68 | + return null; |
| 69 | + } catch (e: any) { |
| 70 | + console.error(e); |
| 71 | + return new Response(String(e || "Error"), { |
| 72 | + status: 500, |
| 73 | + headers: { |
| 74 | + "Content-Type": "text/plain; charset=utf-8", |
| 75 | + }, |
| 76 | + }); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + const notFound = async (request: Request) => { |
| 81 | + try { |
| 82 | + const url = new URL(request.url); |
| 83 | + |
| 84 | + // In the development server, we replace the getNotFound function |
| 85 | + // For static paths, we assign a static "Not Found" message. |
| 86 | + // This ensures consistency between development and production environments for specific URLs. |
| 87 | + const notFoundHtml = |
| 88 | + !request.headers.get("accept")?.includes("text/html") || |
| 89 | + isStaticPath(request.method || "GET", url) |
| 90 | + ? "Not Found" |
| 91 | + : getNotFound(url.pathname); |
| 92 | + return new Response(notFoundHtml, { |
| 93 | + status: 404, |
| 94 | + headers: { |
| 95 | + "Content-Type": "text/html; charset=utf-8", |
| 96 | + "X-Not-Found": url.pathname, |
| 97 | + }, |
| 98 | + }); |
| 99 | + } catch (e) { |
| 100 | + console.error(e); |
| 101 | + return new Response(String(e || "Error"), { |
| 102 | + status: 500, |
| 103 | + headers: { |
| 104 | + "Content-Type": "text/plain; charset=utf-8", |
| 105 | + }, |
| 106 | + }); |
| 107 | + } |
| 108 | + }; |
| 109 | + |
| 110 | + return { |
| 111 | + router, |
| 112 | + notFound, |
| 113 | + }; |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * @deprecated Use `createQwikRouter` instead. Will be removed in V3 |
| 118 | + * @public |
| 119 | + */ |
| 120 | +export const createQwikCity = createQwikRouter; |
| 121 | + |
| 122 | +/** @public */ |
| 123 | +export interface QwikRouterFetchOptions |
| 124 | + extends Omit<ServerRenderOptions, "qwikCityPlan"> { |
| 125 | + getClientConn?: (request: Request) => ClientConn; |
| 126 | +} |
0 commit comments