feat(structure): compact archive + tag pages + hero/featured, and fix site search
Deploy / build (push) Failing after 13m52s

Found a real bug while verifying the nav search: /pagefind/pagefind.js is an ES
module that defines no window.Pagefind, but Search.astro loaded it with a classic
<script src> and read window.Pagefind — so every query answered "Search is
unavailable." (confirmed live before the fix). Now a dynamic import; the specifier
must be a variable or Rollup resolves it at build time and the build fails.

Measured before/after in a real browser:

- archive /posts/ + /zh/posts/: 18,220px -> 3,788px (71 rows at 44px, grouped by
  year) with a client-side filter (title/tags/description text, category chips,
  14 tag chips, zero requests). Tested: "nginx" -> 6 rows, chip devops -> 28.
- tag pages for tags with >= 3 posts. Measured 235 distinct tags (an earlier brief
  claimed 74), 156 used exactly once — pages only where a topic exists: 40 tags ->
  80 pages, build 165 -> 245 pages. Pills link only when a page exists.
- homepage + ZH homepage: hero (288px) with the real scale claim, three CTAs, a
  fixed Featured 4 (scale -> automation -> observability -> a delivered result),
  Latest 10 -> 6. ZH hero had linked "关于我" to the English /about/.
- long code blocks collapse above 420px with an Expand button + gradient, copy
  button still copies the whole block (verified on the 1010px block).
- search is visible ("⌕ Search /" pill, "/" plus the existing Ctrl/Cmd+K) and the
  zero-result screen offers topic chips instead of a dead end.

Docs: project-state B3 entry, ops-runbook Pagefind gotcha.
This commit is contained in:
hoelee committed 2026-10-16 21:18:25 +08:00
1 parent 8619e48271
commit f4008d9e8b
15 files changed
+669 -60

No files matched your search

+44 -15
View File
@@ -11,6 +11,13 @@ const placeholder =
lang === 'zh' ? '搜索文章…' : 'Search posts…';
const noResults =
lang === 'zh' ? '没有匹配的结果。' : 'No results.';
// Empty-search state: a dead end helps nobody. Offer the topic entry points that
// actually exist (tag pages are generated for tags with >= 3 posts).
const popular = ['docker', 'self-hosting', 'nginx', 'monitoring', 'automation'];
const popularHref = (t: string) => (lang === 'zh' ? `/zh/tags/${t}/` : `/tags/${t}/`);
const tryLabel = lang === 'zh' ? '试试这些主题:' : 'Try a topic:';
const archiveLabel = lang === 'zh' ? '浏览全部文章' : 'Browse all posts';
const archiveHref = lang === 'zh' ? '/zh/posts/' : '/posts/';
---
<div class="search-overlay" id="search-overlay" hidden>
@@ -28,6 +35,16 @@ const noResults =
</div>
<div class="search-results" id="search-results"></div>
</div>
<!-- Dead-end screen for a search with no hits: real links, rendered at build time. -->
<template id="search-empty-tpl">
<p class="excerpt" data-role="message">{noResults}</p>
<p class="excerpt" data-role="try">{tryLabel}</p>
<p class="search-empty-chips">
{popular.map((t) => <a class="chip" href={popularHref(t)}>{t}</a>)}
<a class="chip" href={archiveHref}>{archiveLabel}</a>
</p>
</template>
</div>
<style>
@@ -88,6 +105,17 @@ const noResults =
.search-result:hover { color: var(--brand); }
.search-result h3 { margin: 0 0 0.2rem; font-size: 1.05rem; }
.search-result .excerpt { color: var(--muted); font-size: 0.88rem; margin: 0; }
.search-empty-chips { display: flex; flex-wrap: wrap; gap: 0.4rem; margin-top: 0.5rem; }
.search-empty .chip {
color: var(--muted);
background: var(--surface);
border: 1px solid var(--border);
border-radius: 999px;
padding: 0.22rem 0.6rem;
font-size: 0.78rem;
text-decoration: none;
}
.search-empty .chip:hover { color: var(--brand); border-color: var(--brand); }
.search-result mark {
background: color-mix(in srgb, var(--brand) 25%, transparent);
color: var(--ink);
@@ -140,17 +168,17 @@ const noResults =
async function loadPagefind() {
if (pagefind) return pagefind;
// Pagefind's UI/JS is copied into /pagefind/ post-build. Load it via a
// script tag (not a dynamic import) so Vite leaves the URL untouched.
await new Promise<void>((resolve, reject) => {
const s = document.createElement('script');
s.src = '/pagefind/pagefind.js';
s.onload = () => resolve();
s.onerror = () => reject(new Error('pagefind load failed'));
document.head.appendChild(s);
});
const w = window as unknown as { Pagefind?: Pagefind };
pagefind = w.Pagefind;
// /pagefind/pagefind.js is copied into the build post-build. It is an **ES module**
// (`export { search, debouncedSearch, … }`) and defines NO window global, so loading
// it with a classic <script src> leaves window.Pagefind undefined and every search
// answered "Search is unavailable." — which is what happened between the search
// shipping and this fix. Import it as a module instead; @vite-ignore keeps Vite from
// trying to resolve a path that only exists in dist/.
// The specifier must be a VARIABLE: Rollup resolves a literal dynamic-import path
// at build time (and @vite-ignore does not stop it), which breaks the build because
// /pagefind/ only exists in dist/ after the index is generated.
const url = '/pagefind/pagefind.js';
pagefind = (await import(/* @vite-ignore */ url)) as unknown as Pagefind;
return pagefind;
}
@@ -169,10 +197,11 @@ const noResults =
const res = await search;
results.innerHTML = '';
if (!res.results.length) {
const empty = document.createElement('p');
empty.className = 'excerpt';
empty.textContent = 'No results.';
results.appendChild(empty);
// Clone the build-time template: the links cannot drift from the pages that
// actually exist (tag pages are only generated for tags with >= 3 posts).
results.innerHTML = '';
const tpl = document.getElementById('search-empty-tpl') as HTMLTemplateElement | null;
if (tpl) results.appendChild(tpl.content.cloneNode(true));
return;
}
for (const r of res.results.slice(0, 12)) {