Skip to content
Go back

Fixing trailing slash issues with Pagefind and Caddy

| Last edited: Immutable-ish

Pagefind integrates well with Astro, but the search page wasn’t working. The UI rendered, but no results ever loaded. There were no build errors, and the data appeared to be generated correctly in the public/pagefind/ directory.

The issue was in the browser console and Docker logs: requests to Pagefind files like /pagefind/wasm.en.pagefind/ were failing with 404s. The trailing slash was the problem.

Here’s what we saw in the Docker logs:

"GET /pagefind/pagefind.en_c4ae6099d2.pf_meta/ HTTP/1.1" 404
"GET /pagefind/wasm.en.pagefind/ HTTP/1.1" 404

And in the console:

Failed to load resource: the server responded with a status of 404 () https://test.andrewhoog.com/pagefind/wasm.en.pagefind/
Failed to load the meta index:
  Error: invalid gzip data
    (anonymous function) -- pagefind.js:901

Astro Pagefind errors: invalid gzip data

Why it happened

We had this redirect rule in our Caddyfile to normalize URLs by adding a trailing slash:

@missingSlash {
        not path_regexp hasExt \.(css|js|ico|png|jpg|jpeg|svg|webp|xml|txt|json|html|pdf|woff2?)$
        not path /robots.txt /favicon.ico /pagefind
        not method POST
        not path */
}
redir @missingSlash {uri}/ 308

That worked fine for most site content but broke Pagefind, which expects exact file paths with no trailing slash. Even though we excluded /pagefind, it wasn’t enough.

The fix

We added specific exclusions for the Pagefind files to the not path rule:

	# Normalize URLs: add trailing slash if missing (except for files)
	@missingSlash {
		not path_regexp hasExt \.(css|js|ico|png|jpg|jpeg|svg|webp|xml|txt|json|html|pdf|woff2?)$
		not path /robots.txt /favicon.ico
		not path /pagefind* /.pagefind*
		not method POST
		not path */
	}
	redir @missingSlash {uri}/ 308

Astro Pagefind working


Share this post on:

Previous Post
Updating and deploying Docker hosted website from GitHub Actions
Next Post
My new Apple Watch goal