Cloudflare Page Functions - No route (Astro.build)

Hey all, I’ve followed the docs and examples from (https://docs.astro.build/en/guides/deploy/cloudflare/#using-pages-functions and https://developers.cloudflare.com/pages/framework-guides/deploy-an-astro-site/) and have a cloudflare-enabled astro site deployed. However, when I add the root level functions directory (along with a single endpoint) and try to call the endpoint (from index.astro ) I’m getting 500 server errors. I’m assuming it’s because there’s no entry in the generated _routes.json file:

_routes.json
{
  "version": 1,
  "include": [
    "/",
    "/_image"
  ],
  "exclude": []
}

Any ideas?

Repo: https://github.com/serdar/my-astro-app
Cloudflare page: https://my-astro-project.pages.dev/

You haven’t specified the deployment mode in your Cloudflare integration config, it means the default “advanced” mode is used which means your “functions” folder is disregarded - Astro compiles code in ./src folder and generates _worker.js file in the dist folder which is being deployed.

If you want to specify your functions in ./functions folder, make sure to specify directory mode https://docs.astro.build/en/guides/integrations-guide/cloudflare/#options

1 Like

Much appreciated @antipov.alexei - thank you for explaining. Other than the config setting you mentioned I also had to change the fetch API endpoint in my astro file to the wrangler URL (http://localhost:8788/api/echo) in dev mode. Once set, this all worked locally.

When it comes to deployment however if I revert the url back to /api/echo, build, and redeploy I get a 500 error.

New preview url is https://949a94e1.my-astro-project.pages.dev/

Edit: (fixed incorrect url )

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.