Cloudflare Pages deployment
Cloudflare Pages settings
Section titled “Cloudflare Pages settings”| Setting | Value | Description |
|---|---|---|
| Root directory | docs | Relative to the repository root where Astro source files live |
| Build command | npm install && npm run build | Installs dependencies and builds the site. Use npm ci once the lockfile is committed for deterministic builds. |
| Output directory | dist | Astro’s static output directory containing the generated HTML, CSS, and assets. |
| Node version | 24 | Node.js version used for the build environment. Ensure compatibility with your Astro version. |
| Production domain | docs.jampuk.com | The default production domain assigned by Cloudflare Pages. |
Environment variables
Section titled “Environment variables”The canonical URL for the site is set via the PUBLIC_DOCS_SITE environment variable. This variable is read at build time by astro.config.mjs and is also stored in wrangler.toml as a Cloudflare Pages function variable.
PUBLIC_DOCS_SITE=https://docs.jampuk.com npm run buildPUBLIC_DOCS_SITE=$CF_PAGES_URL npm run buildPUBLIC_DOCS_SITE usage
Section titled “PUBLIC_DOCS_SITE usage”astro.config.mjs: Readsprocess.env.PUBLIC_DOCS_SITEto set thesiteconfig, which propagates to sitemap generation, Open Graph tags, and canonical links.wrangler.toml: StoresPUBLIC_DOCS_SITEas a Pages function variable for runtime use (e.g., redirects, API routes)..env.example: DeclaresPUBLIC_DOCS_SITE=https://docs.jampuk.comas the production placeholder.
Note: The
PUBLIC_DOCS_SITEvalue overrides thesiteoption inastro.config.mjs. If not set, the defaulthttps://docs.jampuk.comis used.
Key configuration files
Section titled “Key configuration files”-
wrangler.toml- Cloudflare Pages metadata and variablescompatibility_date = "2026-09-01"- Ensures Node.js compatibility features are available[vars]section containsPUBLIC_DOCS_SITE = "https://docs.jampuk.com"for production- Local development: copy
.env.exampleto.envand adjust as needed
-
public/_headers- Security headers served via Cloudflare Pages- Sets
X-Frame-Options: DENY,X-Content-Type-Options: nosniff,Referrer-Policy: strict-origin-when-cross-origin Permissions-Policyrestricts camera, microphone, and geolocationStrict-Transport-Securityenforces HTTPS with a 1-year max-age- Cache control for
/_astro/*and/pagefind/*assets
- Sets
-
public/robots.txt(generated at build) - Search engine indexing rules- References the sitemap location
- Can be updated if the custom domain changes
- Excludes admin and build paths from indexing
-
astro.config.mjs- Astro configuration- Reads
PUBLIC_DOCS_SITEenv var to set thesiteconfig output: 'static'- Cloudflare Pages servesdist/directly, no adapter required- Integrates Starlight, Partytown, sitemap, and external links plugin
- Reads
Custom domain
Section titled “Custom domain”- Add
docs.jampuk.comas a custom domain on the Cloudflare Pages project dashboard. - In the Pages project settings, set the
PUBLIC_DOCS_SITEenv var tohttps://docs.jampuk.com. - Regenerate the build:
PUBLIC_DOCS_SITE=https://docs.jampuk.com npm run build. - Verify that all canonical URLs, sitemap, and Open Graph tags resolve to
https://docs.jampuk.com.
Important: The
PUBLIC_DOCS_SITEvalue must match the custom domain exactly for correct canonical URLs and sitemap generation.
Build workflow
Section titled “Build workflow”Local development
Section titled “Local development”# Copy env example if neededcp .env.example .env
# Start local dev server (defaults to http://localhost:8088)npm run dev
# Or with custom site URLPUBLIC_DOCS_SITE=https://docs.jampuk.com npm run devProduction build
Section titled “Production build”# Production build with canonical URLPUBLIC_DOCS_SITE=https://docs.jampuk.com npm run build
# Or using the shorthand for preview deploymentsPUBLIC_DOCS_SITE=$CF_PAGES_URL npm run buildTip: Commit the lockfile (
package-lock.jsonorpnpm-lock.yaml) so that remote builds usenpm cifor deterministic installs.
Preview deployments
Section titled “Preview deployments”When pushing to a pull request branch, Cloudflare Pages automatically:
- Sets
CF_PAGES_URLto the preview subdomain (e.g.,https://jampuk-docs-xyz.pages.dev) - Uses
PUBLIC_DOCS_SITE=$CF_PAGES_URLfor the build - Generates a preview sitemap and robots.txt with the preview URL
Troubleshooting
Section titled “Troubleshooting”| Issue | Resolution |
|---|---|
| Canonical URLs point to default domain | Verify PUBLIC_DOCS_SITE is set correctly in the build command or Pages env vars. |
| Sitemap contains wrong URLs | Check that PUBLIC_DOCS_SITE matches the custom domain and regenerate the build. |
| GA4 consent banner missing | Ensure PUBLIC_GA_MEASUREMENT_ID is set in .env and astro.config.mjs forwards it to Partytown. |
Build fails on npm ci | Commit the lockfile or use npm install instead for remote builds. |
| Security headers not applied | Verify public/_headers exists at the repository root and has correct format. |
Post-deployment verification
Section titled “Post-deployment verification”After a successful deployment, verify:
- Site accessibility: Visit
https://docs.jampuk.comand confirm the site loads correctly. - Canonical URL: View page source and confirm
<link rel="canonical">points tohttps://docs.jampuk.com. - Sitemap: Visit
/sitemap.xmland confirm all pages are included with correct URLs. - Security headers: Use browser dev tools or
curl -I https://docs.jampuk.comto verify headers frompublic/_headers. - robots.txt: Visit
/robots.txtand confirm sitemap reference and disallowed paths. - Custom domain DNS: Ensure CNAME record points to the Cloudflare Pages hostname.
