Skip to content

perf(server): dev 起動・初回アクセスを計測・改善 - #601

Merged
hmjn023 merged 2 commits into
developfrom
fix/issue-600-dev-startup-performance
Jul 14, 2026
Merged

perf(server): dev 起動・初回アクセスを計測・改善#601
hmjn023 merged 2 commits into
developfrom
fix/issue-600-dev-startup-performance

Conversation

@hmjn023

@hmjn023 hmjn023 commented Jul 14, 2026

Copy link
Copy Markdown
Owner

概要

Issue #600 の dev server 起動・初回アクセスを隔離環境で計測し、初期化と worker 起動経路を整理します。

Closes #600

変更内容

  • TanStack Start を route tree / code splitting の唯一の owner に統一
  • dev oRPC の server-only import を初回 RPC まで遅延
  • worker / maintenance を成功した 2xx RPC の完了後に 1 回だけ起動
  • SSR の media / thumbnail 経路が dev worker を二重起動しないよう service 初期化を分離
  • 隔離 runtime を使う dev startup measurement と cold SSR / hydration / RPC の計測を追加
  • E2E fixture を measurement と共有し、計測手順を設計ドキュメントに記録

検証

  • bun run test
  • bun run --cwd apps/server typecheck
  • bun run --cwd apps/server lint
  • bun run --cwd apps/server build
  • bun run --cwd apps/server test:e2e:dev -- route-reload.spec.ts
  • bun run --cwd apps/server test:e2e:production -- route-reload.spec.ts
  • bun run --cwd apps/server measure:dev-startup

Summary by CodeRabbit

  • 新機能

    • 開発サーバーの起動時間や初回表示、RPC応答を計測できる機能を追加しました。
    • E2Eテストおよび計測用の隔離ランタイムを導入しました。
    • 計測プロファイル(通常、mkcert無効、DevTools無効)を選択できるようになりました。
  • 改善

    • 開発環境での初期化処理を最適化し、初回RPC成功後にバックグラウンド処理を開始するよう調整しました。
  • ドキュメント

    • 開発サーバー計測の手順、指標、代表的な結果を追加しました。

Consolidate route generation, defer dev RPC initialization, and add isolated cold-start measurements.
@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@hmjn023, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 38 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 50e3c658-5539-4c1a-8986-f07d5d9817b5

📥 Commits

Reviewing files that changed from the base of the PR and between 6983e8c and 52f6a65.

📒 Files selected for processing (1)
  • docs/design/web-rendering-strategy.md
📝 Walkthrough

Walkthrough

開発サーバーの起動計測機能を追加し、隔離ランタイム、ブラウザ計測、RPC遅延ロード、ワーカー起動制御、route tree生成設定、計測ドキュメントを更新しました。

Changes

開発サーバー起動計測

Layer / File(s) Summary
隔離ランタイム準備
apps/server/scripts/isolated-runtime.ts, apps/server/scripts/e2e-server.ts
隔離ディレクトリ、設定、DBフィクスチャ、画像、サムネイル、route treeプレースホルダを生成し、E2Eサーバーから共通利用します。
ブラウザ計測フロー
apps/server/scripts/measure-dev-startup-schema.ts, apps/server/scripts/measure-dev-startup-browser.ts
静的アセット、SSR、hydration、画面操作、RPC応答、ブラウザエラーを計測し、Zodで結果を検証します。
計測オーケストレーション
apps/server/scripts/measure-dev-startup.ts, apps/server/package.json
計測プロファイル、隔離環境、サーバー・ブラウザプロセス、ログマイルストーン、順序制約、JSON出力、後処理を実装します。
開発サーバーとルート初期化
apps/server/vite.config.ts, apps/server/src/infrastructure/server-route-bootstrap.ts, apps/server/src/routes/api/*
分離開発設定、遅延RPCロード、成功RPC後のワーカー開始、開発時のルート初期化を更新します。
計測手順と結果の記録
docs/design/web-rendering-strategy.md
計測コマンド、比較プロファイル、出力指標、代表値、route treeとワーカー起動の制約を追加します。

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Runner as measure-dev-startup.ts
  participant Server as bun run dev
  participant Browser as measure-dev-startup-browser.ts
  participant RPC as devOrpcNodeMiddlewarePlugin
  Runner->>Server: 隔離環境と環境変数で起動
  Runner->>Browser: base URL と開始時刻を渡す
  Browser->>Server: 静的アセットと /config を取得
  Browser->>RPC: config.get RPC を実行
  RPC-->>Browser: RPC応答
  Browser-->>Runner: BrowserMeasurement JSON
  Server-->>Runner: ログマイルストーン
Loading

Possibly related PRs

  • hmjn023/solid-imager#595: E2Eサーバーのブートストラップと隔離ランタイム準備に関連します。
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed タイトルは dev 起動・初回アクセスの計測と改善という変更の主旨を簡潔に表しています。
Linked Issues check ✅ Passed 主要な計測区間の追加、TanStack Start への route-tree 集約、dev oRPC の遅延化が #600 の要件に合致しています。
Out of Scope Changes check ✅ Passed 追加されたスクリプト、隔離ランタイム、計測、ドキュメントはいずれも #600 の目的に沿っています。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/issue-600-dev-startup-performance

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
apps/server/vite.config.ts (1)

113-141: 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

getDevRpcHandler が失敗したPromiseを永続キャッシュしリトライ不能になる

devRpcHandlerPromise ??= loadDevRpcHandler(); は、初回の動的import/初期化が一時的な理由(HMR中の一時的なモジュール解決失敗など)で失敗した場合でも、reject済みのPromiseをそのままキャッシュし続けます。??= はundefinedの場合のみ再代入するため、以降のすべてのRPCリクエストが同じ失敗を再現し、開発サーバーを再起動するまで復旧できません。

🛠️ 提案: 失敗時にキャッシュをリセットする
 function getDevRpcHandler(): ReturnType<typeof loadDevRpcHandler> {
-	devRpcHandlerPromise ??= loadDevRpcHandler();
+	devRpcHandlerPromise ??= loadDevRpcHandler().catch((error) => {
+		devRpcHandlerPromise = undefined;
+		throw error;
+	});
 	return devRpcHandlerPromise;
 }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@apps/server/vite.config.ts` around lines 113 - 141, Update getDevRpcHandler
so a rejected loadDevRpcHandler promise clears devRpcHandlerPromise before
propagating the error, allowing subsequent requests to retry initialization;
preserve caching for successful resolutions.
🧹 Nitpick comments (2)
apps/server/scripts/measure-dev-startup-browser.ts (1)

112-116: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

SSR成功判定を文言依存から独立させる余地

ssrHtml.includes('Save Changes') はUI文言に依存しています。文言変更時にすぐ気づける設計(明確なエラーを投げる)にはなっていますが、data-testid等の安定したマーカーに置き換えるとUI copy変更の影響を受けにくくなります。

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@apps/server/scripts/measure-dev-startup-browser.ts` around lines 112 - 116,
Replace the UI-text check in the SSR response validation near ssrHtml and
firstSsrHtmlAtMs with a stable selector or data-testid marker for the settings
form. Keep throwing a clear error when that marker is absent, without relying on
the “Save Changes” copy.
apps/server/scripts/isolated-runtime.ts (1)

147-181: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick win

prepareIsolatedRuntime に安全性チェックを内蔵すべき

prepareIsolatedRuntime は呼び出し側が事前に assertSafeRuntimeDir を呼ぶことを前提に、無条件で rm(runtimeDir, { recursive: true, force: true }) を実行します。現在の呼び出し元(e2e-server.tsmeasure-dev-startup.ts)はいずれも正しく事前チェックしていますが、この関数自体は破壊的操作を伴う共有APIであるため、将来の呼び出し元がチェックを忘れた場合に意図しないディレクトリを再帰削除するリスクがあります。

♻️ 提案: 内部でも検証する
 export async function prepareIsolatedRuntime(
 	runtimeDir: string,
+	allowedRuntimeRoot: string,
 ): Promise<IsolatedRuntime> {
+	assertSafeRuntimeDir(runtimeDir, allowedRuntimeRoot);
 	await rm(runtimeDir, { recursive: true, force: true });
 	await mkdir(runtimeDir, { recursive: true });
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@apps/server/scripts/isolated-runtime.ts` around lines 147 - 181,
prepareIsolatedRuntime の冒頭で、再帰削除を実行する前に assertSafeRuntimeDir(runtimeDir)
を呼び出して内部検証を行ってください。既存の呼び出し側による事前チェックには依存せず、検証後にのみ
rm、mkdir、設定書き込み、seedMediaFixtures を続行させます。
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@apps/server/scripts/measure-dev-startup.ts`:
- Around line 241-249: runMeasurement の runtimeDir 生成を更新し、Vite transform cache
の影響を比較できるよう、毎回一意な runtimeDir を作るモードに加えて同一 runtimeDir を再利用するフラグ、または cacheDir
のみを固定する切り替えを追加してください。既存の安全性検証と通常のコールドスタート計測は維持し、キャッシュ比較時は実行間で対象キャッシュが保持されるようにします。

In `@docs/design/web-rendering-strategy.md`:
- Around line 146-147: Update the worker/maintenance startup condition in the
TanStack Start development flow to explicitly state that it begins exactly once
after completion of the first RPC response with a 2xx status. Replace the
ambiguous “successful RPC response” wording while preserving the existing
lazy-import and single-start behavior.

---

Outside diff comments:
In `@apps/server/vite.config.ts`:
- Around line 113-141: Update getDevRpcHandler so a rejected loadDevRpcHandler
promise clears devRpcHandlerPromise before propagating the error, allowing
subsequent requests to retry initialization; preserve caching for successful
resolutions.

---

Nitpick comments:
In `@apps/server/scripts/isolated-runtime.ts`:
- Around line 147-181: prepareIsolatedRuntime の冒頭で、再帰削除を実行する前に
assertSafeRuntimeDir(runtimeDir)
を呼び出して内部検証を行ってください。既存の呼び出し側による事前チェックには依存せず、検証後にのみ
rm、mkdir、設定書き込み、seedMediaFixtures を続行させます。

In `@apps/server/scripts/measure-dev-startup-browser.ts`:
- Around line 112-116: Replace the UI-text check in the SSR response validation
near ssrHtml and firstSsrHtmlAtMs with a stable selector or data-testid marker
for the settings form. Keep throwing a clear error when that marker is absent,
without relying on the “Save Changes” copy.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 6d75ccc1-a111-4287-a11c-47614bec4b8c

📥 Commits

Reviewing files that changed from the base of the PR and between 62c48ac and 6983e8c.

📒 Files selected for processing (12)
  • apps/server/package.json
  • apps/server/scripts/e2e-server.ts
  • apps/server/scripts/isolated-runtime.ts
  • apps/server/scripts/measure-dev-startup-browser.ts
  • apps/server/scripts/measure-dev-startup-schema.ts
  • apps/server/scripts/measure-dev-startup.ts
  • apps/server/src/infrastructure/server-route-bootstrap.ts
  • apps/server/src/routes/api/rpc.$.ts
  • apps/server/src/routes/api/sources.$mediaSourceId.$mediaId.ts
  • apps/server/src/routes/api/sources.$mediaSourceId.thumbnail.$mediaId.ts
  • apps/server/vite.config.ts
  • docs/design/web-rendering-strategy.md

Comment thread apps/server/scripts/measure-dev-startup.ts
Comment thread docs/design/web-rendering-strategy.md Outdated
@hmjn023
hmjn023 merged commit 3f16b2e into develop Jul 14, 2026
1 check passed
@hmjn023
hmjn023 deleted the fix/issue-600-dev-startup-performance branch July 14, 2026 13:47
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.

1 participant