Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for static resources with fragments. Closes #5507 #5510

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading