Use headers from thrown Responses (v3_singleFetch) #13391
Replies: 1 comment
-
I chatted with @ryanflorence on this and I don't think we're going to adopt this. You nailed it above in that the real solution here for "libraries/utilities that want to throw responses to short circuit" is to do it from middleware. It would just introduce churn to add breaking behavior behind a future flag, that only served a short-term solution until middleware was adopted. Headers behavior is, tbh, already a bit tricky - but at least it's consistent now with single fetch in that It's worth noting that your "pre single-fetch" example above was has a generally unnoticed bug in that your headers were present on data requests but not on document requests. |
Beta Was this translation helpful? Give feedback.
-
With
v3_singleFetch
enabled, routes must add a headers export if a loader or action throws or returns Responses with headers that matter. This makes the DX a little tricky for packages that throw Responses.Headers & single_fetch
Pre
single_fetch
:I believe this must change post
single_fetch
to this:The Package use case
Where it gets a little nuanced is a package which throws responses with use case critical headers. For example:
Here 2 things could be throwing a Response that is use-case critical without it being obvious:
authenticate(request)
context.doSomething()
If the consumer of the package does not realize that these 2 functions can throw Responses, or if they forget to add the headers export, functionality could break. If the functionality is not encountered during development (call it an edge case), it seems easy to ship breaking code to production.
Today's solution is for the package to provide a function that manages headers and for the developer to remember a headers export. For example:
But this has some downsides. The package author must educate the developer that this is required, and when. A package could provide a linting rule, but there is no guarantee a developer uses it. Even if the developer does use the linting rule and
manageHeaders
, it seems possible that a complex headers function may still drop the required headers in a way that would be difficult to lint against.The Proposal
This would ensure that code pre
single_fetch
still works postsingle_fetch
:There may be a version of this where if the headers export is present, it is used rather than the automatic behaviour I've proposed above. This has benefits:
The downside is that If a developer adds a headers export, but forgets to merge headers thrown by the loader/action, use-case ciritical headers provided by the package may be lost.
Middleware
Middleware may be the eventual best-case end-state solution here since they have ultimate control over the Response. In the example above
authenticate
andcontext.doSomething
could add headers to Context for a Middleware to read.But what I'm proposing here gives package authors adopting
v3_singleFetch
the ability to enable the same DX they offer developers today, without forcing a migration to Middleware.Beta Was this translation helpful? Give feedback.
All reactions