Feat: Dynamic Page Route

动态路由页面 + 404 异常捕获
This commit is contained in:
奇趣保罗 2023-12-01 23:01:44 +08:00
parent 9391a37b21
commit 9482b2ac18
6 changed files with 154 additions and 3 deletions

View File

@ -9,6 +9,61 @@
}
}
h2, h3 {
font-size: 1.2em;
}
h1 {
margin-top: 5rem;
font-size: 1.75em;
position: relative;
margin-bottom: 1em;
padding-bottom: .25em;
display: inline-block;
scroll-margin-top: 7em;
&:first-child {
margin-top: 0;
}
&::before {
content: "";
left: 0;
bottom: 0;
position: absolute;
width: 1.5em;
height: 4px;
display: block;
border-radius: 4px;
background-color: rgb(244 114 182 / var(--tw-text-opacity));
}
}
h2 {
margin-top: 2em;
scroll-margin-top: 4em;
&:first-child {
margin-top: 0;
}
}
a {
color:rgb(244 114 182 / var(--tw-bg-opacity));
}
img {
border-radius: .75rem;
}
em {
font-size: smaller;
font-style: normal;
padding: .15rem .5rem;
border-radius: .75rem;
}
pre {
tab-size: 4;
padding: 1em;

View File

@ -7,13 +7,56 @@ import {
Outlet,
Scripts,
ScrollRestoration,
isRouteErrorResponse,
useRouteError,
} from "@remix-run/react";
import Header from "./components/layout/header";
import Spinner from "./components/common/spinner";
import Footer from "./components/layout/footer";
import { siteTitle } from "~/utils";
import "./index.css";
export function ErrorBoundary() {
const error = useRouteError();
const isRouteError = isRouteErrorResponse(error);
const [statusCode, message] = (() => {
if (isRouteError) {
return [error.status, error.statusText];
}
if (error instanceof Error) {
return [500, error.message];
}
return [500, "未知异常"];
})();
return (
<html lang="zh-cmn-hans">
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>{siteTitle(statusCode)}</title>
<Meta />
<Links />
</head>
<body className="font-mi pt-16 bg-orange-50 text-neutral-600">
<Header />
<main className="px-2 py-24 max-w-3xl mx-auto">
<h1 className="text-center text-5xl/tight md:text-7xl/tight mb-4">
{statusCode}
</h1>
<p className="text-center opacity-60">{message}</p>
</main>
<Footer />
<Scripts />
</body>
</html>
);
}
export const links: LinksFunction = () => [
...(cssBundleHref ? [{ rel: "stylesheet", href: cssBundleHref }] : []),
{ rel: "icon", href: "icon.png" },

39
app/routes/$.tsx Normal file
View File

@ -0,0 +1,39 @@
import { LoaderFunctionArgs, MetaFunction, json } from "@remix-run/node";
import { useLoaderData } from "@remix-run/react";
import Article from "~/components/common/article";
import { siteTitle } from "~/utils";
export const meta: MetaFunction<typeof loader> = ({ data }) => {
return [
{ title: data ? siteTitle(data.data.title) : "404" },
{ name: "description", content: data?.data.desc },
];
}
export async function loader({ params }: LoaderFunctionArgs) {
const slug = params["*"];
const page = await fetch(`https://paul.ren/api/page/get?slug=${slug}&html`).then((res) => res.json()) as API.Response<API.Page.IPageData>;
if (page.status === "Failed") {
throw json("Not Found", { status: 404, statusText: page.msg });
}
return json(page);
}
export default function DynamicPage() {
const page = useLoaderData<typeof loader>();
return (
<main className="px-2 py-24 max-w-3xl mx-auto">
<section className="mb-12">
<h1 className="text-center text-5xl/tight md:text-7xl/tight mb-4">{page.data.title}</h1>
<p className="text-center opacity-60">{page.data.desc}</p>
</section>
<section className="p-5 bg-white rounded-xl border-b-4 border-b-cyan-200">
<Article html={page.data.content} />
</section>
</main>
);
};

View File

@ -12,13 +12,13 @@ export const meta: MetaFunction<typeof loader> = ({ data }) => {
export async function loader({ params }: LoaderFunctionArgs) {
if (Number.isNaN(Number(params.year)) || Number.isNaN(Number(params.id))) {
throw json("Not Found", { status: 404 });
throw json("Not Found", { status: 404, statusText: "链接格式错误" });
}
const note = await fetch(`https://paul.ren/api/note/get?id=${params.id}&year=${params.year}`).then((res) => res.json()) as API.Response<API.Note.INoteData>;
if (note.status === "Failed") {
throw json("Not Found", { status: 404 });
throw json("Not Found", { status: 404, statusText: note.msg });
}
return json(note);

14
app/types/api.page.d.ts vendored Normal file
View File

@ -0,0 +1,14 @@
declare namespace API {
namespace Page {
interface IPageData {
id: number
title: string
desc: string
slug: string
content: string
hidden: boolean
created_at: string
updated_at: string
}
}
}

View File

@ -1,5 +1,5 @@
// 站点名称
export const siteTitle = (title?: string) => {
export const siteTitle = (title?: string | number) => {
const siteName = import.meta.env.APP_SITENAME;
return title ? `${title} - ${siteName}` : siteName;