Next.js Discord

Discord Forum

Where in next.js source code can I find the page handler?

Unanswered
Sinhala Hound posted this in #help-forum
Open in Discord
Sinhala HoundOP
I'd like to see how next.js does a few things. How does it responde to GET requests for page requests in the first load situation. How does it switch from static mode, to dynamic mode when a draftMode token is set in the headers. Things like that.

More specifically, I'm interested in what it would take to add the ability to POST to the same end point (for being able to post content and render it with RSC for "live preview" functionality in a CMS like Payload). I assume it's more complicated than something like an express.js, but if someone could point me to the code that handles that, it'd be a helpful start.

5 Replies

Sinhala HoundOP
I'm looking at next.js/packages/next/src/server/request/cookies.ts|draft-mode.ts|header.ts - as a starting point.
What ideally I'd want is something like those methods, which would follow all the same rules as those methods, with reguard to putting the route in to "dynamic" mode (or draft mode - I'd even be fine with a restriction that requires draft mode, since that is my purpose here.)
So, a method like:
const post = await postBody();
const postData = await post.json();

With something like this, I could post to the page location, and then render with that data, instead of whatever I get from a CMS like payload. This would be faster than the way Payload currently does it, by stuffing a version of the current editor in to a database, then refreshing the client to load that in draft mode. There's a lot of moving parts there, and it requires a backing store.
I'd rather just post and render - more similar to how client side live preview works. It seems like it'd be easy enough to figure out how to expose the above API by adding it to the above referenced folder, and implementing it in a similar way. But I don't know how to run the POST through to that function. That's probably the part you wouldn't want to leaven enable either - so maybe that part can only be turned on in draft mode.
(I also imagine it's not super easy, given the amount of infrastructure support built up in these pathways)