Skip to content

Custom static file handling: handle_response_inner is private, leading to excessive code duplication #3996

Closed
@NCura

Description

@NCura

Hi! I'm using Axum with Leptos and wanted to customize how static files are served, specifically to add a Cache-Control header.

I saw that I could replace:

.fallback(leptos_axum::file_and_error_handler::<AppState, _>(shell))

with my own custom version. After checking the source, I found this helpful comment in file_and_error_handler:

A reasonable handler for serving static files (like JS/WASM/CSS) and 404 errors.
This is provided as a convenience, but is a fairly simple function. If you need to adapt it, simply reuse the source code of this function in your own application.

That’s exactly what I needed. I wanted to tweak the get_static_file function to include a CACHE_CONTROL header, so I:

  1. Created a fileserv.rs file.

  2. Copy-pasted the implementations of:

    • file_and_error_handler
    • file_and_error_handler_with_context
    • get_static_file

However, I quickly hit a snag, file_and_error_handler_with_context calls a private function: handle_response_inner. That meant I also had to copy that function, and now I’m up to 241 lines of mostly unchanged code just to make a minor adjustment to one function.

It works well, but I’m wondering:

  • Is there a specific reason handle_response_inner is private?
  • Would you accept a PR making it public so customization like mine requires less duplication?
  • Or is the current expectation to copy-paste all relevant source if a change is needed?

Thanks for your time!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions