Problem/Motivation
On high-traffic sites with a large volume of anonymous users, the /better-social-share/get-popup endpoint receives tens of thousands of unnecessary requests that all hit the origin. Two separate issues compound each other:
- The popup is fetched eagerly on every page load. The module's JavaScript sends an AJAX request to get-popup on every page where the share buttons are rendered, regardless of whether the user ever opens the popup. In practice the vast majority of these requests are for a popup that nobody sees.
- The response carries no cache metadata. The controller returns a plain JsonResponse, so Drupal responds with Cache-Control: no-cache, private and X-Drupal-Cache: UNCACHEABLE. A CDN (CloudFront in our case) can't cache any of it, so every single request falls through to the origin and is answered by Drupal one by one.
What makes this stand out is that the returned content is effectively static: it's just the list of share platforms and their icons. It does not depend on the page, the user, or the session. It only changes when the module configuration changes. This is exactly the kind of response that should be served from the CDN instead of round-tripping to Drupal.
To be clear about the impact: this is not a CPU or memory problem. Each request is lightweight and resolves quickly (nothing alarming in our APM). The problem is pure volume, a large number of requests that add no value and that don't need to reach Drupal at all when they could be served from cache.
Steps to reproduce
Enable the module and place the share buttons on a page that gets anonymous traffic.
Load the page (without clicking the share button) and observe an AJAX request to /better-social-share/get-popup.
Inspect the response headers: Cache-Control: no-cache, private, X-Drupal-Cache: UNCACHEABLE.
On a CDN-fronted site, confirm these requests are not cacheable and all reach the origin.
Proposed resolution
Address both fronts:
- Lazy-load the popup. Stop firing the AJAX request on page load. Fetch the popup only when the user actually clicks the share button.
- Make the response cacheable. Return a CacheableJsonResponse with proper cache metadata (cache tag config:better_social_share.settings) and public + s-maxage headers (24h). This lets a CDN cache the response and have it invalidated automatically when the module configuration changes.
Combined, this should reduce origin requests to this endpoint by roughly 90–99%, between the requests that are no longer fired and the ones now served from the CDN.
Issue fork better_social_share-3611578
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
trebormcOpened MR !11.
Setting to Needs review.
Comment #4
csakiistvanComment #5
csakiistvan✅ Tested and works — applied MR !11, rebuilt caches, reloaded an article as anonymous with the Network tab open and checked the endpoint with
curl -sI:/better-social-share/get-popupis now requested only on the first click of the share trigger, not on page load or reopening, and returnsCache-Control: max-age=60, public, s-maxage=86400withX-Drupal-Cache: HITon the second request. The popup is lazy-loaded and cacheable.Notes
system.performance cache.page.max_age.Vary: Accept-Encodingduplicates the one Drupal already sets; the line could be dropped.Comment #6
sujan shrestha commentedFixed in 1.1.8 (commit 2388a9e). The popup is requested only when a visitor first opens it. The response is a cacheable JSON response, tagged with the module settings, so the page cache and reverse proxies can serve it, and saving the settings invalidates it. Covered by kernel and functional tests, which check that the page cache serves it.