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.

  1. Launch a sandbox from http://simplytest.me/project/sharethis/7.x-2.12
  2. Visit /admin/config/services/sharethis
    • Under 'Context' vertical tab, change 'Location' to 'Block'
  3. Visit /admin/config/development/performance
    • Enable 'Cache blocks'
  4. Visit /admin/structure/block/manage/sharethis/sharethis_block/configure
    • Set region to 'Content' for Bartik theme
    • Under 'Content types' vertical tab, check 'Article'
  5. Visit /node/add/article, create dummy node (path should be node/1)
  6. 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.
  7. Refresh the page. Observe that:
    • Sharethis icons do not load
    • Viewing source shows the javascript is not loaded in the <head> tag.
  8. 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.
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

vinmassaro created an issue. See original summary.

vinmassaro’s picture

Issue summary: View changes
mizage@gmail.com’s picture

I can confirm this behavior. Any ideas why this has suddenly started to occur? I'm running 7.43 with ShareThis 2.10.

purushotam.rai’s picture

Hi,

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

purushotam.rai’s picture

Here goes the patch regarding my last comment with approach no. 3.

purushotam.rai’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 5: block_caching_causes-2682859-5.patch, failed testing.

purushotam.rai’s picture

Status: Needs work » Needs review
xangy’s picture

Status: Needs review » Needs work

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

purushotam.rai’s picture

Status: Needs work » Needs review
FileSize
2.3 KB

Status: Needs review » Needs work

The last submitted patch, 10: block_caching_causes-2682859-5.patch, failed testing.

xangy’s picture

Status: Needs work » Reviewed & tested by the community

Patch works fine. Ignoring DrupalCI error, seems like error in DrupalCI and not the patch itself.

PHP Notice:  Undefined variable: classes in /opt/drupalci_testbot/src/DrupalCI/Plugin/BuildSteps/publish/JunitXMLFormat.php on line 144
PHP Warning:  Invalid argument supplied for foreach() in /opt/drupalci_testbot/src/DrupalCI/Plugin/BuildSteps/publish/JunitXMLFormat.php on line 169
Reformatted test results written to /var/lib/drupalci/web/jenkins-default-111971/artifacts/xml/testresults.xml
Completed publish:junit_xmlformat
Completed publish
Checking console output
Recording test results
ERROR: Step ?Publish JUnit test result report? failed: None of the test reports contained any result
Finished: FAILURE

  • purushotam.rai committed 0d9b201 on 7.x-2.x
    Issue #2682859 by purushotam.rai: Block caching causes inconsistent...
purushotam.rai’s picture

Status: Reviewed & tested by the community » Fixed

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

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.