Ktor 1.6.8 Help

Webjars

This plugin (previously known as feature) enables serving static content provided by webjars. It allows you to package your assets such as javascript libraries and css as part of your uber-jar.

Add dependencies

To enable Webjars support, you need to include the ktor-webjars artifact in the build script:

implementation "io.ktor:ktor-webjars:$ktor_version"
implementation("io.ktor:ktor-webjars:$ktor_version")
<dependency> <groupId>io.ktor</groupId> <artifactId>ktor-webjars</artifactId> <version>${ktor_version}</version> </dependency>

Install Webjars

To install the Webjars plugin, pass it to the install function in the application initialization code. Depending on the way used to create a server, this can be the embeddedServer function call ...

import io.ktor.features.* // ... fun main() { embeddedServer(Netty, port = 8080) { install(Webjars) // ... }.start(wait = true) }

... or a specified module.

import io.ktor.features.* // ... fun Application.module() { install(Webjars) // ... }

Configure Webjars

install(Webjars) { path = "assets" //defaults to /webjars zone = ZoneId.of("EST") //defaults to ZoneId.systemDefault() }

This configures the plugin to serve any webjars assets on the /assets/ path. The zone argument configures the correct time zone to be used with the Last-Modified header to support caching (only if Conditional Headers feature is also installed).

Versioning support

Webjars allow developers to change the versions of the dependencies without requiring a change on the path used to load them on your templates.

Let's assume you have imported org.webjars:jquery:3.2.1, you can use the following html code to import it:

<head> <script src="/webjars/jquery/jquery.js"></script> </head>

You don't need to specify a version, should you choose to update your dependencies you don't need to modify your templates.

Last modified: 27 May 2021