Play upgrade 2.4.6 to 2.6.21

scala

(Yash Bhardwaj) #1

Hi, I think I’m almost done with Play upgrade 2.4.6 to 2.6.21. Problem I’m facing right now is with WSResponse.

First, in Play 2.4 we used to hit the node server for view and process it like:

for

GET           /           controllers.Application.developmentRouterIndex

we had

if (environment.mode.equals(Mode.Dev))
      Action.async {
        val response = wSClient.url("http://localhost:8080/").get()
        response.map { res =>
          Ok(res.body).withHeaders((CONTENT_TYPE, MimeTypes.HTML))
        }
      }
else controllers.Assets.versioned("/public/www", Asset("index.html"))

and things were loading perfectly on http://localhost:9000/.

But after upgrade, it is giving below warning and not loading the assets correctly.

2019-01-23 18:35:40 [WARN] from akka.actor.ActorSystemImpl - Explicitly set HTTP header 'Content-Type: text/html' is ignored, explicit `Content-Type` header is not allowed. Set `HttpResponse.entity.contentType` instead.
2019-01-23 18:35:40 [WARN] from akka.actor.ActorSystemImpl - Explicitly set HTTP header 'Content-Type: text/html; charset=UTF-8' is ignored, explicit `Content-Type` header is not allowed. Set `HttpResponse.entity.contentType` instead.
2019-01-23 18:35:40 [WARN] from akka.actor.ActorSystemImpl - Explicitly set HTTP header 'Content-Length: 150' is ignored, explicit `Content-Length` header is not allowed. Use the appropriate HttpEntity subtype.

And only the index.html plain text is getting displayed. Means, it is working but not able to bootstrap things.
I’m not able to figure the Gap, I know the static way of Assets and ExternalAssets is deprecated, but it can still be used right? So I’ll need to discuss this with you (experts).

On the previous version it also loaded a lot other resources like shown in the picture.

Why it is not loading these resources in the newer version, is there some different way to implement this in the newer Play?


(Yash Bhardwaj) #2

I changed

 Action.async {
        val response = wSClient.url("http://localhost:8080/").get()
        response.map { res =>
          Ok(res.body).withHeaders((CONTENT_TYPE, MimeTypes.HTML))
        }
      }

to

 Action.async {
    val response = wSClient.url("http://localhost:8080/").get()
    response.map { res =>
      val header = new ResponseHeader(OK, Map(CONTENT_TYPE -> MimeTypes.HTML))
      val httpEntity = HttpEntity.Streamed(res.bodyAsSource, Some(res.bodyAsBytes.length), Some(MimeTypes.HTML))
      Result(header, httpEntity)
    }
  } 

Now it has started to load some more resources:

But it is still not able to bootstrap the view, showing Loading...
Still showing

[WARN] from akka.actor.ActorSystemImpl:? - Explicitly set HTTP header 'Content-Type: text/html' is ignored, explicit `Content-Type` header is not allowed. Set `HttpResponse.entity.contentType` instead.

(Marcos Pereira) #3

You need to replace:

Ok(res.body).withHeaders((CONTENT_TYPE, MimeTypes.HTML))

With:

Ok(res.body).as(MimeTypes.HTML)

Best.