Disable browser caching for Lightning Components

Salesforce Lightning Heroes

Over the last 18 months Desynit have worked on a number of projects involving complex custom Lightning Components. The Lightning Component Framework has come a long way in that time, and many of the initial roadblocks we encountered (like missing documentation, and problems with components in changesets) have been fixed in recent releases. However, the persistent caching of components in development has remained a frustration.

Cache on delivery

To put it kindly, LEX has been known to be a little sluggish. To maximise performance, Salesforce implements an aggressive caching policy to minimise time consuming round-trips to the server. This may be sensible in a Production environment, where components rarely change, but forcing multiple page refreshes (often taking 5 seconds or more) to view changes is a major overhead.

When viewing markup changes we can at least keep hitting F5 until new elements appear. It’s much more problematic if we are updating the JavaScript code in our controller or helper files, because there may not be a visual clue to let us know if we’re viewing the latest version of our component.

A primitive option…

To minimise the hassle this can cause, we got into the habit of inserting a spurious div or p element into our markup, incrementing the content each time a change was made anywhere in the component:

[prism field=code_example_1 language=java]

Not a very elegant solution, but it gets the job done (as long as you remember to update your content).

A better option…

I came across the official solution for this problem buried away in the 51st minute of a Salesforce Webinar called ‘Best Practices for Lightning Component Development’.

  • From Setup, locate the link to ‘Session Settings’
  • Locate the ‘Caching’ section
  • Uncheck the option to ‘Enable secure and persistent browser caching to improve performance’

Caching Options

And just like magic, my Lightning Component is as fresh as a daisy on every load – result! Naturally, you should only consider turning off browser caching in a Developer Org or Sandbox (because nobody updates Lightning Components in Production, right???)

Further investigation shows that this feature has in fact been available since the Spring ‘16 release, but I had not come across it before, and this problem has been mentioned by a lot of developers at meetups, as well as on blogs and podcasts that I follow. Looks like we all need to scrutinise the release notes a little more from now on…

Lightning Cartoon Figures
By Dorian Sutton
22 May 2017
Salesforce DevelopmentThe Dev ZoneThe Good Systems Blog

Share this post

Catt to action

Amet aliquam id diam maecenas ultricies mi eget mauris

Lorem ipsum dolor sit amet, consectetur elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.