nitro icon indicating copy to clipboard operation
nitro copied to clipboard

Possibility to render a single route directly to stdout

Open jrutila opened this issue 1 year ago • 7 comments

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

  • [X] Would you be willing to help implement this feature?

jrutila avatar May 24 '24 10:05 jrutila

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

jrutila avatar May 24 '24 10:05 jrutila

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.

jrutila avatar May 28 '24 09:05 jrutila

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

pi0 avatar Jun 17 '24 09:06 pi0

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?

jrutila avatar Jun 17 '24 10:06 jrutila

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:

    1. Standalone CLI preset
    1. Selective prerender with smart rebuild

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.

pi0 avatar Jun 17 '24 10:06 pi0

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 avatar Jun 17 '24 10:06 jrutila

That is the PR based on v2

jrutila avatar Jun 17 '24 11:06 jrutila