How to properly configure app.yaml to serve JavaScript files as text/javascript?

250 views Asked by At

JavaScript files are being served as text/plain in App Engine, regardless of what I do to change that:

'content-type' header media type value should be 'text/javascript', not 'text/plain'.

Anyone have any insight into what I'm doing wrong and how to get these to show up as the appropriate type?

I have three .js files in a 'static' subdirectory off of the main project. I've tried a few different ways to configure this, but nothing seems to work.

app.yaml:

- url: /(.*\.js)
  static_files: static/\1
  upload: static/.*\.js
  mime_type: text/javascript

I tried specifying them individually. (for static/script.js:)

- url: /static/script\.js
  static_files: static/script.js
  upload: static/script\.js
  mime_type: text/javascript

also

- url: /static/store.js
  static_files: static/store.js
  upload: static/store.js
  mime_type: application/javascript

I've also tried setting it in the html:

<script src="{{ url_for('static', filename='script.js') }}" type="text/javascript" charset="UTF-8"></script>

Everything works, but I'm getting errors about the javascript files being the wrong MIME type on every page, as well as "Response should include 'x-content-type-options' header." for every file accessed by each page.

I finally was able to remove most of the x-content-type-options errors by adding 'http_headers' as below, but then all scripts from the .js files are refused because they're not registering as the correct MIME type.

handlers:
- url: /static
  static_dir: static
  http_headers:
    X-Content-Type-Options: nosniff

Thanks in advance for any insights you can provide!

*Update
Flask mime type configuration:

app = Flask(__name__, static_url_path='/static')

app.config['MIME_TYPES'] = {
    '.js': 'text/javascript'
}
1

There are 1 answers

1
NoCommandLine On
  1. If you didn't use dev_appserver.py when testing locally, then you didn't actually test the contents of app.yaml file which might be why it worked locally

  2. For python, all you need in your app.yaml file is something like this

- url: /static
  static_dir: static/

Note that the value of static_dir depends on the location/path of the static folder with respect to your project root folder