I did some more careful reading and building sample projects. And either there is a bug or cloudflar

I did some more careful reading and building sample projects. And either there is a bug or cloudflare needs to update their documentation about deploying an astro site. My observations:

  1. if you deploy an astro site using the astro cloudflare plugin (SSR), you cannot use the /functions directory. you must use astro endpoints
  2. if you deploy astro static site (don't use astro cloudflare plugin), you can use the /functions directory
no where in the official documentation does it clearly mention this. either in cloudflare docs or astro docs:
https://developers.cloudflare.com/pages/framework-guides/deploy-an-astro-site/
https://docs.astro.build/en/guides/deploy/cloudflare/

replicate the issue with SSR:

pnpm create astro@latest # choose basic site, accept defaults
cd yourproject
pnpm run astro add cloudflare
# open astro.config.js and modify adapter line to look like this:
#   adapter: cloudflare({mode: "directory"})
mkdir functions
touch functions/hello.js
# add code to hello.js
# add code to a src/pages/index.astro to call to hello function.
# maybe add a button so when you click it calls fetch('/hello')
# run project locally:
wrangler pages dev -- npm run dev
# click button and you will see the function is called successfully
# now push your project up to github. and connect cloudflare to github repo, deploy it.
# you will see that you get a 404 when it tries to fetch('/hello')

both astro and cloudflare docs need to be updated to make this limitation clear IMO. the cloudfare docs make it seem as though you can use the functions directory while using SSR but this is not the case. (unless i'm doing something wrong)
Astro is an all-in-one web framework for building fast, content-focused websites. By default, Astro builds websites that have zero JavaScript runtime
Astro Documentation
How to deploy your Astro site to the web using Cloudflare Pages.
Was this page helpful?