I've found an issue related to serving SPAs from t...
# kotlin-toolchain
l
I've found an issue related to serving SPAs from the resources folder in Ktor. In short, the fat jar built with
kotlin package
isn't serving my web ui correctly. Instead of displaying the HTML file, it's triggering a download because the content type is, for some reason,
application/octet-stream
. The problem doesn't occur when running with
kotlin run
though, as the content type stays as html. The workaround was to add
call.respondResource
as follows:
Copy code
get("/") {
    call.respondResource("webui/index.html")
}

singlePageApplication {
    useResources = true
    filesPath = "webui"
    defaultPage = "index.html"
}
To be sure this is an issue with KTC, I tried the same in a normal KMP project with Gradle (without the workaround) and it worked just fine with both the
buildFatJar
and
runFatJar
tasks.
j
Hi Lucas! Thanks for the report. Where is
webui/index.html
declared? Is it in the resources of the Ktor server module? Or in another local module that the server depends on?
l
In the resources of the ktor module. Full path is
ktor-backend/resources/webui/index.html
. It's a vue frontend built with
vite
- nothing unusual.
🙏 1
j
Thanks. I'm suspecting the difference in behavior is due to the JAR format used. The JAR produced in the Gradle setup is a fat JAR with all the contents merged in a single jar. The JAR produced by
kotlin package
is an executable JAR following the Spring Executable JAR format (which contains nested JARs).
Ok I think that my suspicion is correct. There is an issue on Ktor side to support the
jar:nested:
protocol to serve resources from this type of JAR: youtrack.jetbrains.com/issue/KTOR-8883 It seems to be implemented already but not released yet. It should be released in Ktor 3.6.0. @Bruce could you please confirm all this?
Side note: maybe I'm mistaken and the problem comes from the fact that Ktor is serving the directory entry
webui
instead of ignoring it and falling back to the default. @Bruce, is this expected?
b
Yep, that looks right. Maybe you could try on 3.6.0-eap-1675 with the EAP repository? https://redirector.kotlinlang.org/maven/ktor-eap
l
Same issue on
3.6.0-eap-1675
.
j
Yeah it seems to be about Ktor serving the directory entry. It's not only the wrong content type, it's also an empty content. I think there is something to fix on Ktor side here. @Bruce, could you please have a look?
b
yeah I'll log a new issue
I've got a hunch on what's going on there
j
Thanks, perfect!