Skip to content

Commit

Permalink
Enable SVG preview caching
Browse files Browse the repository at this point in the history
  • Loading branch information
ku1ik committed May 5, 2024
1 parent df926d8 commit 12af104
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/asciinema_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ defmodule AsciinemaWeb do
import AsciinemaWeb.Auth, only: [require_current_user: 2]
import AsciinemaWeb.Plug.ReturnTo
import AsciinemaWeb.Plug.Authz
import AsciinemaWeb.Caching

unquote(verified_routes())

Expand Down
17 changes: 17 additions & 0 deletions lib/asciinema_web/caching.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule AsciinemaWeb.Caching do
import Plug.Conn

def put_etag(conn, content) do
etag = Crypto.md5(to_string(content))

conn
|> put_resp_header("etag", etag)
|> register_before_send(fn conn ->
if etag in get_req_header(conn, "if-none-match") do
send_resp(conn, 304, "")
else
conn
end
end)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
class="thumbnail"
style={"background-color: #{theme(@asciicast).bg}; border-right: 1ch solid #{theme(@asciicast).bg}"}
>
<img src={~p"/a/#{@asciicast}" <> ".svg?v=t"} />
<img src={~p"/a/#{@asciicast}" <> ".svg?f=t&v=#{svg_cache_key(@asciicast)}"} />
</div>
11 changes: 9 additions & 2 deletions lib/asciinema_web/controllers/recording_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule AsciinemaWeb.RecordingController do
alias Asciinema.{Recordings, PngGenerator}
alias Asciinema.Recordings.Asciicast
alias AsciinemaWeb.PlayerOpts
alias AsciinemaWeb.RecordingHTML

plug :clear_main_class
plug :load_asciicast when action in [:show, :edit, :update, :delete, :iframe]
Expand Down Expand Up @@ -102,6 +103,9 @@ defmodule AsciinemaWeb.RecordingController do
end
end

# 1 hour
@svg_max_age 3600

def do_show(conn, "svg", asciicast) do
if asciicast.archived_at do
path = Application.app_dir(:asciinema, "priv/static/images/archived.png")
Expand All @@ -112,12 +116,15 @@ defmodule AsciinemaWeb.RecordingController do
|> send_file(200, path)
else
variant =
case conn.params["v"] do
case conn.params["f"] do
"t" -> :thumbnail
_ -> :show
end

render(conn, variant, asciicast: asciicast)
conn
|> put_resp_header("cache-control", "public, max-age=#{@svg_max_age}, must-revalidate")
|> put_etag(RecordingHTML.svg_cache_key(asciicast))
|> render(variant, asciicast: asciicast)
end
end

Expand Down
3 changes: 3 additions & 0 deletions lib/asciinema_web/controllers/recording_html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ defmodule AsciinemaWeb.RecordingHTML do
asciicast.views_count
end

def svg_cache_key(asciicast),
do: Timex.to_unix(asciicast.updated_at) - Timex.to_unix(asciicast.inserted_at)

def head("show.html", assigns), do: head_for_show(assigns)
def head(_, _), do: nil
end

0 comments on commit 12af104

Please sign in to comment.