I have set up a website that uses Flask, nginx and uWSGI.
The website needs to always show the most up to date information. I have been testing that by changing the theme of my website. I made this theme work by storing the hex value in a JSON file and then retrieving that when my app.py is first called. I update the global theme_colour variable whenever they save the config. I then pass theme_colour into each render_template and within the HTML I have a style tag that contains something like this
h1 {
color: {{ theme_colour }}
}
My problem is that when I update the theme, it should instantly change all pages but it does not. When I update it on the config page it automatically reloads the page and the change works there, but when I navigate to another page it's using the old colour. Sometimes refreshing the page fixes it, but then randomly swapping between pages will often result in it swapping back to the old colour for some reason. Clearing the cache and refreshing it will fix it on that page, but then navigating to another page and back it often swaps back to the old colour.
I can't figure out what is going on here. I didn't have any issues with the theme before I hosted the website properly (it was just on the dev server before). I tried to eliminate caching like this but it clearly hasn't worked:
server {
listen 80 default_server;
server_name mydomain.com www.mydomain.com;
location / {
include uwsgi_params;
uwsgi_pass unix:/home/ubuntu/test/test.sock;
# kill cache
sendfile off;
add_header Last-Modified $date_gmt;
add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
if_modified_since off;
expires off;
etag off;
proxy_no_cache 1;
proxy_cache_bypass 1;
}
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl default_server; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot#
server_name mydomain.com www.mydomain.com;
location / {
include uwsgi_params;
uwsgi_pass unix:/home/ubuntu/test/test.sock;
# kill cache
sendfile off;
add_header Last-Modified $date_gmt;
add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
if_modified_since off;
expires off;
etag off;
proxy_no_cache 1;
proxy_cache_bypass 1;
}
}
Whenever I send a GET request it says the response headers contain Cache-Control: no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0
so I'm not sure that this is the problem.
Here is all of my theme_colour code:
@app.route("/index")
def load_index():
if check_logged_in(session, 0): # if user is logged in, doesn't require elevated privileges
return render_template("index.html", theme_colour=theme_colour) # logged in
else:
return redirect("/login") # not logged in
@app.route("/config", methods=["POST", "GET"])
def load_config():
if check_logged_in(session, 2): # if user is logged in, does require elevated privileges
if request.method == "POST": # if user clicks update button
global theme_colour # make theme colour accessible to all pages
config = list(request.form.values()) # get input box values
save_config(config) # save values to json
theme_colour = update_theme() # find theme colour and update global variable
return render_template("config.html", theme_colour=theme_colour)
else:
return abort(403) # forbidden if not logged in
def update_theme(): # get current theme colour
with open("config.json", "r") as json_file:
data = json.load(json_file)
theme_colour = data["colour"]
return theme_colour
theme_colour = update_theme()
I feel like I am missing something here. Does anyone have any ideas?
Thanks.