How Session Storage Can Improve Site Performance

Most of the time, you read about web applications that use sessionStorage inside web applications. These applications will use sessionStorage to do things like store the state of an app, so you can resume working on the app, even if you reload the page, or accidentally leave close the app. Most of the time, any further considerations for how to use sessionStorage is not considered, and that’s a shame. It’s a very useful utility that can not only improve your site speed, but drastically reduce server load. In fact, the site you’re using right now makes use of sessionStorage to accomplish this exact thing.

Most-any headless, or nearly-headless WordPress website can lean on session storage.

What is JavaScript sessionStorage?

sessionStorage is a data store that is managed by your web browser, using JavaScript. Any data placed in sessionStorage remains there for the extent of the browser’s session. As soon as the visitor ends their session, this storage is deleted. Most browsers end their session when the browser is completely closed.

This means that all of the data that is stored in sessionStorage should be thought of as temporary, since any of the data in it can be deleted at any time. In other words, this works very much like the WordPress object cache, where you can store data to be accessed later, but you should always have a way to create that data if it doesn’t exist in the cache at that moment.

Using sessionStorage

You don’t need any libraries or special things to use sessionStorage, it’s built directly into your Window object. There are 4 key methods to work with sessionStorage.

  1. sessionStorage.setItem() – used to add an item to sessionStorage to be accessed later
  2. sessionStorage.getItem() – used to get an item that was previously stored in sessionStorage
  3. sessionStorage.removeItem() – used to remove an item from sessionStorage
  4. sessionStorage.clear() – used to clear all records in sessionStorage. (use with caution)

Most of the time, you use sessionStorage.getItem() to see if the cached item exists, and if it doesn’t, calculate the value, and add it to the cache for next-time. Kind-of like this:

It is important that the sessionStorage key is unique, otherwise you would end up getting the wrong cached item. That could certainly cause some problems, so be sure your key is always unique. Another thing about sessionStorage is that it takes many data types, and will convert them to a string when storing in sessionStorage. In the example above, the result returns an array. When stored in sessionStorage, this data is converted into a string, which would cause unexpected results if the data were to be accessed later. To work around this, the sessionStorage result is converted to a JSON string beforehand.

Let’s take this trivial example, and make it less-trivial by adding an argument to the function that gets used to change the result. This would change the data that is stored in the cache based on the argument, and could cause a mismatch between what would be generated and what is in the cache. To get around this, it’s best to use the arguments to generate the cache key, like this:

Reduce Server Load

The examples provided above are fairly trivial, but what if you stored RESTful requests that simply fetch public data from an API? By doing this, you could reduce the number of REST requests your website needs to make to access data, and simply load it directly from sessionStorage. This would allow many of your web pages to load instantly, since the data is already stored in the cache.

The example below wraps everything in a promise. This allows us to use async/await inside the promise and allows us to determine iffetchExample should be called asynchronously, or synchronously.

Now, the first time this function is called, it will make a REST request to fetch the posts, however any time it runs thereafter, it will automatically fetch the data from sessionStorage, instead. Pretty freaking cool, right? If you’re using the WordPress apiFetch library, you could even take this a step further and make the above function work as middleware. You could set it up so that your instance of apiFetch automatically caches the data through apiFetch. This is a bit of an advanced technique, but when set-up, anyone who extends your system would benefit from all of your caching optimizations without necessarily even knowing they’re using it!

Nicholas and SessionStorage

The Nicholas router is both a routing, and caching system designed to work with caching content on different WordPress pages. Behind the scenes, it uses sessionStorage, and if your content is specific to a webpage, it can make caching much easier.

Nicholas is both a router and a caching solution, designed to cache data as it pertains to different webpages, and is used extensively in the nearly headless WordPress website course that’s coming out soon.

Conclusion

Unlike server-side caching, sessionStorage caches directly in the browser. This can bypass the need to even touch the server, allowing many things to load instantly. This is the key component behind what makes the nearly headless website demo so fast – it makes extensive use of sessionStorage to store all of the website data needed to render any given page. You don’t have to push it this far, however. Even something as simple as the fetch example above have a big impact on your site’s performance.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *