perf(web): 起動時に一覧の取得をセッションの確認と並べて始める - #213
Merged
Merged
Conversation
/recipes の loader で一覧の最初のページを先に取る。画面の描画はこれまで どおり session の確定を待つ。API は requireAuth で自ら session を確かめる ので、未ログインなら 401 が返るだけで画面には出ない。 - 一覧の取得設定を recipeListQueryOptions にまとめ、画面と loader で共有する - router の context で queryClient を渡し、router は main.tsx で作る - URL にタグの指定があるときは先に取らない(消えたタグの id を外してから取るため) - 先に取るときは 401 を再試行しない Refs #202
起動時の一覧は常に絞り込みなしで、絞り込んだ一覧は画面が自分で取るのと 同時になるので、先に取っても縮まない。条件の組み立てを loader と画面で 二重に持たないよう、先取りを絞り込みなしに限る。 Refs #202
401 は取り直しても変わらず、session の回復は viewer の経路が担う(ADR 0011)。 一覧の先取りだけに置いていた 401 の除外を、既定の再試行に移す。 Refs #202
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概要
起動時、一覧(
/recipes)の取得をget-sessionの完了を待たずに始めます。画面の描画はこれまでどおり session の確定を待ちます(ADR 0011)。ブラウザと Worker の往復が直列に 1 回減ります。Closes #202
背景
ProtectedLayoutは session が確定するまで<Outlet />を描かず、一覧のuseInfiniteQueryはその中にあるため、GET /api/recipesはget-sessionの応答のあとに始まっていました。#202 の起票後、Worker を
aws:ap-southeast-1に置いた(#203)ことで DB 1 段の待ちは小さくなり(#205 の計測で cache 切れと有効の差は 6〜20ms)、起動時の待ちの大半はブラウザと Worker の往復(日本から cookie なしのget-sessionで TLS 確立後 約 160〜190ms)が 2 回直列に並ぶことによるものになっています。先に取っても、cache が切れていれば
/api/recipesは自分で session を 2 段引くので、クリティカルパス上の DB の段数は変わりません。縮むのは往復 1 回分と、その間の描画です。Worker の配置を日本側に戻しても遅くはならず、短縮幅が小さくなるだけです。変更
queryFn、staleTime5 分など)をrecipeListQueryOptionsにまとめ、画面と loader で共有するqueryClientを渡し、router はmain.tsxで作る/recipesの loader で一覧の最初のページをprefetchInfiniteQueryする。Promise は返さない(返すと router が完了を待つ)isUnauthorizedErrorをlib/api.tsに置き、protected-access.tsの同じ判定を置き換える未ログインで
/recipesを開くと、/api/recipesが 401 を 1 回返してからログインへ移ります。失敗は一覧のキャッシュに残りますが、ログイン後に画面が mount すると取り直します(retryOnMount)。起動時は
get-sessionと/api/recipesがそれぞれ DB で session を引き、それぞれが cookie cache を配ります(session の照会が 1 本増える)。計測
ローカルのモック(API 遅延 1000ms、
/recipesを開き直した回)で、変更前のコミットと比較。get-session/api/recipesの開始本番での所要時間は未計測です。現在の配置での見込みは、往復 1 回分の約 170ms です。
検証
pnpm typecheckpnpm lintpnpm test(全パッケージ)router.test.tsx):get-sessionの応答を止めた状態で一覧の取得が 1 回出て、session の確定後にその結果を取り直さずに描画する。loader の先取りを外すと失敗することを確認済みgit diff --check