Elisabeth
Elisabeth

Reputation: 146

Play framework external Javascript URL syntax

How to use external JavaScript files in the Play framework?

I used this syntax:

<script src="https://www.gstatic.com/charts/loader.js" type="text/javascript"></script>

I put this in the <head> section of main.scala.html.

https://www.gstatic.com/charts/loader.js is the correct link, but it doesn't load and the status of the package is (blocked:csp):

Status (blocked:csp) (picture1)

Headers:

Package header (picture2)

Local javascript files work fine, for example:

<script src="@routes.Assets.versioned("javascripts/hello.js")" type="text/javascript"></script>

Upvotes: 1

Views: 613

Answers (1)

Dexmo
Dexmo

Reputation: 171

CSP stands for Content Security Policy (see more):
The corresponding header defines from which sources certain components are allowed to be loaded. Usually, the default settings is default-src: 'self'. Than means only your own host is allowed as source for scripts, css, images, etc. In your case that was localhost:9999, so your local javascript file was passed. You will need to add gstatic.com as an allowed script-src.

Thus, this configuration needs to be done in your application.conf-file:

play.filters.headers.contentSecurityPolicy = "default-src: 'self'; script-src: 'self' gstatic.com"

For further information, please visit the official documentation.

Upvotes: 1

Related Questions