unjs / nitro

Next Generation Server Toolkit. Create web servers with everything you need and deploy them wherever you prefer.
https://nitro.unjs.io
MIT License
5.44k stars 471 forks source link

Possibility to render a single route directly to stdout #2466

Open jrutila opened 1 month ago

jrutila commented 1 month ago

Describe the feature

There should be a way to use nitro to generate single pages. One place to run this would be Azure Function that just outputs, using prebuilt nitro files, the desired page output when it is run and then dies out.

This should be possible through the prerender function and a command line tool.

Command line tool would work like this: nitro render /path/to/page and when you want stdout: nitro render --stdout /path/to/page

I have already studied the generateRoute code and will post a PR that will implement the needed options and the cli tool.

Additional information

jrutila commented 1 month ago

This would also trickle down to frameworks like nuxt that could extend its generate command to support single page and stdout renderings.

jrutila commented 1 month ago

This could be extended so that an output function could be provided with the prerender options. This function would be called with the Buffer and path so that anything could be done with the rendered output. Like, sending to email, saving to cloud storage etc.

pi0 commented 2 weeks ago

This is a nice idea but perhaps something to rethink for the future. (some context: https://github.com/unjs/nitro/pull/2467#issuecomment-2172866120)

jrutila commented 2 weeks ago

I would like to work on this feature. You said in the PR: "Nitro has already a CLI preset intended for same thing (standalone output that can render to stdout without depending in nitro builder core) -- PR welcome to improve it". Do you mean the static preset for build command? What would be the desired way to call it from cli?

And, regarding the programmatic API, is the src/core/prerender/prerender.ts file's generateRoute the correct place to implement this (as I did on the PR)? I couldn't find the mentioned prerenderRoute function anywhere. Or am I in wrong branch, now?

pi0 commented 2 weeks ago

Do you mean the static preset for build command? What would be the desired way to call it from cli?

I meant --preset cli (src).

(I guess not working on latest branch needs a simple fix to remove CJS check)

For node variant, ./server/index.mjs [route]. We can add CLI shortcut later but this usage allows to use CLI renderer independent from big Nitro builder package and keep it portable.

And, regarding the programmatic API

Yes we could export it from render.ts however let's not rush into this part as mentioned, we need to first implement smart rebuild.


In summary there are two tracks we can move this forward:

First, requires updates to the current cli preset, is portable and we can work on it today.

Second, requires introduction of smart rebuild and some cleanups in prerender implementation to make sure we can maintain it in the future.

jrutila commented 2 weeks ago

Alright, now I got the gist of the cli preset. Makes sense. I made a PR for the fix for the preset, now. I will next investigate if it suits my use case.

jrutila commented 2 weeks ago

That is the PR based on v2