Skip to content

Commit

Permalink
add commit to static asset URLs to invalidate browser cache on deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
azuline committed Jun 24, 2024
1 parent 6979116 commit 138d96b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
37 changes: 28 additions & 9 deletions scripts/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ def parse(cls, slug: str, d: dict[str, Any]) -> PostMeta:
@functools.cached_property
def lastupdated(self) -> datetime | None:
p = f"src/posts/tex/{self.slug}.tex"
r = subprocess.run(["git", "log", "-1", "--format=%cI", p], capture_output=True, text=True)
r = subprocess.run(
["git", "log", "-1", "--format=%cI", p],
check=True,
capture_output=True,
text=True,
)
text = r.stdout.strip()
if not text:
return None
Expand All @@ -62,7 +67,12 @@ def lastupdated(self) -> datetime | None:


def site_updated_at() -> datetime:
r = subprocess.run(["git", "log", "-1", "--format=%cI"], capture_output=True, text=True)
r = subprocess.run(
["git", "log", "-1", "--format=%cI"],
check=True,
capture_output=True,
text=True,
)
text = r.stdout.strip()
return datetime.fromisoformat(text).astimezone(pytz.utc)

Expand All @@ -87,23 +97,24 @@ def empty_dist() -> None:
raise Exception(f"{f} is not a file or directory.")


def compile_index(posts: PostIndex):
def compile_index(posts: PostIndex, commit: str):
with Path("src/index.jinja").open("r") as fp:
tpl = je.from_string(fp.read())

# Write the main index.html
publicposts = {k: v for k, v in reversed(posts.items()) if v.public}
index = tpl.render(posts=publicposts)
index = tpl.render(posts=publicposts, commit=commit)
with Path("dist/index.html").open("w") as fp:
fp.write(index)

# Write a staging index.html
staging = tpl.render(posts=dict(reversed(posts.items())))
stagingposts = dict(reversed(posts.items()))
staging = tpl.render(posts=stagingposts, commit=commit)
with Path("dist/staging.html").open("w") as fp:
fp.write(staging)


def compile_posts(posts: PostIndex):
def compile_posts(posts: PostIndex, commit: str):
Path("dist/posts").mkdir()
with Path("src/posts/template.jinja").open("r") as fp:
tpl = je.from_string(fp.read())
Expand Down Expand Up @@ -139,7 +150,7 @@ def compile_posts(posts: PostIndex):
# Wrap the post with a Jinja template.
slug = f.stem
meta = posts[slug]
post = tpl.render(slug=slug, meta=meta, body=post)
post = tpl.render(slug=slug, meta=meta, body=post, commit=commit)

# Write the compiled post.
with Path("dist/posts", f.parts[-1]).with_suffix(".html").open("w") as fp:
Expand Down Expand Up @@ -179,10 +190,18 @@ def main():
with Path("src/posts/index.json").open("r") as fp:
posts = {k: PostMeta.parse(k, v) for k, v in json.load(fp).items()}

r = subprocess.run(
["git", "rev-parse", "--short", "HEAD"],
check=True,
capture_output=True,
text=True,
)
commit = r.stdout.strip()

empty_dist()
shutil.copytree("src/assets", "dist/assets")
compile_index(posts)
compile_posts(posts)
compile_index(posts, commit)
compile_posts(posts, commit)
compile_feed(posts)


Expand Down
2 changes: 1 addition & 1 deletion src/head.jinja
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="/assets/css/global.css" />
<link rel="stylesheet" type="text/css" href="/assets/css/global.css?v={{ commit }}" />
<link href="https://sunsetglow.net/atom.xml" type="application/atom+xml" rel="alternate" title="Atom feed" />
<script defer src="https://ozu.sunsetglow.net/script.js" data-website-id="6519316e-0391-4a7b-ae2a-f308d136b119"></script>
2 changes: 1 addition & 1 deletion src/index.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
{% include "head.jinja" %}
<title>sunset glow</title>
<link rel="stylesheet" type="text/css" href="/assets/css/index.css" />
<link rel="stylesheet" type="text/css" href="/assets/css/index.css?v={{ commit }}" />
</head>
<body>
<div class="flex flex-col gap-2 p-4">
Expand Down
2 changes: 1 addition & 1 deletion src/posts/template.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
{% include "head.jinja" %}
<title>{{ meta.title }}</title>
<link rel="stylesheet" type="text/css" href="/assets/css/post.css" />
<link rel="stylesheet" type="text/css" href="/assets/css/post.css?v={{ commit }}" />
</head>
<body>
<div class="w-content mx-auto p-8">
Expand Down

0 comments on commit 138d96b

Please sign in to comment.