|
| 1 | +import type { GetServerSideProps, InferGetServerSidePropsType } from "next"; |
| 2 | +import type { BundleErrorResponse } from "~/api"; |
| 3 | +import { type Context, getRequestContext } from "~/context"; |
| 4 | +import type { SharedEnvironmentVariables } from "~/env"; |
| 5 | + |
| 6 | +import { Documentation } from "~/layouts/Documentation"; |
| 7 | +import { Error } from "~/layouts/Error"; |
| 8 | +import { Homepage } from "~/layouts/homepage"; |
| 9 | +import { Site } from "~/layouts/Site"; |
| 10 | +import { Redirect, bundleCodeToStatusCode } from "~/utils"; |
| 11 | + |
| 12 | +export const getServerSideProps = (async ({ params, req, res }) => { |
| 13 | + const env = { |
| 14 | + VERCEL: process.env.VERCEL || null, |
| 15 | + VERCEL_ENV: process.env.VERCEL_ENV || null, |
| 16 | + VERCEL_GIT_COMMIT_SHA: process.env.VERCEL_GIT_COMMIT_SHA || null, |
| 17 | + } satisfies SharedEnvironmentVariables; |
| 18 | + |
| 19 | + const param = params?.path ? params.path : []; |
| 20 | + const chunks = Array.isArray(param) ? param : [param]; |
| 21 | + |
| 22 | + // If there are no chunks, render the homepage. |
| 23 | + if (chunks.length === 0) { |
| 24 | + return { props: { env, ctx: null } }; |
| 25 | + } |
| 26 | + |
| 27 | + // Any paths with only one chunk are invalid, and should be handled |
| 28 | + // by other page routes. |
| 29 | + if (chunks.length === 1) { |
| 30 | + return { notFound: true }; |
| 31 | + } |
| 32 | + |
| 33 | + // Any paths with two chunks or more are a repository page. |
| 34 | + let [owner, repository, ...path] = chunks; |
| 35 | + let ref: string | undefined; |
| 36 | + |
| 37 | + // Check if the repo includes a ref (invertase/foo~bar) |
| 38 | + if (repository.includes("~")) { |
| 39 | + [repository, ref] = repository.split("~"); |
| 40 | + } |
| 41 | + |
| 42 | + let ctx: Context | null = null; |
| 43 | + |
| 44 | + try { |
| 45 | + ctx = await getRequestContext(req, { |
| 46 | + owner, |
| 47 | + repository, |
| 48 | + ref, |
| 49 | + path: path.join("/"), |
| 50 | + }); |
| 51 | + } catch (error) { |
| 52 | + // If error is a Redirect instance |
| 53 | + if (error instanceof Redirect) { |
| 54 | + return { |
| 55 | + redirect: { |
| 56 | + destination: error.url, |
| 57 | + permanent: false, |
| 58 | + }, |
| 59 | + }; |
| 60 | + } |
| 61 | + |
| 62 | + // If error is a Response instance |
| 63 | + if (error instanceof Response) { |
| 64 | + // Parse the response body as JSON. |
| 65 | + const body = (await error.json()) as BundleErrorResponse; |
| 66 | + |
| 67 | + // Set the status code to the bundle error code. |
| 68 | + res.statusCode = bundleCodeToStatusCode(body); |
| 69 | + |
| 70 | + return { |
| 71 | + props: { |
| 72 | + error: body, |
| 73 | + }, |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + throw error; |
| 78 | + } |
| 79 | + |
| 80 | + // Cache the response for 1 second, and revalidate in the background. |
| 81 | + res.setHeader( |
| 82 | + "Cache-Control", |
| 83 | + "public, s-maxage=1, stale-while-revalidate=59" |
| 84 | + ); |
| 85 | + |
| 86 | + return { props: { env, ctx } }; |
| 87 | +}) satisfies GetServerSideProps< |
| 88 | + | { |
| 89 | + env: SharedEnvironmentVariables; |
| 90 | + ctx: Context | null; |
| 91 | + } |
| 92 | + | { |
| 93 | + error: BundleErrorResponse; |
| 94 | + } |
| 95 | +>; |
| 96 | + |
| 97 | +export default function Route({ |
| 98 | + env, |
| 99 | + ctx, |
| 100 | + error, |
| 101 | +}: InferGetServerSidePropsType<typeof getServerSideProps>) { |
| 102 | + // If there is an error, render the error page. |
| 103 | + if (error) { |
| 104 | + return ( |
| 105 | + <Site> |
| 106 | + <Error error={error} /> |
| 107 | + </Site> |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + if (!ctx) { |
| 112 | + return <Homepage />; |
| 113 | + } |
| 114 | + |
| 115 | + return ( |
| 116 | + <> |
| 117 | + <script |
| 118 | + dangerouslySetInnerHTML={{ |
| 119 | + __html: `window.ENV = ${env ? JSON.stringify(env) : "{}"}`, |
| 120 | + }} |
| 121 | + /> |
| 122 | + <Documentation ctx={ctx} /> |
| 123 | + </> |
| 124 | + ); |
| 125 | +} |
0 commit comments