How do I enable "Access-Control-Allow-Origin" headers for static files in Yesod?

I am using a scaffolded site and a static subdomain to serve my static files. Static files are requested using explicit urls from Javascript games that I use for my yesod app (written using Phaser framework).

Sample error message from Javascript console:

Phaser.Loader error loading file: player1_icon from URL http://example.com/static/games/mygame/images/player1/icon.png 
Image from origin 'http://example.com' has been blocked from loading by Cross-Origin Resource Sharing policy: 
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

      

Some research has shown that adding a string addHeader "Access-Control-Allow-Origin" "*"

to the handler function can solve this problem. However, a static route does not use a handler function, so I don't know where I would say that.

Any help would be greatly appreciated.

+3


source to share


1 answer


This can be achieved with WAI middleware that adds this header. This middleware will be added to your module Application

. The wai-extra package has several intermediate elements that can be used as an example of how to do this.



More generally: this is probably a function that serves as a satellite. Can you open a Github question about this?

+2


source







All Articles