Cloudflare Pages Functions
How to add dynamic content to the Pages
I planned that most of the web site should be static. I would like to have only some parts to have authentication or another dynamic features. Cloudflare pages can use Workers. If the project has directory ‘/functions’ Cloudflare use workers environment to execute the code from there. I could not really find examples how to use Pages functions with Hugo.
But Cloudflare is doing everything for us. Just add ‘/functions’ directory to the root of the project and push to the git. I created file functions/helloworld.js
export function onRequest(context) {
return new Response("Hello, world!")
}
During build process you can see:
12:57:10.653 Found Functions directory at /functions. Uploading.
12:57:11.544 ✨ Compiled Worker successfully
Then access web-site url/helloworld and you have the content generated by the worker.
Now we need to limit our workers to be executed only on some of the site content. Most of the static pages should not use workers. _routes.json
{
"version": 1,
"include": ["/private/*"],
"exclude": []
}