feat: reply trailers support#3794
feat: reply trailers support#3794mcollina merged 15 commits intofastify:mainfrom climba03003:feat-trailers
Conversation
I feel like it's already supporting an asynchronous operation that by the time this callback is invoked all the inputs should be available to calculate the trailing values you need. At the same time, I can see it useful to support async callbacks for cases where someone might want to use an api call in order to calculate a value you want to send in a trailing header. I think this would make it most flexible, but would also give more rope to the framework user to do lots of extraneous network calls before resolving a header value. I suppose its not necessarily terrible since the response to the client has already been sent. |
I can think of a solution for At least, I need somebody help on the area of |
|
I'm not sure this is needed to solve the etag case. In the vast majority of cases we would want to return a 304 - the content can be cached. In that case you'd need to compute the etag before sending it. This is not very efficient. As you can read at https://serverfault.com/questions/690341/algorithm-behind-nginx-etag-generation, NGINX does not read the full file to compute the etag. I have never found a good use case for trailers and in practices they are not very commonly used. |
I never use the I am good with the current proposal for the minimal support of |
|
That's cool! fastify.get('/', function (request, reply) {
reply.addTiming('reqLifecycle', rrequest.RequestLifecyleTime)
reply.addTiming('routingTime', request.RoutingTime)
reply.send(data)
})Edit: ok there already is a Server-Timing plugin my bad |
This is from MDN: There are two types of etags, weak and strong. Date and content length hashes are a good approach at generated weak etags. But if some content has a one character typo that is correct then, for example, a base64 hash of date+contentLength would not be great. In regards to having to read the full file contents up front, that scenario is only necessary if the client has sent a |
Considering how CDNs works, the |
I'm not sure what you mean here, I shouldn't be serving files from node.js? This is confusing, isn't that what fastify.js is basically facilitating here? And yes, if-match/if-none-match are the most common cases, how do you suppose the CDN validates the etags it has cached with the etags its receiving from clients like browsers if not to ask an origin server that may or may not be running fastify.js? I'm confused as to what you're implying. |
NGINX and others provide better performance for serving files from disk.
Browser (not cached) -> CDN (cached) -> Therefore you are almost always hitting the case of having to compute the etag before starting to send the content. Doing it as a trailer only adds complexity with extremely limited gain. Finally quite a few CDNs do not support trailers. |
|
I'm trying for that gain as this does not apply to a simple single website. Our cluster runs lots of websites and we incur millions of requests per half hour, so small incremental gains all add up quickly, which is why I was trying to do it with this approach, and our CDN supports them just fine. We also use fastify.js because our entire application and build pipeline is entirely javascript, and our web server doesn't only serve static files, that is just a portion of the application. |
docs/Reference/Reply.md
Outdated
| ### .trailer(key, function) | ||
| <a id="trailer"></a> | ||
|
|
||
| Sets a response trailer. |
There was a problem hiding this comment.
Please
document in which moments we call the functions as well as that the functions must be synchronous.
Co-authored-by: Tomas Della Vedova <delvedor@users.noreply.github.com>
|
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Closes #630
Some question
payloadasnullwhenstreamis sent?payload.on('end')correct forstreamhandling and add trailers?traileronly support sync function?Checklist
npm run testandnpm run benchmarkand the Code of conduct