How do I return assets with a 404 status code in Play?

Currently I have a play application with Scala, in where I have the frontend in the public folder as a SPA. I have a /404 route in where I want to return the frontEnd with the status code of 404. However if I write:

def returnNotFound(): Action[AnyContent] = Action {
    NotFound(assets.at("index.html"))
}

Then I get a compilation error because assets returns an Action, not a Writeable. So how can I return the index.html in the public folder with the status code of 404?

Upvotes: 0

Views: 316

Answers (1)

Ivan Kurchenko
Ivan Kurchenko

Reputation: 4063

The issue here is that assets.at("index.html") returns Action[AnyContent] which suppose to be return method of Controller, hence in order to change status you need to change result from assets.at like:

def returnNotFound(): Action[AnyContent] = Action.async { implicit request =>
    assets.at("index.html")(request).map { result =>
      new Result(result.header.copy(status = 404), result.body)
    }
}

Upvotes: 1

Related Questions