Skip to content

frontend: Server Component の React Query prefetch パターンを共通化する - #129

Merged
f0reachARR merged 7 commits into
mainfrom
issue-113-prefetch-commonize
May 15, 2026
Merged

frontend: Server Component の React Query prefetch パターンを共通化する#129
f0reachARR merged 7 commits into
mainfrom
issue-113-prefetch-commonize

Conversation

@f0reachARR

Copy link
Copy Markdown
Owner

概要

Server Component (page.tsx) に分散していた React Query の prefetch ボイラープレートを共通ヘルパーに抽出するリファクタリングです。挙動変更はありません。

Closes #113

要件

  • primary fetch + notFound 判定の helper を作成する
  • secondary prefetch の重複を減らす
  • page.tsx はページに必要なデータ宣言とレイアウトに集中させる
  • Streaming SSR / hydration の既存挙動を維持する
  • pnpm typecheckpnpm lint が通る

調査結果

対象ページ 8 ファイルで以下の定型コードが繰り返されていた:

  • getQueryClient() + createServerClient() の初期化 (全8ファイル)
  • $api.queryOptions() で options 定義 → serverClient.GET() で queryFn を手書き (path/params の二重記述)
  • secondary: prefetchQuery() で queryFn 手書き (全8ファイル、計12箇所)
  • HydrationBoundary state={dehydrate(queryClient)} ラッパー (全8ファイル)

実装概要

新規ファイル

boardflow/src/lib/api/server-prefetch.ts — 3つのヘルパー関数:

関数 役割
fetchPrimary<T> primary resource を fetchQuery し、取得失敗なら notFound()
prefetchSecondary secondary resource を prefetchQuery (await しない → Streaming SSR 維持)
withServerFetcher<T> $api.queryOptions() の queryKey と serverClient.GET の queryFn を合成

変更ファイル (8ページ)

ファイル パターン
repositories/page.tsx A: prefetchSecondary のみ
repositories/[repositoryId]/page.tsx B: fetchPrimary + prefetchSecondary
boards/[boardProjectId]/page.tsx B
boards/[boardProjectId]/runs/page.tsx B
settings/tokens/page.tsx B
runs/[boardRunId]/page.tsx C: fetchPrimary + 3x prefetchSecondary
runs/[boardRunId]/diff/page.tsx D: prefetchSecondary のみ
runs/[boardRunId]/checks/[checkKind]/page.tsx D

変更しないファイル

  • クライアントコンポーネント、API クライアント、QueryClient 設定は一切変更なし

テスト結果

チェック 結果
pnpm typecheck
pnpm lint
pnpm build

挙動維持の確認

  • Streaming SSR: prefetchSecondary は await しないため、secondary リソースの Streaming SSR 挙動は変更前と同一
  • notFound タイミング: fetchPrimary は同期的に await + notFound() するため、primary リソース不在時の 404 タイミングは変更なし
  • HydrationBoundary: 全ページで HydrationBoundary state={dehydrate(queryClient)} パターンを維持
  • エラーメッセージ: secondary の withServerFetchererrorMessage を明示指定し、ログメッセージが失われないことを確認

更新ドキュメント

  • docs/frontend/summary.md — prefetch パターンの記述を実装に合わせて更新 (共通ヘルパーの追記、primary error handling の実態記述に修正)
  • docs/logs/113/worklog.md — 作業ログ

外部調査メモ

  • Next.js Server Components + React Query の prefetch パターン分析済み (session memory 参照)

残リスク

なし。純粋なリファクタリングで Streaming SSR / hydration / notFound のタイミングは変更前と同一。

レビュー / ドキュメント確認

  • pr_ready: true (review エージェント確認済み)
  • docs_ready: true (docs エージェント確認済み)

…ndary, withServerFetcher)

Deduplicate React Query server-side prefetch patterns across 8 page
components into shared helpers in lib/api/server-prefetch.ts.

No behavioral changes — Streaming SSR, hydration, and notFound
semantics remain identical.

Closes #113
Add optional errorMessage parameter to withServerFetcher so that
secondary prefetch failures throw Error with the same descriptive
messages as before the refactoring. Primary fetches remain unchanged
since fetchPrimary catches errors via .catch(() => null).
@f0reachARR
f0reachARR merged commit 841f85b into main May 15, 2026
2 checks passed
@f0reachARR
f0reachARR deleted the issue-113-prefetch-commonize branch May 15, 2026 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

frontend: Server Component の React Query prefetch パターンを共通化する

1 participant