Merge pull request 'reverse_proxy,endpoint,uploaded_media: add immutable cache-control flag' (#7835) from Yonle/pleroma:develop into develop
Reviewed-on: https://git.pleroma.social/pleroma/pleroma/pulls/7835
This commit is contained in:
commit
9db47790bb
8 changed files with 150 additions and 5 deletions
|
|
@ -29,7 +29,7 @@ defmodule Pleroma.Constants do
|
|||
|
||||
const(static_only_files,
|
||||
do:
|
||||
~w(index.html robots.txt static static-fe finmoji emoji packs sounds images instance sw.js sw-pleroma.js favicon.png schemas doc embed.js embed.css)
|
||||
~w(index.html robots.txt static static-fe finmoji emoji packs sounds images instance sw.js sw-pleroma.js schemas doc embed.js embed.css)
|
||||
)
|
||||
|
||||
const(status_updatable_fields,
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ defmodule Pleroma.ReverseProxy do
|
|||
@keep_resp_headers @resp_cache_headers ++
|
||||
~w(content-length content-type content-disposition content-encoding) ++
|
||||
~w(content-range accept-ranges vary)
|
||||
@default_cache_control_header "public, max-age=1209600"
|
||||
@default_cache_control_header "public, max-age=1209600, immutable"
|
||||
@valid_resp_codes [200, 206, 304]
|
||||
@max_read_duration :timer.seconds(30)
|
||||
@max_body_length :infinity
|
||||
|
|
|
|||
|
|
@ -46,8 +46,9 @@ defmodule Pleroma.Web.Endpoint do
|
|||
plug(Pleroma.Web.Plugs.HTTPSecurityPlug)
|
||||
plug(Pleroma.Web.Plugs.UploadedMedia)
|
||||
|
||||
@static_cache_control "public, max-age=1209600"
|
||||
@static_cache_control "public, max-age=1209600, immutable"
|
||||
@static_cache_disabled "public, no-cache"
|
||||
@favicon_cache_control "public, max=age=86400, immutable" # cache for a day
|
||||
|
||||
# InstanceStatic needs to be before Plug.Static to be able to override shipped-static files
|
||||
# If you're adding new paths to `only:` you'll need to configure them in InstanceStatic as well
|
||||
|
|
@ -64,6 +65,15 @@ defmodule Pleroma.Web.Endpoint do
|
|||
}
|
||||
)
|
||||
|
||||
plug(Pleroma.Web.Plugs.FaviconPlug,
|
||||
at: "/",
|
||||
only: ["favicon.png"],
|
||||
cache_control_for_etags: @favicon_cache_control,
|
||||
headers: %{
|
||||
"cache-control" => @favicon_cache_control
|
||||
}
|
||||
)
|
||||
|
||||
plug(Pleroma.Web.Plugs.InstanceStatic,
|
||||
at: "/",
|
||||
gzip: true,
|
||||
|
|
|
|||
71
lib/pleroma/web/plugs/favicon_plug.ex
Normal file
71
lib/pleroma/web/plugs/favicon_plug.ex
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2026 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Web.Plugs.FaviconPlug do
|
||||
@behaviour Plug
|
||||
|
||||
@moduledoc """
|
||||
This is a shim to call `Plug.Static` but with runtime `from` configuration for instance favicon.
|
||||
|
||||
Serves default or custom favicon.png with cacheable cache-control.
|
||||
"""
|
||||
|
||||
import Plug.Conn, only: [put_resp_header: 3, send_resp: 3, halt: 1]
|
||||
|
||||
require Logger
|
||||
|
||||
def init(opts) do
|
||||
opts
|
||||
|> Keyword.put(:from, "__unconfigured_favicon_static_plug")
|
||||
|> Plug.Static.init()
|
||||
end
|
||||
|
||||
def call(%{request_path: "/favicon.png"} = conn, opts) do
|
||||
case find_favicon_dir() do
|
||||
{:ok, dir} ->
|
||||
call_static(conn, opts, dir)
|
||||
|
||||
:error ->
|
||||
# Favicon should always be available and this should never occur.
|
||||
# If it does, halt the pipeline before having unintended side-effects.
|
||||
Logger.error("No favicon.png found! Is the default favicon deleted?")
|
||||
|
||||
conn
|
||||
|> send_resp(404, "Not found")
|
||||
|> halt()
|
||||
end
|
||||
end
|
||||
|
||||
def call(conn, _) do
|
||||
conn
|
||||
end
|
||||
|
||||
defp find_favicon_dir do
|
||||
instance_dir = Pleroma.Config.get([:instance, :static_dir], "instance/static")
|
||||
instance_path = Path.join(instance_dir, "favicon.png")
|
||||
|
||||
priv_dir = Application.app_dir(:pleroma, "priv/static")
|
||||
priv_path = Path.join(priv_dir, "favicon.png")
|
||||
|
||||
cond do
|
||||
File.exists?(instance_path) -> {:ok, instance_dir}
|
||||
File.exists?(priv_path) -> {:ok, priv_dir}
|
||||
true -> :error
|
||||
end
|
||||
end
|
||||
|
||||
defp call_static(conn, opts, from) do
|
||||
opts =
|
||||
opts
|
||||
|> Map.put(:from, from)
|
||||
|> Map.put(:content_types, false)
|
||||
|
||||
conn = set_content_type(conn)
|
||||
Plug.Static.call(conn, opts)
|
||||
end
|
||||
|
||||
defp set_content_type(conn) do
|
||||
put_resp_header(conn, "content-type", "image/png")
|
||||
end
|
||||
end
|
||||
|
|
@ -17,7 +17,7 @@ defmodule Pleroma.Web.Plugs.UploadedMedia do
|
|||
# no slashes
|
||||
@path "media"
|
||||
|
||||
@default_cache_control_header "public, max-age=1209600"
|
||||
@default_cache_control_header "public, max-age=1209600, immutable"
|
||||
|
||||
def init(_opts) do
|
||||
static_plug_opts =
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue