Dynamically use App Router pages or Route Handlers depending on the CMS #85164
Replies: 2 comments 6 replies
-
Beta Was this translation helpful? Give feedback.
-
|
Are you self-hosting? And in full control of this app deploys and such? Otherwise... If we take a step back, maybe this take:
Is kind of the way to go? Fill the key value look up Db with path -> type associations and use that to route. Yes it is an extra layer, and adds overhead, but it'll get the job done, and be predictable, easy to debug and likely maintain too. As long as the latency of doing the look up is very low, this should be fine. A less optimal solution would be to try and rely on the fetch cache layer, again, this kind of depends on how much control you have over the deployment, let's assume there's just one deployment, then you could handle all requests in a Route Handler, fetch with cache, so that the response is saved to the data cache, and if the content turns out to be a HTML, proxy to a server component page, and stream that back to the client. You'd need to hide that server component page from direct access but it'd get the job done. The latency bulk would be in the one-maybe-two HTTP round trips (client -> route handler -> (possibly) to page -> route handler -> client). Since we relied on the fetch data cache, the page round trip would just read from there and start streaming the response back to the route handler, which in turn forwards that to the client. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
My colleagues and I are in the process of migrating our sites to use App Router. We make a request to our CMS with the incoming URL that then returns the page content. There is no way of knowing ahead of time what kind of content the CMS will respond with, this could be normal site pages (using App Router) or it could be plain text, JSON etc.
Those kind of custom responses can only be achieved using Route Handlers, so we effectively have two possible entry points.
The way to solve this would be to move our CMS request out from the root server component and into middleware, where we could rewrite the response to be either the
page.tsxor theroute.tsdepending on the content type.The issue with this is that middleware is completely isolated from app router meaning we'd have to make the CMS request twice: first in the middleware then in the server component/route handler.
I've seen various hacky ways to somewhat work around this:
I understand that the reason middleware is unable to directly pass data to the app router is because it's designed to be an Edge function, but we deploy our project as standalone and it all runs from within the same Docker image. I was hoping the newly introduced
nodejsmiddleware runtime would resolve this but it seems the still run in completely separate processes.I know our use case is very common and it's frustrating to rely on hacks to make it work for what should be simple. Maybe a new
context.tsfile could exist under app router directories allowing it to pick between using the page or route handler whilst providing initial "context" (in our case containing the CMS response) to them either via props or via a new argument/option in the route handler?Or maybe an option to run the middleware in the same node process as the rest, allowing us to create an in-process in-memory cache accessible to both?
Are there any plans to implement a solution to this problem? Or are there other cleaner ways of pulling this off?
Thanks
Beta Was this translation helpful? Give feedback.
All reactions