Need to add a a header when returning an asset using the Play Framework Assets Controller

本秂侑毒 提交于 2020-01-07 02:01:52

问题


I need to return pdf files with the header

X-Robots-Tag: noindex

https://developers.google.com/webmasters/control-crawl-index/docs/robots_meta_tag?hl=de#robots-meta-tag-verwenden

Is there a way to do this using the standard play Assets Controller? Took a look inside the source code and the only thing that seems to be configurable is:

val configuredCacheControl = config(_.getString("\"assets.cache." + name + "\""))

Thanks


回答1:


I might go with Kris's idea. Something kinda like this should get you part of the way there, basically all I'm doing is leveraging the Play! Controller that serves static resources but adding a header to the result:

object AssetsController extends Controller {

def modifiedAsset(path: String, file: String): Future[Result] = Async.action { implicit request =>
  Assets.at(path, file)(request).map { result => 
   result.withHeaders(("X-Robots-Tag", "noindex"))
  }
}

Then your routes file would need to be modified from controllers.Assets.at(path="/public", file) to controllers.AssetsController.modifiedAsset(path="/public", file)



来源:https://stackoverflow.com/questions/36942470/need-to-add-a-a-header-when-returning-an-asset-using-the-play-framework-assets-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!