fallback

fun fallback(fallback: suspend (String, <Error class: unknown class>) -> Unit)(source)

Configures custom fallback behavior when a requested static resource is not found.

This function allows you to provide a callback that inspects the originally requested path (e.g. "plugins/file.php") and the ApplicationCall, and then perform custom logic such as:

  • redirecting to a different path,

  • responding with a specific HTTP status (e.g. 410 Gone or 400 Bad Request),

  • or serving an alternative static file manually.

Example:

staticFiles("/static", File("files")) {
fallback { requestedPath, call ->
when {
requestedPath.endsWith(".php") -> call.respondRedirect("/static/index.html")
requestedPath.endsWith(".xml") -> call.respond(HttpStatusCode.Gone)
else -> call.respondFile(File("files/index.html"))
}
}
}

This differs from the existing default(path: String?):

  • default(...) serves a fixed, path‑agnostic fallback resource.

  • fallback { requestedPath, call -> ... } gives full control and context for conditional behavior.

See also