Problem/Motivation

While you are developing a custom module, and integrating with anything client cached, you won't find your new elements (e.g. Contextual links). As a developer, your first thought would be "clear Drupal cache", but that won't solve it. Even knowing the excellent client-side caching solution in D8, I lost some time debugging while trying to integrate a custom module with Contextual Links (i.e. the new contextual links weren't showing up).

In the case of contextual links, window.sessionStorage is used, which means that opening a new tab is enough to get rid of the client-side cache. But this won't be obvious for a D8 newcomer.

Client-side caching works as designed, but there is no way of disabling it while in development. There should be, to have a better DX.

Proposed resolution

Introducing a drupalSettings.disableClientSideCaching boolean flag, that can be enabled from settings.local.php (setting) /development.services.yml (container parameter). Any Drupal module doing client-side caching could then opt in to check whether that value exists and if it's set to TRUE, it would disable all client-side caching.

Remaining tasks

Discuss possible solutions.
Patch.
Review.
Commit.

User interface changes

None.

API changes

None.

Files: 
CommentFileSizeAuthor
#3 disable_client_side_caching_flag-2395453-3.patch6.06 KBWim Leers
PASSED: [[SimpleTest]]: [PHP 5.4 MySQL] 82,542 pass(es). View

Comments

dawehner’s picture

Wim Leers’s picture

Title: Client caching is a hidden feature that can boost your site, but reduce DX » Client-side caching is a hidden feature that can boost your site, but reduce DX
Issue summary: View changes
Wim Leers’s picture

Title: Client-side caching is a hidden feature that can boost your site, but reduce DX » Allow client-side caching to be disabled while developing
Assigned: Unassigned » nod_
Status: Active » Needs review
FileSize
6.06 KB
PASSED: [[SimpleTest]]: [PHP 5.4 MySQL] 82,542 pass(es). View

And patch.

We don't want to uniformly forbid the use of sessionStorage/localStorage; it's often also used to persist state. This is not the same as client-side caching of data. Each piece of JS can choose how to handle the drupalSettings.clientSideCaching setting.

Assigning to nod_ for review.

nod_’s picture

Status: Needs review » Needs work
+++ b/core/misc/drupal.js
@@ -428,4 +428,20 @@ if (window.jQuery) {
+    Object.keys(storage).forEach(function (key) {
+      if (key.substring(0, prefix.length) === prefix) {
+        storage.removeItem(key);
+      }
+    });

That's not a reliable way to get the keys,

for (var i = 0; i < storage.length; i += 1) {
  var key = storage.key(i);
  // the rest
}

is the "spec" way of doing it.

More generally I don't like the fact that we have an arbitrary variable used all over the place and especially expect contrib to know about it and use it properly. If we're serious about this we should abstract local/sessionStorage in a — very simple — Drupal.storage object (that way we could simplify some JSON.stringify/JSON.parse too). That way contrib don't have to deal with all this.

All in all I agree there is a problem, I agree we should have a solution, I just don't like adding killswitches and the java-ish-named method deleteFromClientSideCacheByPrefix. There is such a thing as too explicit, if we're still going this way a name such as clearStoragePrefix would probably be better.

Wim Leers’s picture

If we're serious about this we should abstract local/sessionStorage in a — very simple — Drupal.storage object (that way we could simplify some JSON.stringify/JSON.parse too). That way contrib don't have to deal with all this.

I considered this too, but that's not an option either. Look at e.g. Quick Edit's JS: it needs to write to localStorage/sessionStorage, but it can empty it at the beginning of every request. Contrast that with e.g. Contextual's JS, which can just return early.
Different designs, different needs, there's no one-size-fits-it-all solution.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

tim.plunkett’s picture

Version: 8.2.x-dev » 8.3.x-dev
Assigned: nod_ » Unassigned

This is extremely important for DX, IMO

Wim Leers’s picture

Priority: Normal » Major

Bumping to major then. Can you clarify why you find this so important? Did you lose much time over this?

tim.plunkett’s picture

Infinite amount of drush cr-ing will not help you on this.
What happens to help is rage-force-quitting your browser and then trying again later!

This needs a $settings flag in settings.local.php just like the ones to disable CSS/JS aggregation and render cache.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.