Creating static html from Rocket template

Hi everyone,

I have an API that serve data.
One endpoint allow to preview an HTML page.
I need an other endpoint that will generate an HTML file and return the file path (to be available without API running).

I'm using rocket with Tera template.

I've seen that Template::show can do what I want, but the documentation say :
Render the template named `name` with the context `context` into a `String`. This method should **not** be used in any running Rocket application. This method should only be used during testing to validate `Template` responses. For other uses, use [`render()`](#method.render) instead.

So is it possible or not ?

Thanks :slight_smile:

Both render() and show() will work fine for your purposes.

The warning in render() is trying to prevent

  • Unnecessary work where you first render the entire HTML page to a String only to immediately copy it into the response body and throw away the original String
  • Waiting for the entire HTML page to be rendered before you start sending it to the client when you could write bytes directly to outgoing TCP stream as they are being rendered and let the browser start parsing the HTML as it comes in

By returning the Template value from render() from the handler we get that streaming effect we want.

1 Like

Can you elaborate more on why you need this? If one endpoint already serves the HTML, can't someone who called the API once simpy save the contents to a file and be done with it?

Hi and thanks for your answers,

At the beginning, the purpose was to serve a static HTML file.

Finally, we will save only the data and the front will render the file (the initial purpose was compatibility with old browser version and light HTML file)

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.