({ params })
| 3 | import { getUriPreview } from "~/services/uriPreview.server"; |
| 4 | |
| 5 | export const loader: LoaderFunction = async ({ params }) => { |
| 6 | try { |
| 7 | invariant(params.url, "expected params.url"); |
| 8 | |
| 9 | const decoded = decodeURIComponent(params.url); |
| 10 | |
| 11 | const earlyReturn = earlyRespondIfHomepagePreviewUri(decoded); |
| 12 | |
| 13 | if (earlyReturn) { |
| 14 | return new Response(JSON.stringify(earlyReturn), { |
| 15 | headers: { |
| 16 | "Content-Type": "application/json; charset=utf-8", |
| 17 | "Cache-Control": "public, max-age=3600", |
| 18 | }, |
| 19 | }); |
| 20 | } |
| 21 | |
| 22 | const result = await getUriPreview(decoded); |
| 23 | |
| 24 | return new Response(JSON.stringify(result), { |
| 25 | headers: { |
| 26 | "Content-Type": "application/json; charset=utf-8", |
| 27 | "Cache-Control": "public, max-age=3600", |
| 28 | }, |
| 29 | }); |
| 30 | } catch { |
| 31 | return new Response( |
| 32 | JSON.stringify({ error: "Unable to preview this URL" }), |
| 33 | { |
| 34 | headers: { |
| 35 | "Content-Type": "application/json; charset=utf-8", |
| 36 | "Cache-Control": "public, max-age=3600", |
| 37 | }, |
| 38 | } |
| 39 | ); |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | function earlyRespondIfHomepagePreviewUri(uri: string) { |
| 44 | if (uri === "https://www.theonion.com/") { |
nothing calls this directly
no test coverage detected