-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Way to access the response body size in the hook #5749
Comments
|
Thanks for the response @benmccann (see what I did there 😂). Anyways, what if Sveltekit set the |
This is terrible and you shouldn't use it, but it can go in the hooks handle (really what you already mentioned, I realize after re-reading): const bytes = await response.clone().arrayBuffer()
console.log('response size:', bytes.byteLength) You might be able to avoid the clone impact somewhat by returning your own streaming response that you pipe the current response to, counting the size as you do. |
In the case where you're rendering a page, you can do this: let length = 0;
const response = await resolve(event, {
transformPageChunk({ html }) => {
length += html.length;
return html;
}
}); (In future if we add a As of #5748, non-page responses must be generated by you, the app author — we don't have a way to inspect the |
@Rich-Harris Thanks for looking into this. I suppose there are 3 cases here:
Getting the content size for (1) pages is taken care of by As a tangent, I'm grateful for the thoughtful changes in #5748. I like the new distinction between standalone endpoints (returning a |
…t(...)` helper (#8371) * add content-length headers to generated responses, via new `text(...)` helper - closes #5749 * correctly determine length * fix * Update packages/kit/types/index.d.ts * Update .changeset/nine-walls-listen.md * Update .changeset/old-weeks-reflect.md Co-authored-by: Simon H <[email protected]> Co-authored-by: Ben McCann <[email protected]>
Describe the problem
I want to set up logging in my sveltekit hook. One of the things I want to log is the number of bytes in the response body. It would be nice if the
Response
exposed the body length so that we can access it. Something like this...I notice that the length is somewhere on the object if I just
console.log(response)
when returning JSON......but that piece of data is private (attached to a symbol) and I can't access it.
In my opinion, being able to observe information like this in my hook is important to building debuggable, observable web services. Response payload size is an important metric to monitor. Putting that logging logic in the hook seems to be the only way to make sure it is reliably logged.
Describe the proposed solution
The workaround I have right now is
await response.clone().text()
, but that is not ideal because I have to consume the stream. This is wasteful especially if I only want the length.Alternatives considered
I suppose I could do something with
Object.getOwnPropertySymbols()
, but I think that's frowned upon?Importance
would make my life easier
Additional Information
As I understand it (may be wrong), the content length isn't necessarily always known without reading the stream, but there are many cases where the length is known at the time of the response instantiation, like when JSON or HTML are being returned from a string in memory. Sveltekit should be able to set the length in cases where length is known.
Here is a simple case where content length is known but not exposed: https://stackblitz.com/edit/sveltejs-kit-template-default-9ydsbu?file=src%2Fhooks.js&terminal=dev
The text was updated successfully, but these errors were encountered: