Problem/Motivation
We want to use BinaryFileResponse implement CacheableResponseInterface.
But site crash and throw error 'Symfony\Component\HttpFoundation\File\File' is not allowed in serialize().
check PHP stack, it's from DynamicPageCacheSubscriber::onResponse()
Steps to reproduce
Proposed resolution
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | 3227041-4.patch | 1.3 KB | Oscaner |
| #2 | 3227041-2.patch | 1.25 KB | Oscaner |
Comments
Comment #2
Oscaner commentedComment #3
jeroentComment #4
Oscaner commentedReroll on D9.2.x
Comment #8
catchWhat are you trying to cache BinaryFileResponse in that's not the dynamic page cache? BinaryFileResponse is a Symfony class and doesn't implement the interface, so would never enter the cache in the first place due to the CacheableResponse check in ::onRequest(). I'm not sure why we'd special case an interface that already doesn't implement CacheableResponseInterface by default.
If this is for custom code, you can add a dynamic page cache response policy to exclude BinaryFileResponse - that would allow you to implement the interface but still exclude it from dynamic page cache. I could see someone trying to do similar but also trying to make things serializable too, in which case the hard-coding of the interface here would prevent that.
Moving to needs more info until the use case is clearer.
Comment #9
donquixote commentedI just ran into this.
The CacheableResponseInterface has two effects:
1. The page can be cached in Drupal's dynamic page cache.
2. The page gets cache tag headers and can be cached in Varnish.
The Media Alias Display module replaces the media controller and sends a BinaryFileResponse when visiting a media canonical url.
The module currently does not add any cache info to the response, making it uncacheable.
Our own varnish config auto-caches everything for anonymous for 8h.
This means that now these files are not purged on update, because Varnish does not have the cache tags.
My solution attempt is to create this class:
Now I only need to figure out which cache tags and cache contexts to add...
Comment #10
donquixote commentedLink to #3404512: Make the response cacheable for Media Alias Display.
Comment #12
smustgrave commentedWith the fix in for media alias display is this still needed?
Comment #13
smustgrave commentedSince the contrib module was fixed not sure this one is needed.