hertz icon indicating copy to clipboard operation
hertz copied to clipboard

Proposal: Add reload mechanism to html render

Open welkeyever opened this issue 3 years ago • 8 comments

Describe the solution you'd like

Add or replace the way of rendering HTML in order to let the server reload HTML templates without restarting.

Describe alternatives you've considered

A timer or a file system watcher may help to decide whether to reload or not.

Additional context

Be aware of performance overhead. DO NOT reload it every time when render api is called.

welkeyever avatar Jun 23 '22 03:06 welkeyever

I would like to help with this issue

stephenzhang0713 avatar Jun 24 '22 11:06 stephenzhang0713

Some of my thoughts on this issue

  • By analyzing the source code of hertz, I found that the method of loading HTML is located in pkg/route/engine.go, and there are two main functions in engine.go which are responsible for executing loading HTML, namely LoadHTMLGlob and LoadHTMLFiles
  • As mentioned in the description of the issue above, consider using a timer or watcher to listen to the file system, I think we can use a channel to listen for events and perform a reload once the files in the directory change (read or write)

stephenzhang0713 avatar Jun 30 '22 02:06 stephenzhang0713

Yes, LoadHTMLGlob and LoadHTMLFiles will set the HTMLRender immediately, so here a new auto-reloading Render may help do the reload job(check the files during .Instance()).

stage 1 of the impl: users can choose the auto-reloading Render for their project. stage2 : switching automatically between normal Render and auto-reloading Render according to some conditions(maybe config/env/etc.)

welkeyever avatar Jun 30 '22 09:06 welkeyever

possible to get ideas from here? it's the fastest templating engine but doesnt do live reload https://github.com/Joker/jade

hiqsociety avatar Jul 04 '22 19:07 hiqsociety

Hi~ I am wondering how it's going? Do you encounter any difficulties? You may join our Feishu Chat Group to discuss more details about this task.

Duslia avatar Jul 07 '22 06:07 Duslia

Hi~ I am wondering how it's going? Do you encounter any difficulties? You may join our Feishu Chat Group to discuss more details about this task.

Sorry I just saw your message, I have been in hospital for a month, I'm sorry to reply to you too late

stephenzhang0713 avatar Jul 22 '22 08:07 stephenzhang0713

what during .Instance() mean ? auto-reloading Render may add some time conditions such as cron ? and set a default cron if is auto-reloading Render

byene0923 avatar Aug 03 '22 15:08 byene0923

i see fiber support this ,every time do the Render() will the tpl be reloaded if engine.reload is ture https://github.com/gofiber/fiber/blob/c450072f4a490355dffc31f5cc36036a2383b1c7/internal/template/html/html.go#L187

so may hertz do as fiber ?

byene0923 avatar Aug 03 '22 15:08 byene0923