com-lihaoyi/cask

Serving css / js appears to have incorrect / no mime types?

Closed this issue · 1 comments

To be clear - I'm unclear whether this is actually expected behaviour or not.

in "dev"

@cask.staticResource("assets")
def s() = "assets"

Works fine, where my html file has something like

<link rel = "stylesheet" href = "assets/css/todo.css"/>

However, once I "publish" (for me that looks like going through sbt stage, it's hosted somewhere I don't understand on AWS), and the environment changes to goes through https, some authentication wrapper etc...

the styles are not applied. I can see them being correctly loaded in the browser... but ... no style.

This appears to fix my problem... i.e. manually butcher some contents types into the resource routes


  class myStaticResources(val path: String,
                        resourceRoot: ClassLoader = classOf[myStaticResources].getClassLoader,
                        headers: Seq[(String, String)] = Nil)
    extends HttpEndpoint[String, Seq[String]]{
    val methods = Seq("get")    
    type InputParser[T] = QueryParamReader[T]
    override def subpath = true
    
    def wrapFunction(ctx: Request, delegate: Delegate) = {
      delegate(Map
      ()).map(t => {                
        println(ctx.remainingPathSegments.headOption)
        val headersOut : Seq[(String, String)] = ctx.remainingPathSegments.headOption match {
          case Some(s) if s.takeRight(2) == "js" => Seq(("Content-Type", "text/javascript"))
          case Some(s) if s.takeRight(3) == "css" => Seq(("Content-Type", "text/css"))
          case Some(s) if s.takeRight(4) == "html" => Seq(("Content-Type", "text/html"))
          case _ => Nil
        }       
        cask.model.StaticResource(StaticUtil.makePath(t, ctx), resourceRoot, headersOut)
      }
      )
    }

    def wrapPathSegment(s: String): Seq[String] = Seq(s)
  }

@ myStaticResources("assets")
def s() = "assets"

But I have to say the behaviour was non-obvious to me... I didn't spot it in the docs - is there an existing decorator for this? It feels like something which should be "standard", but then perhaps there is complexity here I don0t understand. The underlying cause is the CSS being served as "text/plain" rather than "text/css"