使用方式:簡單來說,這是基於 next 14 官網資料的 ai 聊天機器人,有想知道的就可以直接問它,但資料量不多,所以需要搭配這篇重點整理去問,例如:
next js 的資料夾結構?
next js 如何處理個人驗證?

資料夾結構

- /app: 主要的資料夾
- /app/lib: 放hooks, utils
- /app/ui: UI 元件
- /public: 圖片 靜態資源
- /scripts/: 資料庫
- global.css: reset.css 全域 css 規則
- 條件判斷的 css 可用 clsx 套件
- 字型 font.ts
- 圖片 < Image> 元件
- 如果要優化 外部圖片 or 本地字型 看這篇最下面
路由
-
巢狀路由

-
以 page.tsx 為主軸,例如 /dashboard/page.tsx => 頁面 /dashboard
-
/dashboard 中所有 page 的共用元件,則放在 /dashboard/layout.tsx,dashboard 的子頁面時,共用元件就不需要 re-render (partial-rendering)
-
< Link> 元件 切換頁面時有 SPA 的效果,而且在載入 Link 元件時,會同步 prefetch 他的對應頁面,達到效能優化的效果。
-
usePathname 或任何 react 的 hook 都需要在 'use client' 環境使用
-
可以使用 clsx 套件做 active 的樣式
fetching data
- 使用 React Server Components (server 端接資料),則不需要 API layer
- 如果在 client 端皆資料的話,則需要 API layer
在 server 端撈 sql 的資料步驟
/lib/data.ts
/dashboard/page.tsx
避免 request waterfalls

可利用 Parallel data fetching 解決上述問題 提升效能
骨架屏
-
整頁骨架屏:搭配loading.tsx實作 /dashboard 頁面專屬的骨架屏 (必須放在(overview:檔名隨便)的資料夾中)

-
元件骨架屏:為了避免 request waterfalls,將元件拆細,便可做到 code split 的效果,並且利用 react Suspense 元件做出骨架屏的效果
- You could stream the whole page like we did with loading.tsx… but that may lead to a longer loading time if one of the components has a slow data fetch.
- You could stream every component individually… but that may lead to UI popping into the screen as it becomes ready.
- You could also create a staggered effect by streaming page sections. But you'll need to create wrapper components.
利用 URL 實作搜尋功能
- usePathname() => 取得全部網址
- useSearchParams() => 取得 query
- useRouter() => 切換搜尋列表網址並有 SPA 的效果
- URLSearchParams => web API
- use-debounce => debounce 套件
- 利用上述 client 端取得 server 端的參數 去搜尋資料庫中的資料
參考這頁 adding-search-and-pagination
CRUD
利用 React Server Actions 建立方法 createInvoice
React Server Actions 會創建 post 請求的 api 節點,所以我們不需要手動創建
並且利用 zod 檢查表單型別(server端)
- 利用react server actions 實作新建資料的邏輯
- 在頁面引入後 利用action的 props 觸發方法
<form action={createInvoice}>
處理錯誤
- 使用 try catch 處理 server 端的錯誤
- 使用 error 元件處理所有錯誤頁面(必須是 client component)
- 使用 notFound(), not-found 元件處理401錯誤
表單檢查
- require attr
- server 端使用 zod 驗證表單
isAuth 檢查
- 使用 next-auth
- 被保護的路由只有在 middleware 驗證通過才會 render
- bcrypt 套件依賴 Node.js (產生密碼用的)
結論
優點:
- 提供了許多好用的 api, component 包含路由、圖片優化等
- 活用了 react server component 的用法
- 用資料夾來決定路由的方式在小型專案的情況下非常方便
- 這次新推出的官方課程算是簡短好上手,相當推薦嘗試看看
缺點:
- 雖然狀態管理一樣推薦用 redux,但是 Server Components 是一种新的實驗性功能,需要重新考慮如何管理和共享狀態
- 儘管也有管理元件的概念,但”資料夾決定路由“這件事情在大型專案下的維護性值得觀察