Forums

local browser cache

to speed up my website I want to trigger that the local browser uses its local cache, I would like to send the status code 304 to the browser, but I cant get it to work. i am only using web2py

How is it not working?

I send a http request with a parameter so i can check in my application what the browser currently is showing, using request.vars.currentdataid

if the data has not changed I want to send back a short as possible response to reduce the amout of data that flows to the client So from my application I try to send back a response.status=304 in case there has not been any change to the page, the intention is that the browser will load it again from its local cache or leave the current content

the intention is to create an auto reload page that uses as less as possible load on the server and minimal data flow

Ok. That sounds good. Do that.

so this is what I tried:

in the controller i put the following code:

mtime = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime() )
response.headers["Last-Modified"] = mtime
response.headers["Expires"] = "Fri, 09 Oct 2016 20:20:57 GMT"
response.headers["Pragma"] = "cache"
response.headers["Cache-Control"] = "private"
if <some check if there is new data>:
    response.status = 304 # optimize traffic 304
    return "304 TO BE SEND"
else:
    return "200 TO BE SEND"

when i try this out i always get a 200 never a 304 as status code, while i get the correct text "304 TO BE SEND" in the browser

finally after a Ctrl F5 I got the 304 back without content in the response, so that is as expected, but pitty the browser cleans the content and does not show it's local cache. I will try some other browsers.

this is because i use a parameter in the URL that changes, now i made it constant, and this works in my trail program. The URL have to sty exactly the same.

Cool. Glad you worked it out.