Updating browser cache on flask

Viewed 297

I have a small flask server I'm running mostly for experimenting and tools I'm developing for self use (on my home network). It is running on development mode on a raspberry pi machine. It is configured to launch on startup via rc.local:

sudo -H -u pi /home/pi/Server/start.sh &

and the start.sh file reads

#!/bin/bash

cd /home/pi/Server
source /home/pi/Server/venv/bin/activate
export FLASK_APP=/home/pi/Server/app.py
export FLASK_ENV=development
export FLASK_RUN_HOST=192.168.1.104
export FLASK_RUN_PORT=5001

flask run

At the first couples of days everything was running fine, but now I get the following error:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/lib/python3/dist-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/lib/python3/dist-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python3/dist-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/usr/lib/python3/dist-packages/flask/app.py", line 2291, in wsgi_app
    ctx.push()
  File "/usr/lib/python3/dist-packages/flask/ctx.py", line 377, in push
    self.app, self.request
  File "/usr/lib/python3/dist-packages/flask/sessions.py", line 343, in open_session
    data = s.loads(val, max_age=max_age)
  File "/usr/lib/python3/dist-packages/itsdangerous.py", line 643, in loads
    .unsign(s, max_age, return_timestamp=True)
  File "/usr/lib/python3/dist-packages/itsdangerous.py", line 466, in unsign
    return value, self.timestamp_to_datetime(timestamp)
  File "/usr/lib/python3/dist-packages/itsdangerous.py", line 404, in timestamp_to_datetime
    return datetime.utcfromtimestamp(ts + EPOCH)
OverflowError: timestamp out of range for platform time_t

From what I see here This is an issue of browser cache. How can I tell flask to cope with this?

2 Answers

Looks like you're using sessions/cookies? Try looking into that, maybe the date isn't proper or invalid. Try clearing it session.clear() or use a shorter expiration date. I've also had issues after upgrading from python 2 to 3 that messed up the cookies, if you've done that, you need to clear your cache so python3 date/time cookies can be set.

This seems to be an error when time returned is 0 from this Adafruit CircuitPython NTP issue. A direct approach would be to patch some flask dependencies with a PR.

However this seems more to be an error with your cache age. Try reducing it to a short time

@app.after_request
def after_request(response):
    response.headers["Cache-Control"] = "max-age=300" # in second
    return response
Related