next-on-pages and modifying routes
So I only want page functions to run whenever I hit my nextjs API route, how can I do this?
7 Replies
That is not advised with next-on-pages I'm afraid. There exists the
_routes.json
file to tell Cloudflare when to invoke the worker script, however, excluding some of your routes has an incredibly high chance of completely breaking the routing process for matching your request to the right page. I would not recommend interfering with how the routing system works.To clarify, the default setup of next-on-pages hits a bundled worker for every static asset (domain.com/favicon.ico) for example.
Then it also sounds like there isn't a good way to fix this?
Not all static assets. We exclude everything in
which will again significantly change how the routing process works to much more closely align with how Vercel treats routing.
_next/static/*
, which include all the chunk files used by pages, CSS, fonts, etc. If you're referring to just certain static assets, yeah, they might be safe to exclude, but it's still not necessarily advised.
How Vercel builds certain static files changes constantly, and how we treat those files changes constantly. For instance, they recently changed how things like favicon.ico
are treated when placed in the app directory, which we have to add custom behaviour during build time to change.
If you have dynamic routes that are generated to static HTML, excluding them will break the ability for them to receive the dynamic parameter, because retrieving the dynamic parameter is done in the routing process. Likewise, they won't have any headers that should be applied during routing, correct static codes, overridden content types where necessary, rewritten/redirected paths when required, etc.
In case you haven't seen, there's a new routing system that will be landing ![soontm](https://cdn.discordapp.com/emojis/885759264013049856.png)
The new routing system you're referring to, is that the new Nextjs routing or cloudflare routing?
Thanks so much for the in-depth response btw. Been banging my head against the wall all day to get a POC next-on-pages + clerk setup
When I say new routing system, I'm referring to the routing system that we have built ourselves to handle the Vercel build output that is used in the worker script we generate for next-on-pages. (https://github.com/cloudflare/next-on-pages/pull/206)
By the way, that reply was mostly in the context of the new routing system. The current routing system has a lot of issues with processing requests properly and finding the correct routes. By proxy of that, statically generated routes do not work at the moment, but will be supported in the new routing system.
Got it. Thanks so much for the heads up. I really appreciate the quick and thorough reply 🙂
No worries 🙂