Advanced Usage
If you need to run code before or after your Next.js application, create your own Worker entrypoint and forward requests to your Next.js application.
This can help you intercept logs from your app, catch and handle uncaught exceptions, or add additional context to incoming requests or outgoing responses.
- Create a new file in your Next.js project, with a
fetch()
handler, that looks like this:
This looks like a Worker — but it does not need its own wrangler.toml
file. You can think of it purely as code that @cloudflare/next-on-pages
will then use to wrap the output of the build that is deployed to your Cloudflare Pages project.
- Pass the entrypoint argument to the next-on-pages CLI with the path to your handler.