Skip to content

hotfix(v1.2): preview-404-fallback plugin の / と /privacy/ 404 副作用修正#200

Merged
shunei-web merged 1 commit into
v1.2from
hotfix/199-preview-404-fallback-directory-fix
May 1, 2026
Merged

hotfix(v1.2): preview-404-fallback plugin の / と /privacy/ 404 副作用修正#200
shunei-web merged 1 commit into
v1.2from
hotfix/199-preview-404-fallback-directory-fix

Conversation

@shunei-web
Copy link
Copy Markdown
Collaborator

概要

PR #199 で実装した preview-404-fallback plugin に副作用が発覚。QC リトライで検出。

問題

URL 期待 PR #199 実際
/ 200 404
/privacy/ 200 404
/nonexistent 404 404 ✅

根本原因

Vite の middleware 実行順:

viteAssetMiddleware → htmlFallbackMiddleware(MPA) → postHooks(カスタム) → indexHtmlMiddleware → notFoundMiddleware

PR #199 の実装は「post-hook は Vite 処理後の未処理のみ届く」前提だったが誤り。
カスタム middleware は indexHtmlMiddleware より前に動くため、存在チェックなしで全リクエストを飲み込んでいた。

修正内容

既存ファイル / ディレクトリ存在チェックを追加:

const candidates = [
  resolve(distDir, url.slice(1)),              // dist/{url}(ファイル / ディレクトリ)
  resolve(distDir, url.slice(1), 'index.html'), // dist/{url}/index.html
];

for (const candidate of candidates) {
  if (fs.existsSync(candidate)) {
    return next(); // 存在する → Vite に委譲
  }
}
// 存在しない → 404.html 返却

動作確認(全 11 URL)

URL 期待 結果
/ 200 ✅ 200
/index.html 200 ✅ 200
/privacy/ 200 ✅ 200
/privacy/index.html 200 ✅ 200
/404.html 200 ✅ 200
/assets/main-BYe0Lm16.css 200 ✅ 200
/assets/main-1c-sa6K-.js 200 ✅ 200
/scripts/viewport.js 200 ✅ 200
/nonexistent 404 ✅ 404
/nonexistent/deep 404 ✅ 404
/nonexistent タイトル ページが見つかりません — iluha ✅ 一致

lint / build

  • pnpm run lint:js: PASS
  • pnpm run lint:css: PASS
  • pnpm run lint:html: PASS
  • pnpm build: PASS

関連

🤖 Generated with Claude Code
Co-Authored-By: Claude Sonnet 4.6 noreply@anthropic.com

PR #199 の simpler approach(post-hook で未処理のみ届く前提)が誤り。
Vite 8.x の middleware 順序で indexHtmlMiddleware より前に動き、
全リクエストを飲み込むため / と /privacy/ も 404 化していた。

修正: 既存ファイル / ディレクトリ存在チェック追加
- dist/{url}(ファイル直接 or ディレクトリ)が存在 → next() で Vite に委譲
- dist/{url}/index.html が存在 → next() で Vite に委譲
- どちらも存在しない → 404.html を 404 ステータスで返す

QC リトライで FAIL 検出 → 全 URL バリエーション(11件)動作確認で網羅性確保。

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@cloudflare-workers-and-pages
Copy link
Copy Markdown

Deploying starter with  Cloudflare Pages  Cloudflare Pages

Latest commit: 2706338
Status: ✅  Deploy successful!
Preview URL: https://5cf17fd1.starter-20t.pages.dev
Branch Preview URL: https://hotfix-199-preview-404-fallb.starter-20t.pages.dev

View logs

@shunei-web shunei-web merged commit b04d67b into v1.2 May 1, 2026
2 checks passed
@shunei-web shunei-web deleted the hotfix/199-preview-404-fallback-directory-fix branch May 1, 2026 02:56
shunei-web added a commit that referenced this pull request May 4, 2026
#205)

PR #199 / #200 で `vite.config.ts` の preview-404-fallback plugin は実装済
だったが、Issue #198 の完了条件「README または CONTRIBUTING に 404 動作明記」
が未完遂(PR body には記載があったが実 commit に含まれていなかった partial
misreport)のため補完。

## 変更内容

- README.md: 「ビルドと納品」末尾に「404 ページの動作確認」セクション追加。
  本番ホスティング(Cloudflare Pages / Netlify / Vercel / GitHub Pages 共通)
  + ローカル preview + 直接アクセスの 3 確認パターンを記載。
- CONTRIBUTING.md: 「基本フロー」に `pnpm run preview` 動作確認手順を追加し、
  README の該当節へリンク。
- CHANGELOG.md: Unreleased Added に preview-404-fallback plugin の動作を
  追記(PR #199 / #200 で更新漏れ)。

## 実機検証

`npm run build && npm run preview` 後の動作確認:

| URL | 期待 | 実測 |
|-----|------|------|
| `/` | 200 | ✅ 200 |
| `/privacy/` | 200 | ✅ 200 |
| `/nonexistent` | 404 | ✅ 404 |
| `/404.html`(直接) | 200 | ✅ 200 |
| `/assets/notfound.css` | 404 | ✅ 404 |

Closes #198
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