Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@otthe
Copy link
Contributor

@otthe otthe commented Apr 5, 2025

Description

This PR implements recent feature request related to static file responses. It introduces a new configuration setting, :static_headers, which allows developers to define custom headers that will be applied to all static file responses served by static! -method.

Why?

Fixes #2088
Sinatra serves static files directly via static!, bypassing filters and middleware. This makes it so that there is no good ways to add headers like Access-Control-Allow-Origin, which are often needed for CORS access (e.g., when using fonts or images on canvas).

@otthe otthe force-pushed the static-headers-clean branch from a81d277 to fc4868e Compare April 8, 2025 06:30
Copy link
Member

@dentarg dentarg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're in a Rack 3 world now, so please user lowercase for all header keys :-)

Comment on lines 1160 to 1164
if settings.respond_to?(:static_headers) && settings.static_headers
settings.static_headers.each do |k, v|
headers[k] = v
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, there's no need to loop

Suggested change
if settings.respond_to?(:static_headers) && settings.static_headers
settings.static_headers.each do |k, v|
headers[k] = v
end
end
headers(settings.static_headers) if settings.static_headers?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add this remove before or after cache_control(*settings.static_cache_control) if settings.static_cache_control??

If we add it before, static_cache_control have precedence, if we add it after, you can overwrite headers like Cache-Control, with static_headers. What do we want?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question, personally I think that calling headers(settings.static_headers) after Cache-Control makes more sense, since it gives the users more control. It feels more explicit and flexible that way, at least to me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable to me

…d, add static_headers to available settings on readme
@otthe
Copy link
Contributor Author

otthe commented Apr 24, 2025

Thanks for the feedback, made the changes you suggested. Let me know if there is something else to be addressed.

Copy link
Member

@dentarg dentarg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To encourage the style of lowercase headers

@dentarg dentarg merged commit 91cfb54 into sinatra:main Apr 25, 2025
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Static file responses don't allow setting CORS headers (e.g., Access-Control-Allow-Origin)

2 participants