Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
We have run into an issue where the Sharethis block does not load consistently for anonymous users when Drupal block caching is enabled. Below are the steps to reproduce. Thanks.
- Launch a sandbox from http://simplytest.me/project/sharethis/7.x-2.12
- Visit /admin/config/services/sharethis
- Under 'Context' vertical tab, change 'Location' to 'Block'
- Visit /admin/config/development/performance
- Enable 'Cache blocks'
- Visit /admin/structure/block/manage/sharethis/sharethis_block/configure
- Set region to 'Content' for Bartik theme
- Under 'Content types' vertical tab, check 'Article'
- Visit /node/add/article, create dummy node (path should be node/1)
- Open an incognito window or new browser where you are not logged in. Visit /node/1. Observe that:
- Sharethis icons load above node content
- Viewing the page source will show
<script type="text/javascript" src="https://ws.sharethis.com/button/buttons.js"></script>
inside the<head>
tag.
- Refresh the page. Observe that:
- Sharethis icons do not load
- Viewing source shows the javascript is not loaded in the
<head>
tag.
- To verify this is related to block caching: Visit /admin/config/development/performance and disable 'Cache blocks' and save settings. Next, click 'Clear all caches', then visit /node/1 as an anonymous user and refresh a few times. The Sharethis block loads correctly each time.
Comment | File | Size | Author |
---|---|---|---|
#10 | block_caching_causes-2682859-5.patch | 2.3 KB | purushotam.rai |
| |||
#5 | block_caching_causes-2682859-5.patch | 2.34 KB | purushotam.rai |
|
Comments
Comment #2
vinmassaro CreditAttribution: vinmassaro commentedComment #3
mizage@gmail.com CreditAttribution: mizage@gmail.com commentedI can confirm this behavior. Any ideas why this has suddenly started to occur? I'm running 7.43 with ShareThis 2.10.
Comment #4
purushotam.rai CreditAttribution: purushotam.rai at QED42 commentedHi,
While investigating on this issue, I found that the reason behind this issue is
"If you call drupal_add_js() inside a block and have caching enabled for that block, that JS code will not be included after the block gets cached!"
So, We have 3 options to solve this:
1. As of now block has been set to DRUPAL_CACHE_PER_PAGE which can be changed to DRUPAL_NO_CACHE
2. Or we can use #attached property of $block['content'].
3. We can set DRUPAL_NO_CACHE and in hook_block_view() cache the output of the html after adding the JS.
Probably the third option looks best suitable in this case.
Thanks
Comment #5
purushotam.rai CreditAttribution: purushotam.rai at QED42 commentedHere goes the patch regarding my last comment with approach no. 3.
Comment #6
purushotam.rai CreditAttribution: purushotam.rai at QED42 commentedComment #8
purushotam.rai CreditAttribution: purushotam.rai at QED42 commentedComment #9
xangyTested this on sandbox of sharethis module with patch #5 on simplytest.me.
Followed the steps provided in the issues. The sharethis block was loaded correctly in incognito window with cache blocks enabled. Please standardize the code though.
Comment #10
purushotam.rai CreditAttribution: purushotam.rai at QED42 commentedComment #12
xangyPatch works fine. Ignoring DrupalCI error, seems like error in DrupalCI and not the patch itself.
Comment #14
purushotam.rai CreditAttribution: purushotam.rai at QED42 commentedCommited to 7.x-2.x dev branch. Would be included in next stable release too.
Thanks all for your valuable inputs. Marking this issue as fixed. Feel free to re-open in case issue persists.