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

User interface changes

API changes

Comments

Wim Leers’s picture

Fabianx’s picture

Title: Make URLs cache context aware » [PP-1] Make URLs cache context aware
Status: Active » Postponed

Postponed 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.

Wim Leers’s picture

Wim Leers’s picture

Wim Leers’s picture