Skip to content

Hot Reloading

For a regular website

Hot reloading is supported out of the box when using the gg.jte.resolve.DirectoryCodeResolver (or the gt.jte.resolve.ResourceCodeResolver with resources located outside of JAR files). Before a template is resolved, the modification timestamp of the template file and all of its dependencies is checked. If any modification is detected, the template is recompiled, and the old one is discarded to GC.

Warning

It makes sense to do this in your local development environment only. When running in production, for maximum performance and security, precompiled templates are recommended instead.

If you clone jte repository, you can launch the gg.jte.benchmark.SimpleWebServer example's main method. It will fire up a tiny webserver with one page to play with at http://localhost:8080.

For a statically rendered website

If you're using jte to pre-render static websites as HTML files, you can also listen to template file changes during development and re-render affected static files. Add the jte-watcher module to your project:

1
2
3
4
5
<dependency>
    <groupId>gg.jte</groupId>
    <artifactId>jte-watcher</artifactId>
    <version>${jte.version}</version>
</dependency>
implementation("gg.jte:jte-watcher:${jteVersion}")

The gg.jte.watcher.DirectoryWatcher::start() method starts a daemon thread listening to file changes within the jte template directory. Once file changes are detected, a listener is called with a list of changed templates.

1
2
3
4
5
6
7
8
if (isDeveloperEnvironment()) {
    DirectoryWatcher watcher = new DirectoryWatcher(templateEngine, codeResolver);
    watcher.start(templates -> {
        for (String template : templates) {
            // Re-render the static HTML file
        }
    });
}
1
2
3
4
if (isDeveloperEnvironment()) {
    val watcher = DirectoryWatcher(templateEngine, codeResolver)
    watcher.start(templates -> templates.forEach { /* re-render the static HTML file */ });
}

Customizing generated class directory

By default, generated sources and classes are outputted into the subdirectory jte-classes under the current directory. It is possible to customize this directory when creating the template engine. But to have the hot reload feature working, a custom class directory must not be on the classpath. If it is on the classpath, generated classes will be visible to the default class loader, and once a generated class is loaded, it will not be possible to reload it after recompiling a template, thus making the hot reload effectively non-functional.