Some CDNs (in my case keyCDN) need the content length header to be able to cache the response.
The first question is why this is missing as a normal Drupal HTML response has this header.
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | jsonapi-3040738-5.patch | 694 bytes | ikphilip |
Comments
Comment #2
wim leersInteresting!
This is the first time I hear that
Content-Lengthis required for a CDN to be able to cache a response — this does not make sense I think, but 🤷♂️ Are you absolutely certain?I would think that Symfony should be doing this for us automatically, but apparently it is not. Could you investigate why Symfony doesn't do this?
Comment #3
yobottehg commentedYes thats true but i got it from their support because i ask why my server response is not cacheable for the their CDN.
So yes i am :)
I'll try to investigate in the next days. I'll assign this to me.
Comment #4
wim leersYay, thanks! It shouldn’t be too difficult to add this :)
Comment #5
ikphilip@yobottehg I looked into this. There's a few things.
You will not have a content-length header if transfer-encoding header is set.
Symfony\Component\HttpFoundation\Response
But I could not track where transfer-encoding is set in Drupal so this code is not executed
I looked at my Apache config and realized it will set Transfer-Encoding header before delivering a response depending on if the response body is compressed or not(?). I wonder if this is can be solved in http server settings instead of Drupal.
If you desire a patch for your JSONAPI responses then it would be attached. However the value not be accurate if you're transfer-encoding is chunked or otherwise when it's dispatched from the http server. So I would not recommend adding a content-length based on what I've read.
Comment #6
wim leersDrupal/Symfony also don't do this for HTML responses. Caching HTML responses on a CDN is far more common. Yet I haven't seen complaints about that. I don't think this problem is JSON:API-specific.
So I searched some more. And there is an issue for the general problem, and apparently I reviewed the patch over there too. It's been blocked on tests for over 2.5 years now: #2579775: Missing Content-Length header blocks response until all registered shutdown functions execute and the Kernel terminate events run. Let's fix that issue instead!
Comment #7
wim leersComment #8
e0ipsoSome related FUD: I've had some very tricky and obscure issues in RESTful with this header when there's utf content and responses are compressed.
Comment #9
vacilando commentedRecently needed the same so as to be able to use KeyCDN.
Successfully solved using module https://www.drupal.org/project/content_length_header (full blog post at https://www.vacilando.org/article/using-cdn-accelerate-drupal-api-sourci...).