Persisting Kanban Tasks with localStorage in Svelte

Introduction to Data Persistence

In our Kanban board application so far, we've implemented a robust task management system with the ability to add, move, and delete tasks. However, there's a significant limitation: every time you refresh the page, all your tasks reset to the initial state. This happens because our application's state is stored only in memory, which gets cleared when the page reloads.

This is where data persistence comes in. Persistence means saving data so it remains available across browser sessions. For web applications like our Kanban board, the browser's localStorage API provides a simple yet powerful solution for client-side persistence.

Why localStorage is Perfect for Our Kanban Board

The localStorage API is part of the Web Storage API and allows you to store key-value pairs in the browser. These key-value pairs persist even when the page is closed or refreshed, making it perfect for saving user data like tasks. Here's what makes localStorage useful for our Kanban application:

  • It's built into all modern browsers
  • It can store up to 5–10 MB of data (varies by browser)
  • Data is stored as strings, so we'll need to use JSON to convert our task objects
  • It's synchronous and easy to use with simple get/set methods

By implementing localStorage persistence, our Kanban board will become much more practical for real-world use. Users will be able to add tasks, close their browsers, and return later to find their tasks exactly as they left them.

Our Persistence Implementation Plan

In this lesson, we'll modify our task store to load tasks from localStorage when the application starts and save tasks whenever they change. This will create a seamless experience where users never have to worry about losing their work.

We'll implement this in several steps:

  1. Create a function to load tasks from localStorage
  2. Set up automatic loading when the app starts
  3. Implement automatic saving when tasks change
  4. Add timestamp tracking to tasks

Creating the loadTasksFromStorage Function

The first step in implementing persistence is to create a function that retrieves saved tasks from localStorage. This function will be responsible for:

  1. Checking if there are any saved tasks in localStorage
  2. Parsing the saved tasks from JSON format
  3. Providing default tasks for first-time users
  4. Handling any errors that might occur during this process

Let's create this function in our task store file:

function loadTasksFromStorage() {
  try {
    const savedTasks = localStorage.getItem('kanban-tasks');
    return savedTasks ? JSON.parse(savedTasks) : [
      { id: 1, title: 'Learn Svelte 5', description: 'Study the new Runes API', status: 'todo', createdAt: new Date().toISOString() },
      { id: 2, title: 'Design components', description: '', status: 'todo', createdAt: new Date().toISOString() },
      { id: 3, title: 'Build Kanban board', description: 'Create the main layout', status: 'inprogress', createdAt: new Date().toISOString() },
      { id: 4, title: 'Setup project', description: 'Initialize SvelteKit', status: 'done', createdAt: new Date().toISOString() }
    ];
  } catch (error) {
    console.error('Failed to load tasks from localStorage:', error);
    return [
      { id: 1, title: 'Learn Svelte 5', description: 'Study the new Runes API', status: 'todo', createdAt: new Date().toISOString() }
    ];
  }
}
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal