Skip to content
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

feat: add content-length headers to generated responses, via new text(...) helper #8371

Merged
merged 9 commits into from Jan 19, 2023

Conversation

Rich-Harris
Copy link
Member

closes #5749.

This is just an idea; I don't know if it's the right one. In order to make it easier to determine response content length inside handle, for logging purposes, this adds a new text(body) helper similar to the json(data) helper. It returns a Response object with a content-length header, and uses it in favour of new Response(...) when generating pages and other responses.

One observation: it'd be nice if we could unit-test Server rather than having Playwright tests for the bulk of the tests in server.test.js. Might look into that separately.

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. All changesets should be patch until SvelteKit 1.0

@changeset-bot
Copy link

changeset-bot bot commented Jan 6, 2023

🦋 Changeset detected

Latest commit: 4e36022

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link

vercel bot commented Jan 7, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
kit 🔄 Building (Inspect) Jan 9, 2023 at 9:32AM (UTC)
kit-svelte-dev 🔄 Building (Inspect) Jan 9, 2023 at 9:32AM (UTC)

Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach makes sense to me

packages/kit/types/index.d.ts Outdated Show resolved Hide resolved
Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok maybe it doesn't make sense..? This stackoverflow answer makes me kinda nervous - it seems that compression can alter the content length, and I'm not sure what bugs we might introduce if we set the uncompressed size

@Rich-Harris
Copy link
Member Author

Ah, that's a good point. I would have assumed that if something in between SvelteKit and the user were to compress the body it would generate a new response with new headers, but I'll freely confess my ignorance here. Safest thing would be to close #5749 as wontfix but that seems unsatisfying.

Am inclined to just leave this here until someone with more authority weighs in

@bjon
Copy link
Contributor

bjon commented Jan 9, 2023

@dummdidumm Google's dynamic compression (CDN) requires the response to have content-length. Without it, no compression is done. https://cloud.google.com/cdn/docs/dynamic-compression#response-not-compressed

It's was no problem to add content-length in sveltekit manually. Maybe the text() and json() could have an optional addContentLength: boolean arg, or something similar? It would be great to have kit handle it.

@Conduitry
Copy link
Member

If there's some library or something that compresses responses on the fly but doesn't correctly update the content-length header, that's certainly a bug in that library. I think we should only worry about that if there were some extremely popular library that did that that seemed extremely unlikely to want to fix that bug.

@Rich-Harris
Copy link
Member Author

I don't think there's much value in having an addContentLength option — it doesn't cost anything extra for us, and I can't imagine any cases where you wouldn't want the header. Honestly I'm a little surprised it doesn't get added automatically (if you do new Response('hello') it'll add a content-type: text/plain;charset=UTF-8 header, but not content-length — what the hell?)

So it sounds like the consensus is in favour of merging this?

Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Rereading the posted Stackoverflow answer, I noticed that the concern there is the other way around - someone's reading the response text and comparing that with the length. So this is the other end, consumer (user in the browser for our case), not producer (our server).

That said - any way we can merge another maybe bit nicer feature before releasing this? 😄 Having 1.1 just for this feels wasted. Not sure how much we care about minor version marketing at this point.

@dummdidumm dummdidumm changed the title add content-length headers to generated responses, via new text(...) helper feat: add content-length headers to generated responses, via new text(...) helper Jan 12, 2023
@Rich-Harris Rich-Harris merged commit 06a56ae into master Jan 19, 2023
@Rich-Harris Rich-Harris deleted the gh-5749 branch January 19, 2023 23:13
@github-actions github-actions bot mentioned this pull request Jan 19, 2023
cedricr added a commit to gip-inclusion/dora-front that referenced this pull request Feb 10, 2023
…Kit (#184)

Après le passage de SvelteKit de 1.0.0 en 1.5.2, Chrome reportait des
erreurs de Content-Length systématiques, qui bloquaient le reste du
chargement.

Le changement en cause: sveltejs/kit#8371
SvelteKit, depuis la version 1.2.0, rajoute un en-tête Content-Length à
la réponse, or dans

https://github.com/betagouv/dora-front/blob/571f7b9033510dec8acd362c8825650c755fc5c7/src/hooks.server.ts#L34
on réécrivait le contenu sans mettre à jour sa taille.

Depuis 1.2.0, il est également possible d'accéder aux variables
d'environnement directement dans le app.html, ce qui était la raison de
la réécriture jusque là:
sveltejs/kit#8449
Cette PR remplace donc l'ancien mécanisme de réécriture par l'officiel.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Way to access the response body size in the hook
5 participants