Skip to content

Commit 66af1c7

Browse files
committed
Make it possible to provide a proc for custom cache_control behaviour.
1 parent d60c353 commit 66af1c7

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

lib/utopia/static.rb

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,22 @@ def fetch_file(path)
5757
ETAG = "ETag".freeze
5858
ACCEPT_RANGES = "Accept-Ranges".freeze
5959

60+
def response_headers_for(file, content_type)
61+
if @cache_control.respond_to?(:call)
62+
cache_control = @cache_control.call(file)
63+
else
64+
cache_control = @cache_control
65+
end
66+
67+
{
68+
LAST_MODIFIED => file.mtime_date,
69+
CONTENT_TYPE => content_type,
70+
CACHE_CONTROL => cache_control,
71+
ETAG => file.etag,
72+
ACCEPT_RANGES => "bytes"
73+
}
74+
end
75+
6076
def respond(env, path_info, extension)
6177
path = Path[path_info].simplify
6278

@@ -65,13 +81,7 @@ def respond(env, path_info, extension)
6581
end
6682

6783
if file = fetch_file(path)
68-
response_headers = {
69-
LAST_MODIFIED => file.mtime_date,
70-
CONTENT_TYPE => @extensions[extension],
71-
CACHE_CONTROL => @cache_control,
72-
ETAG => file.etag,
73-
ACCEPT_RANGES => "bytes"
74-
}
84+
response_headers = self.response_headers_for(file, @extensions[extension])
7585

7686
if file.modified?(env)
7787
return file.serve(env, response_headers)

0 commit comments

Comments
 (0)