Skip to content

fix: emit Pagefind under a versioned directory - #785

Open
Henglie wants to merge 1 commit into
saicaca:mainfrom
Henglie:fix/pagefind-versioned-assets
Open

Henglie wants to merge 1 commit into
saicaca:mainfrom
Henglie:fix/pagefind-versioned-assets

Conversation

@Henglie

@Henglie Henglie commented Nov 14, 2026

Copy link
Copy Markdown

Type of change

  • Bug fix (a non-breaking change that fixes an issue)
  • New feature (a non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Other (please describe):

Checklist

  • I have read the CONTRIBUTING document.
  • I have checked to ensure that this Pull Request is not for personal changes.
  • I have performed a self-review of my own code.
  • My changes generate no new warnings.

Related Issue

Fixes #774

Changes

Pagefind's browser runtime is loaded from the unversioned /pagefind/pagefind.js and defaults to /pagefind/ as its base path. Hosting providers commonly cache that file far longer than the generated index, so after a Pagefind upgrade a returning visitor can load the old runtime against the new index. Search then fails with Pagefind JS version doesn't match the version the index was built with and no visible response.

  • Move the generated bundle to dist/pagefind-<installed version>/ after pagefind --site dist runs (scripts/version-pagefind.mjs).
  • Read the installed Pagefind version in astro.config.mjs and expose it as __PAGEFIND_VERSION__.
  • In Navbar.astro, import the runtime from the versioned path and pass the same path as basePath, so the runtime and index always come from the same version.
  • Add pnpm check:pagefind (scripts/check-pagefind-versioned.mjs) to assert that every generated page references the versioned runtime and base path and that the legacy dist/pagefind directory is gone.

Example output with Pagefind 1.4.0: dist/pagefind-1.4.0/pagefind.js, const scriptUrl = "/pagefind-1.4.0/pagefind.js";, const basePath = "/pagefind-1.4.0/";.

How To Test

  • pnpm install --frozen-lockfile
  • pnpm build -> Astro 5.13.10 build succeeds, Pagefind 1.4.0 indexes 6 pages, bundle is moved to dist/pagefind-1.4.0.
  • pnpm check:pagefind -> OK: 8 page(s) reference Pagefind 1.4.0 under /pagefind-1.4.0/.
  • Headless runtime check: served dist/pagefind-1.4.0/ from a local HTTP server, imported pagefind.js, set basePath to the versioned directory, and ran search("markdown"); it returned 4 results (/posts/markdown-extended/, /posts/markdown/, /posts/video/).
  • Revert proof: with only the loader/build-script changes reverted (check script kept), pnpm build regenerates dist/pagefind/pagefind.js, and node scripts/check-pagefind-versioned.mjs exits 1 reporting the legacy path and missing versioned assets.

Not verified in this PR:

  • A real Cloudflare Pages deployment with a previously cached runtime, or a browser with a populated HTTP cache.
  • Node 22/23 (CI versions); local verification used Node 26.12.1 on Windows.
  • pnpm astro check reports 2 errors and pnpm type-check reports isolatedDeclarations errors in untouched files; both sets reproduce on unmodified main and are unrelated to this change.

Pagefind's browser runtime defaults to /pagefind/ and is often cached
longer than the index files. After an upgrade, returning visitors can
load an old runtime against a new index and search fails with a version
mismatch.

Move the generated bundle to /pagefind-<installed version>/ and point
both the script import and the Pagefind basePath at it, so a version
change always yields a new URL. Add a build check that asserts generated
pages reference the versioned bundle.
@vercel

vercel Bot commented Nov 14, 2026

Copy link
Copy Markdown

@Henglie is attempting to deploy a commit to the zephyirdgmailcom's projects Team on Vercel.

A member of the Team first needs to authorize it.

This branch has not been deployed

No deployments
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.

[Bug]: Cached unversioned Pagefind assets break search after Pagefind upgrades

1 participant