ShareThis block's internal caching does not vary by language

Created on 14 March 2017, about 8 years ago
Updated 23 January 2025, 3 months ago

Problem/Motivation

The contents of a ShareThis block are generated by the sharethis_block_contents() function in sharethis.module. This function implements its own cache for the block contents, which is varied per-path (i.e.: URL), but not per-language. However, certain structures in Drupal, (such as Views, Panels, internal paths, and entities (nodes, taxonomy terms, Commerce products, etc.) made multilingual using the Entity Translation module), have the same path (URL) in all languages.

This means that, when accessing one of these structures (whose path/URL is the same in all languages) for the first time after a cache clear, the cached block contains data (e.g.: the HTML m_title attribute, containing the title of the page in the current language) for whichever language happened to requested at the time.

For example, suppose we have a Drupal site at http://example.com/ with two languages enabled: English and French, and the ShareThis block enabled on all pages of the site. Suppose we add a view at http://example.com/news, and we set the title to display as "News & Events" in English, and "Nouvelles et evenements" in French. Suppose we clear Drupal caches, and the first person (whom we will call "Person A") to load the view at http://example.com/news happens to be accessing the site in French. Because we just flushed caches, this results in a cache-miss, and Person A sees a ShareThis block, whose HTML m_title attribute is set to "Nouvelles et evenements" (which is the expected behavior); and the fully-rendered (French) HTML is saved to the cache with the key sharethis:sharethis_block:news. Suppose a different user ("Person B") then tries to access the same view at http://example.com/news in English. This now results in a cache-hit, and the fully-rendered (French) HTML generated from the earlier request is loaded from sharethis:sharethis_block:news, and Person B sees a ShareThis block, whose HTML m_title attribute is set to "Nouvelles et evenements" (which is undesired behavior).

Proposed resolution

Modify the cache key from sharethis:sharethis_block:$path to sharethis:sharethis_block:$lang:$path.

Using our example scenario above, loading the view at http://example.com/news in French would result in a cache-miss, and the fully-rendered (French) HTML being saved to a key named sharethis:sharethis_block:fr:news. A subsequent request for the same page in English would also result in a in a cache-miss, and the fully-rendered (English) HTML being saved to a key named sharethis:sharethis_block:en:news. Subsequent requests to the page in either language will result in cache-hits for the appropriate language, until caches are cleared again.

Remaining tasks

  1. Maintainer and/or community member reviews patch, provides feedback / requests changes to patch if necessary, marks as RTBC if not.
  2. Maintainer reviews patch, provides feedback / requests changes to patch if necessary, commits if not.
  3. Maintainer pushes commit to branch, considers new release.

User interface changes

None.

API changes

None.

Data model changes

Cache key changes from sharethis:sharethis_block:$path<code> to <code>sharethis:sharethis_block:$lang:$path.

🐛 Bug report
Status

Closed: outdated

Version

2.0

Component

Code

Created by

🇨🇦Canada mparker17 UTC-4

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024