Lädt...


🔧 Local Storage in 5 mins: A Beginner’s Guide to Cookies, localStorage, and sessionStorage


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Picture of Cookies, localStorage and sessionStorage code
As web applications continue to evolve into robust experiences resembling native apps, the need for local data storage on the client-side becomes increasingly important. Technologies like cookies, localStorage, and sessionStorage provide frontend developers with various options for temporarily caching data within the browser.

In this article, we'll explore the key differences between these three storage mechanisms and how each can be leveraged appropriately based on an application's specific data needs. By understanding the capabilities and limitations of each, you can architect your client-side code for optimal performance while avoiding common pitfalls.

Cookies: The Sticky Notes of the Web

Picture of stacked cookies
Photo by Christina Branco on Unsplash

Cookies have been around since the early days of the web and remain one of the most basic forms of client-side storage. Primarily used for authentication, cookies allow servers to uniquely identify returning users by storing a small amount of data on their machine.

However, cookies come with limitations. Each cookie can only hold up to 4kb of data formatted as a single string, making them ill-suited for structured or large datasets. Additionally, every request to the server will attach all cookie headers, increasing bandwidth consumption.

For these reasons, cookies are best used sparingly - to store things like user IDs, preferences, or session tokens. Libraries exist to simplify encoding/decoding of cookie values to dictionary formats.

// Setting a cookie without a library
document.cookie = "username=JohnDoe; expires=Thu, 18 Dec 2023 12:00:00 UTC; path=/";

// Using a library to set a cookie
Cookies.set('username', 'JohnDoe', { expires: 7, path: '/' });

Libraries like js-cookie provide convenient methods for getting and setting cookies, dealing with the nuances of string parsing under the hood.

localStorage: The Whiteboard

localStorage is like having a whiteboard in your home. You can write down reminders that stay there until you erase them. localStorage provides a more robust alternative to cookies for locally caching larger amounts of structured data. Values are stored as key-value pairs that can be accessed on all pages of a domain, persisting even when the browser is closed.

This makes localStorage ideal for caching dynamic responses, application states, or content likely to be reused across sessions. Developers have up to 5-10mb of storage per origin, though browser support varies.

// Setting an item in localStorage
localStorage.setItem('favoriteColor', 'blue');
// Getting that item back
let favoriteColor = localStorage.getItem('favoriteColor');

SessionStorage: The Notepad

Similar to localStorage, sessionStorage (up to 5mb) also uses a key-value data structure but will only persist for the lifetime of the browser tab or window it was created in. Data is removed once the tab closes.

Well-suited for multi-step workflows, sessionStorage enables locally caching intermediate states or contextual data meant to enhance the user experience on a single page load.

// Setting an item in sessionStorage
sessionStorage.setItem('toDoList', 'Buy milk');
// Getting that item back
let toDoList = sessionStorage.getItem('toDoList');

Managing Your Storage

To maintain organized and efficient data stores, periodic cleaning can be helpful. Through the Storage API, you can remove individual items as well as empty the entirety of cached data:

  • Remove a specific item: localStorage.removeItem('favoriteColor');
  • Clear everything: localStorage.clear();

The same methods apply for sessionStorage.

Using Storage in React with Hooks

Picture of React.JS on a VSCode IDE
Photo by Lautaro Andreani on Unsplash

React's hooks allow you to interact with local storage in a more intuitive way, abstracting complex operations into simple function calls. Here's how you can create a custom hook to manage localStorage in your React apps:

import { useState, useEffect } from 'react';

function useLocalStorage(key, initialValue) {
    const [storedValue, setStoredValue] = useState(() => {
    try {
      const item = window.localStorage.getItem(key);
      return item ? JSON.parse(item) : initialValue;
    } catch (error) {
      console.error(error);
      return initialValue;
    }
}

// Example usage of the hook in a component
function App() {
  const [name, setName] = useLocalStorage('name', 'Alice');

  return (
    <div>
      <input
        type="text"
        value={name}
        onChange={(e) => setName(e.target.value)}
      />
    </div>
  );
}

The Future: Secure Alternatives

While cookies, localStorage and sessionStorage empower versatile application functionality through client-side data caching, several security limitations remain that developers must carefully consider.

As currently implemented, any JavaScript loaded onto a page possesses the ability to access stored values, posing risks if sensitive information is involved. No built-in protection from unauthorized retrieval or modification exists.

Recognizing such concerns, upcoming standards propose enhancements strengthening data confidentiality.

In the meantime, best practices involve judiciously avoiding PII or credential placement within browsers' storage APIs directly. Leverage server-side sessions instead when appropriate. Similarly, exercise caution with third-party code integration to minimize exposure.

Thank you for reading.

If you found this helpful, be sure to check out the rest of my page for additional deep dives covering front-end performance, accessibility techniques and more!

...

🔧 Local Storage in 5 mins: A Beginner’s Guide to Cookies, localStorage, and sessionStorage


📈 116.18 Punkte
🔧 Programmierung

🔧 Web Storage (Cookies, localStorage, and sessionStorage)


📈 71.86 Punkte
🔧 Programmierung

🔧 Understanding Web Storage: LocalStorage, SessionStorage, and Cookies


📈 71.86 Punkte
🔧 Programmierung

🔧 Data Persistence (Cookies, Sessions, Tokens, LocalStorage and SessionStorage)


📈 64.41 Punkte
🔧 Programmierung

🔧 Autenticação Cookies HTTP, HTTP Only, JWT, LocalStorage e SessionStorage.


📈 62.75 Punkte
🔧 Programmierung

🔧 LocalStorage, SessionStorage e Cookies: Como funcionam e quando usar cada um


📈 62.75 Punkte
🔧 Programmierung

🔧 Securing Web Storage: LocalStorage and SessionStorage Best Practices


📈 60.01 Punkte
🔧 Programmierung

🔧 A Step-by-Step Guide to LocalStorage and SessionStorage: Storing Data on the Client Side


📈 58.2 Punkte
🔧 Programmierung

🔧 Understanding Cookies, Local Storage, and Session Storage: A Beginner's Guide


📈 50.37 Punkte
🔧 Programmierung

🔧 How to Add Local Storage to Your Blazor Apps with Blazored.LocalStorage


📈 36.54 Punkte
🔧 Programmierung

🔧 Difference Between Local Storage And Session Storage and cookies in the browser


📈 36.19 Punkte
🔧 Programmierung

🔧 🎒 localStorage or 🍪 cookies : What's the difference?


📈 34.82 Punkte
🔧 Programmierung

🔧 -LocalStorage, Session, Cookies


📈 34.82 Punkte
🔧 Programmierung

🔧 LocalStorage vs Cookies: All You Need To Know About Storing JWT Tokens Securely in The Front-End


📈 34.82 Punkte
🔧 Programmierung

🔧 Understanding Session Storage, Local Storage, and Cookies in Web Development


📈 34.52 Punkte
🔧 Programmierung

🔧 Cookies vs Local Storage vs Session Storage


📈 32.85 Punkte
🔧 Programmierung

🔧 Cookies vs Local Storage vs Session Storage


📈 32.85 Punkte
🔧 Programmierung

🔧 Cookies vs Local Storage vs Session Storage


📈 32.85 Punkte
🔧 Programmierung

🔧 Azure Storage Account: A Guide To Creating an Azure Storage Account and a Blob Storage Container.


📈 29.62 Punkte
🔧 Programmierung

🔧 React Cookies: A Complete Guide to Managing Cookies in React Applications


📈 29.33 Punkte
🔧 Programmierung

🕵️ JetBrains Ktor up to 1.4.x SessionStorage Key inadequate encryption


📈 27.93 Punkte
🕵️ Sicherheitslücken

🔧 Faking sessionStorage to keep sites from crashing


📈 27.93 Punkte
🔧 Programmierung

🕵️ Medium CVE-2015-9544: Cross domain local storage project Cross domain local storage


📈 27.13 Punkte
🕵️ Sicherheitslücken

🕵️ Medium CVE-2015-9545: Cross domain local storage project Cross domain local storage


📈 27.13 Punkte
🕵️ Sicherheitslücken

🕵️ Medium CVE-2020-11610: Cross domain local storage project Cross domain local storage


📈 27.13 Punkte
🕵️ Sicherheitslücken

🔧 NextJS - Access window and localStorage


📈 24.64 Punkte
🔧 Programmierung

🔧 9 differences between IndexedDB and LocalStorage


📈 24.64 Punkte
🔧 Programmierung

🔧 Local storage and Session storage and useful tips for debugging in Chrome


📈 24.34 Punkte
🔧 Programmierung

🎥 5 MINS AGO: VP JD Vance is EXPOSING Everything and it Should Concern All of Us.. | Trump LEAKS


📈 24.02 Punkte
🎥 Video | Youtube

🐧 Chrome 69 will keep Google Cookies when you tell it to delete all cookies


📈 23.69 Punkte
🐧 Linux Tipps

🔧 Battle of the Cookies: Regular Cookies vs. HTTP-Only


📈 23.69 Punkte
🔧 Programmierung

📰 Ein Internet ohne Cookies? Google verabschiedet sich von Third-Party-Cookies


📈 23.69 Punkte
📰 IT Security Nachrichten

matomo