Problem/Motivation
- Urls can be embedded everywhere, but are itself not cache context aware.
- Urls are tricky, because they are rendered to a string and not a render array.
- Urls can vary on things that are cache contexts
Links are the same, but are today created with Url objects.
Examples:
- Urls can e.g. depend on the SESSION (for CSRF right now)
- Urls can - as far as I can see - vary on the language.
- Urls can also vary on access, which is important when doing links, but not when printing Url objects itself (as far as I understand).
This is a soft-blocker for the cacheable CSRF issue as its the same problem space and solving this one will make that one trivial ...
Proposed resolution
- Add getCacheContexts(), etc. to URLs (CacheableDependencyInterface or extend BubbleableMetadata) and give processors of Urls a chance to set cache contexts / post render cache / etc.
We hence need to treat rendering of urls (toString) as if someone was rendering something in a template_preprocess_foo(), as such outside of the rendering stack, but we still need to put it on the stack.
This can be as simple as replacing toString() to call drupal_render with a full render array.
The drawback is, if you want to do:
- Render link => save to database - it will turn up in the render stack where you do that and hence you end up with a cache context that is not rendered.
But the alternative of wrong cache contexts is worse and maybe the solution to that is to be able to enter a "context" / "scope" to do something outside of the render stack for that ...
Because the same is true if you do:
$x = drupal_render($foo);
and send to a remote service / save to database whatever, it will too end up on the render stack.
But then you likely would use drupal_render_root(), which here is difficult to use for the links as its 'just' links ...
Tricky ...
Remaining tasks
- Discuss
- Patch
Comments
Comment #1
Wim LeersComment #2
Fabianx CreditAttribution: Fabianx commentedPostponed on #2450993: Rendered Cache Metadata created during the main controller request gets lost as I see no way to solve this until that issue is fixed.
Comment #3
Wim LeersComment #4
Wim LeersComment #5
Wim LeersOops!
Comment #6
Wim LeersThis is a duplicate of an intersection of #2335661: Outbound path & route processors must specify cacheability metadata + #2450993: Rendered Cache Metadata created during the main controller request gets lost + #2351015: URL generation does not bubble cache contexts.
#2335661: Outbound path & route processors must specify cacheability metadata is already fixed.
#2450993: Rendered Cache Metadata created during the main controller request gets lost is close to RTBC.
#2351015: URL generation does not bubble cache contexts is being worked on.