หน้าตัวอย่าง
ไม่พบหน้า 404
ใช้เมื่อที่อยู่ไม่ตรงกับหน้าใด หรือระเบียนที่ลิงก์ชี้ไปไม่มีแล้ว Next.js แสดง app/not-found.tsx ให้ทุกที่อยู่ที่ไม่มีหน้า และแสดง not-found.tsx ที่ใกล้ที่สุดเมื่อหน้าเรียก notFound() ถ้าผู้ใช้ไม่มีสิทธิ์ดูระเบียนที่มีอยู่จริง ใช้หน้าไม่มีสิทธิ์ 403 แทน
คอมโพเนนต์ที่ใช้
- StatusPageA page for an outcome instead of the content: the page wasn't found, the person may not open it, the server failed, or the system is closed for maintenance.
- ButtonA button, or a link that looks like one with href.
- AppShellThe application frame: the left menu, which collapses to icons on wide screens and slides in on phones, and the top bar with the breadcrumb, global search, language switch, notifications and account menu.
import { StatusPage, Button, AppShell } from "@bsru/ui";กฎของหน้านี้
ถ้อยคำ
- บอกว่าหาไม่พบ ไม่ใช่ว่าผู้ใช้ทำผิด ข้อความเริ่มต้นของ StatusPage ใช้ได้กับทุกที่อยู่
- เมื่อรู้ว่าหาอะไรไม่พบ เช่น บัญชีผู้ใช้ ให้บอกชื่อสิ่งนั้นในหัวข้อ และบอกที่ที่หาได้
ปุ่มและทางไปต่อ
- ปุ่มหลักหนึ่งปุ่ม: กลับหน้าแรก หรือกลับไปรายการที่ระเบียนนั้นอยู่
- ปุ่มรองเป็นทางเลือกเท่านั้น อย่าเกินสองปุ่ม
ห้ามแสดง
- ที่อยู่ที่ผู้ใช้พิมพ์ ชื่อไฟล์ หรือเส้นทางภายในของระบบ
- ข้อความที่บอกว่าระเบียนนั้นมีอยู่แต่ถูกซ่อน เพราะจะบอกว่ามีระเบียนนั้นอยู่จริง
ในแอป
- ก่อนเข้าสู่ระบบหรือนอกโครงแอป ใช้แบบเต็มหน้าจอ พร้อม brand และ notice ในระบบจำลอง
- ในโครงแอป ส่ง fill={false} เพื่อให้เมนูและแถบบนยังอยู่ AppShell แสดงแถบระบบจำลองเอง
- ตอบด้วยสถานะ HTTP 404 ซึ่ง Next.js ทำให้เมื่อใช้ not-found.tsx หรือ notFound()
ตัวอย่างทั้งหน้า
แต่ละตัวอย่างเป็นหน้าเต็ม แสดงในกรอบจอคอมพิวเตอร์ เลือกโทรศัพท์หรือแท็บเล็ตที่แถบบนเพื่อดูบนจอนั้น ข้อมูลทั้งหมดเป็นข้อมูลสมมติ
ERP: ที่อยู่ที่ไม่มีหน้า
app/not-found.tsx แสดงเต็มหน้าจอ ใช้หัวข้อและคำอธิบายเริ่มต้นของ StatusPage ตราลิงก์กลับหน้าแรก
// app/not-found.tsx: every address that matches no page, before the app frame.
import { Button, StatusPage } from "@bsru/ui";
import { House } from "lucide-react";
import { getTranslations } from "next-intl/server";
import { isDemo } from "@/server/env";
export default async function NotFound() {
const t = await getTranslations();
return (
<StatusPage
kind="notFound"
brand={{ name: "BSRU ERP", caption: t("brand.university"), logo: "/bsru-logo.png", alt: t("brand.seal"), href: "/" }}
notice={isDemo() ? t("shell.demo") : undefined}
actions={<Button tone="primary" icon={House} label={t("status.home")} href="/" />}
/>
);
}| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| kind* จำเป็น | "notFound" | StatusKind ค่าที่ใช้ได้: "notFound""forbidden""serverError""maintenance" | notFound for 404, forbidden for 403, serverError for 500 and the other 5xx, maintenance for a planned closure. |
| brand | ออบเจกต์ 5 ฟิลด์{
name: "BSRU ERP",
caption: "มหาวิทยาลัยราชภัฏบ้านสมเด็จเจ้าพระยา",
logo: "/bsru-logo.png",
alt: "ตรามหาวิทยาลัยราชภัฏบ้านสมเด็จเจ้าพระยา",
href: "/",
} | PageBrand | The logo and system name at the top of a whole page. Leave it out inside AppShell, which shows its own. |
| notice | "ระบบจำลอง ใช้ข้อมูลทดสอบเท่านั้น" | string | The gold strip across the top of a whole page, for demo and test systems, such as ระบบจำลอง. |
| actions | <Button tone="primary" icon={House} label="กลับหน้าแรก" href="/" /> | ReactNode | The ways forward, as Button components, with the primary one first, such as Back to the home page. |
คอนโซล: ระเบียนที่ไม่มีแล้ว ในโครงแอป
หน้าเรียก notFound() เมื่อไม่พบบัญชี not-found.tsx ของเส้นทางนั้นแสดงใน AppShell ด้วย fill={false} บอกว่าหาอะไรไม่พบ และให้กลับไปรายการ
// app/(console)/accounts/[id]/not-found.tsx: shown when the page calls notFound(), inside the console's AppShell.
import { Button, StatusPage } from "@bsru/ui";
import { ArrowLeft, House } from "lucide-react";
import { getTranslations } from "next-intl/server";
export default async function AccountNotFound() {
const t = await getTranslations();
return (
<StatusPage
kind="notFound"
fill={false}
title={t("accounts.notFound.title")}
description={t("accounts.notFound.body")}
actions={
<>
<Button tone="primary" icon={ArrowLeft} label={t("accounts.backToList")} href="/accounts" />
<Button icon={House} label={t("status.home")} href="/" />
</>
}
/>
);
}
// app/(console)/accounts/[id]/page.tsx
const account = await getAccount(id);
if (!account) notFound();| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| kind* จำเป็น | "notFound" | StatusKind ค่าที่ใช้ได้: "notFound""forbidden""serverError""maintenance" | notFound for 404, forbidden for 403, serverError for 500 and the other 5xx, maintenance for a planned closure. |
| fill | false | boolean | true fills the screen as a page of its own, with its own main landmark. false sits in AppShell's content area, in the app's main. |
| title | "ไม่พบบัญชีผู้ใช้นี้" | string | What happened, the page's level 1 heading. Defaults to a title for the kind in the screen language. |
| description | "บัญชีอาจถูกลบไปแล้ว หรือลิงก์ไม่ถูกต้อง ค้นหาบัญชีจากรายการบัญชีผู้ใช้อีกครั้ง" | string | What to do next, in a sentence or two. Defaults to a sentence for the kind in the screen language. |