Skip to content

Commit

Permalink
Add support for static resources with fragments. Closes #5507
Browse files Browse the repository at this point in the history
  • Loading branch information
phoebe100 committed Jul 27, 2023
1 parent 97f7ceb commit a4fad82
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/phoenix/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,20 @@ defmodule Phoenix.Endpoint do
@doc """
Generates a route to a static file in `priv/static`.
"""
def static_path(path), do: persistent!().static_path <> elem(static_lookup(path), 0)
def static_path(path) do
{path, fragment} = path_and_fragment(path)

persistent!().static_path <> elem(static_lookup(path), 0) <> fragment
end

defp path_and_fragment(path_incl_fragment) do
path_incl_fragment
|> String.split("#", parts: 2)
|> case do
[path, fragment] -> {path, "#" <> fragment}
[path | _] -> {path, ""}
end
end

@doc """
Generates a base64-encoded cryptographic hash (sha512) to a static file
Expand Down
12 changes: 12 additions & 0 deletions test/phoenix/endpoint/endpoint_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,18 @@ defmodule Phoenix.Endpoint.EndpointTest do
assert Endpoint.static_path("/foo.css") == "/foo-d978852bea6530fcd197b5445ed008fd.css"
end

test "uses correct path for resources with fragment identifier" do
config = put_in(@config[:cache_manifest_skip_vsn], false)
assert Endpoint.config_change([{Endpoint, config}], []) == :ok

assert Endpoint.static_path("/foo.css#info") ==
"/foo-d978852bea6530fcd197b5445ed008fd.css?vsn=d#info"

# assert that even multiple presences of a number sign are treated as a fragment
assert Endpoint.static_path("/foo.css#info#me") ==
"/foo-d978852bea6530fcd197b5445ed008fd.css?vsn=d#info#me"
end

@tag :capture_log
test "invokes init/2 callback" do
defmodule InitEndpoint do
Expand Down

0 comments on commit a4fad82

Please sign in to comment.