I have a flask + react application that is deployed on Google App Engine. Recently, I discovered that each time I deployed a new version to the GAE, my site would go down for a few hours, and several web pages cannot load correctly. I checked the console, the web application is trying to get the static files from the last version, which resulted in a 404 Error. Can anyone help me to find what the problem is?
Here is my app.yaml file:
runtime: python37 env: standard default_expiration: "5m" entrypoint: gunicorn -b :$PORT main:app --timeout 150 instance_class: F4 automatic_scaling: max_instances: 5 min_instances: 1 min_pending_latency: "5s" target_cpu_utilization: 0.75 inbound_services: - warmup handlers: - url: /static/js/(.*) static_files: build/static/js/\1 upload: build/static/js/(.*) - url: /static/css/(.*) static_files: build/static/css/\1 upload: build/static/css/(.*) - url: /static/media/(.*) static_files: build/static/media/\1 upload: build/static/media/(.*) - url: /(.*\.(json|ico))$ static_files: build/\1 upload: build/.*\.(json|ico)$ - url: / static_files: build/index.html upload: build/index.html - url: /.* script: auto
I am here to answer my own question. I seem to find the problem and how to solve it.
The main problem seems to be a caching issue. For the
app.yaml settings, although the default expiration time is set to
5m, the url with path don’t have the expiration set. For example, page
www.example.com/about will have a different caching time than the
js package. This means when a new build folder is deployed, the
js packages have been changed, but the
www.example.com/about page generated by your backend application is still the old version, and it will try to request the
js package from the previous build foler. Thus, causing the 404 error.
The way to solve this is to set the expiration time for your response generated by your backend application. I am using the Flask environment, so the code for that is (credited to this answer)
response["Cache-Control"] = "no-cache, no-store, must-revalidate" # HTTP 1.1. response["Pragma"] = "no-cache" # HTTP 1.0. response["Expires"] = "0" # Proxies.
Answered By – Leon
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0