0
votes

I am new to flask and building my first web app. My service worker registers fine when running from localhost, but when I deploy the app to pythonanywhere. To build this, followed the process on www.flaskpwa.com. Here is what I got:

  1. My sw.js file is located in the static folder.

  2. I have a route to serve the service worker js file:

    @app.route('/sw.js') def sw(): return app.send_static_file('sw.js')

  3. On my PythonAnywhere "Web" page, I have a Static Files path called /static/ pointing to the static folder.

  4. My app.js file is registering the service worker using:

    navigator.serviceWorker.register('./sw.js')

When loading my web app, my console is returning the following two errors:

A bad HTTP response code (404) was received when fetching the script.

Unable to register service worker. TypeError: Failed to register a ServiceWorker for scope ('https://xxxxx.pythonanywhere.com/') with script ('https://xxxxx.pythonanywhere.com/sw.js'): A bad HTTP response code (404) was received when fetching the script.

I am pretty sure my issue is with the Flask route, since I get the same 404 error when attempting to point to xxxxx.pythonanywhere.com/sw.js from the browser. I am stuck in trying to get that correct.

Thanks for your input.

1

1 Answers

0
votes

If you get a 404 error when you try to access https://xxxxx.pythonanywhere.com/sw.js from the browser, then it's not your Flask route -- it's your static route. If you have a static file mapping with the "URL" set to /static then it will only pick up requests starting with https://xxxxx.pythonanywhere.com/static.

If you want to set up a static route specifically for the file sw.js and you want it to be accessible from https://xxxxx.pythonanywhere.com/sw.js, then you need to set up a static file mapping with a "URL" of /sw.js, and the "Directory" set to the full path to the file, for example /home/xxxxx/mysite/static/sw.js.

Check out the PythonAnywhere documentation for static files for more details.