piranha / twinspark-js

Declarative enhancement for HTML: simple, composable, lean.
https://twinspark.js.org/
MIT License
421 stars 20 forks source link

Handle Content-Disposition requests and allow to manually download the response #18

Closed ckald closed 1 year ago

ckald commented 2 years ago

We were implementing an endpoint that validates a form and generates a file. Turns out, twinspark could not handle Content-Disposition headers and always requires the HTML response.

I've implemented a simple workaround that automatically detects Content-Disposition and stops the request handling. Also I added a way to override this behavior or download the response content that does not come with Content-Disposition.

We do not work with complicated batched requests, so please take a look if this makes sense?

piranha commented 2 years ago

Hmmm... I agree that detecting Content-Disposition would be nice, but I'm thinking if there is a way to handle that simpler than with a click on button. Honestly this feels like too much code for an issue like that.

Htmx decided to just ignore the issue: https://github.com/bigskysoftware/htmx/issues/474

ckald commented 2 years ago

They say it is complicated for them. It seems to work for me 🤷 They advise to turn off htmx for such a form -- but we are implementing a form that either validates or generates a file. I think it's a normal use case. Implementing this endpoint in other way would probably require to store the file on disk, which will require lots of additional backend code.

I don't know of another way to download the XHR response using JS. Data-urls are not that well supported.

piranha commented 2 years ago

Yeah it makes sense if you generate file during the http request... Let me think about it a bit.

piranha commented 2 years ago

Sorry, after thinking about this, I decided against inclusion of code for handling this case. What I can propose instead is maybe reworking backend to handle this, or adding an extension point so it could be done with a bit of custom js.