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

Control any response headers from a component #18

Open
egil opened this issue Apr 26, 2024 · 0 comments
Open

Control any response headers from a component #18

egil opened this issue Apr 26, 2024 · 0 comments

Comments

@egil
Copy link
Owner

egil commented Apr 26, 2024

Htmxor should include a generic way for developers to declaratively influence any headers during when returning a response to a request, not just the htmx specific headers that are exposed via the HtmxContext.Response, aka. HtmxResponse object.

There are two scenarios in play:

Headers that are dynamically set based on request or dynamic content:
Right now, developers can [Inject] private HttpContext Ctx in a component and set response headers through that. For convenience, this could also be enabled through a generic Headers property on the HtmxResponse type, which would just point to the HtmlContext.Response.Headers dictionary.

Headers that are statically determined at compile time:
These are good candidates for a declarative approach. My idea is for Htmxor to include a IControlResponseHeaders interface, that any attribute can implement. Then, developers can implement custom attributes that implement this interface and use these attributes in their components declaratively.

Here is the interface definition:

public interface IControlResponseHeaders
{
    void Apply(IHeaderDictionary headers);
}

And an example attribute:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
public sealed class ContentLanguageAttribute(string language) : Attribute, IControlResponseHeaders
{
  public void Apply(IHeaderDictionary headers)
  {
    headers["Content-Language"] = language;
  }
}

This can then be used like this:

@attribute [ContentLanguage("da-DK")]
@page "/da/home"
...

Inspired by: https://mastodon.social/@khalidabuhakmeh/112339241113769565

@egil egil mentioned this issue May 14, 2024
@egil egil changed the title Control response headers through attribute on component Control any response headers from a component May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant