Skip to content
workers-go
Esc
navigateopen⌘Jpreview
On this page

Pages Functions

Serve dynamic routes on Cloudflare Pages with a Go Wasm function.

Pages Functions let you add dynamic routes to a Cloudflare Pages site. A workers-go function works the same way as on Workers — workers.Serve handles the request — only the project layout differs.

Project layout

functions/
  api/
    [[routes]].mjs   # catch-all entry point that loads the Wasm binary
pages/
  index.html         # static assets served by Pages
main.go
wrangler.toml

The [[routes]].mjs file is a Pages Functions catch-all route. It instantiates the Wasm module and forwards requests under /api/* to your Go handler.

Write the handler

Any router works. This example uses chi:

func main() {
	r := chi.NewRouter()
	r.Route("/api", func(r chi.Router) {
		r.Get("/hello", func(w http.ResponseWriter, req *http.Request) {
			name := req.URL.Query().Get("name")
			if name == "" {
				name = "Pages Functions"
			}
			fmt.Fprintf(w, "Hello, %s!", name)
		})
	})
	workers.Serve(r)
}

A request to /api/hello?name=Go is routed through functions/api/[[routes]].mjs into the Go handler and responds with Hello, Go!.

Example project

_examples/pages-functions

Was this page helpful?