"Taxonomy term ID from URL" default views argument should have url.path as cache context, not url

Created on 19 November 2024, 3 months ago

Problem/Motivation

I have a low Varnish hit ratio on a website, and I'm wondering if it could be due to the fact that many annoying bots may request some pages with dummy URL GET query parameters that would prevent cache hits (in Drupal then in Varnish).
Thanks to the Redis module, I found out some "Render cache entries with most variations" related to views using the "Taxonomy term ID from URL" default argument.
This sets a cache context 'url' on the rendered view. I might be wrong, but I guess this default arg only depends on the path part of the URL (/myterms/42), not on the query args (?foo=bar).
If so, the cache context should be 'url.path' and not 'url', so that requests to /myterms/42, /myterms/42?foo=bar, /myterms/42?baz,...) all hit the same cache entry.

I saw that this code was set in https://git.drupalcode.org/project/drupal/-/commit/d2f9c183bf26e1c, but I could not find any explanation about this in #2318377: Determine whether a view is cacheable and its required contexts, store this i/t config entity , so I guess it's just a mistake.

Proposed resolution

This is a quite simple change, just replace 'url' with 'url.path' in web/core/modules/taxonomy/src/Plugin/views/argument_default/Tid.php

Remaining tasks

Ensure 'url.path' is enough, ensure 'url' is not needed.
Maybe search for other mistakes of that kind in other default arg plugins.

User interface changes

None.

Introduced terminology

None.

API changes

None.

Data model changes

None.

🐛 Bug report
Status

Active

Version

11.1 🔥

Component

cache system

Created by

🇫🇷France GaëlG Lille, France

Live updates comments and jobs are added and updated live.
  • Performance

    It affects performance. It is often combined with the Needs profiling tag.

Sign in to follow issues

Comments & Activities

  • Issue created by @GaëlG
  • 🇮🇳India sourav_paul Kolkata

    Can you share proper reproducing steps?

  • 🇳🇿New Zealand quietone

    @sourav_paul, For Drupal core, it is preferred that contributors add a comment that they are working on an issue instead of assigning it to themselves. See Assigning ownership of a Drupal core issue .

    Changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to our policies.

  • 🇮🇳India sourav_paul Kolkata

    Thanks @quietone, from nxt time will keep it in mind...

  • 🇫🇷France GaëlG Lille, France

    @sourav_paul: I could find no easy way to reproduce precisely this on a clean install (such as https://simplytest.me) because there must be other render elements in pages also depending on url cache context, not just this one. Thus I get a x-drupal-cache:miss HTTP header as soon as I add a new query arg (such as ?foo), even without using "Taxonomy term ID from URL" default views argument.

  • 🇮🇳India sourav_paul Kolkata

    Okay I understand, But I don't feel its a good idea as if we cache url path with args, it could create problem with contextual filtration of view.

    Welcome for Opinions...

  • 🇫🇷France GaëlG Lille, France
  • 🇧🇪Belgium kristiaanvandeneynde Antwerp, Belgium

    I would argue it needs to vary by "route" instead. After all, core/modules/taxonomy/src/Plugin/views/argument_default/Tid.php uses the route match to get the data it needs to function.

    P.S.: Just because a page is located at /myterms/42, does not mean the 42 will get translated into a taxonomy term. It needs a route parameter for that in the route behind the page. See Drupal\Core\ParamConverter\EntityConverter. (A custom ParamConverterInterface could also work but that's highly unusual.)

Production build 0.71.5 2024