Skip to content

Commit 9c767a2

Browse files
cache preload
1 parent 6f8b671 commit 9c767a2

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

car/app.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# stdlib
2+
import functools
23
import itertools
34
import json
45
import os
@@ -14,6 +15,7 @@
1415
abort,
1516
flash,
1617
g,
18+
make_response,
1719
redirect,
1820
render_template,
1921
request,
@@ -67,6 +69,18 @@
6769
geoturfs = json.load(f)
6870

6971

72+
def browser_cache(f):
73+
@functools.wraps(f)
74+
def wrapped(*a, **k):
75+
r = f(*a, **k)
76+
resp = make_response(r)
77+
if request.headers.get("HX-Preloaded"):
78+
resp.headers["Cache-Control"] = "private; max-age=60"
79+
return resp
80+
81+
return wrapped
82+
83+
7084
@app.before_request
7185
def before_request():
7286
g.phonebank = False
@@ -387,6 +401,7 @@ def finish_turf(id):
387401

388402

389403
@app.route("/door/<int:id>/")
404+
@browser_cache
390405
def show_door(id: ID):
391406
door = db.get_door_by_id(id)
392407
if door.turf_id is None:
@@ -493,6 +508,7 @@ def door_act(id: ID):
493508

494509

495510
@app.route("/voter/<int:id>/")
511+
@browser_cache
496512
def show_voter(id: ID):
497513
voter = db.get_voter_by_id(id)
498514

@@ -581,6 +597,7 @@ def thing_title(model: Model) -> str:
581597

582598

583599
@app.route("/<typ>/<int:id>/note/", methods=["GET", "POST"])
600+
@browser_cache
584601
def note_obj(typ: str, id: ID):
585602
if typ == "turf":
586603
restrict_turfs(id)

0 commit comments

Comments
 (0)