To implement data persistence without any server or backend, you can use client-side storage options like cookies, Web Storage (localStorage and sessionStorage), and IndexedDB.
Here's a brief overview of each option and how to use them in JavaScript:
- Cookies: Cookies are small pieces of data stored by the browser on the user's computer. They are primarily used for session management and tracking user preferences. Cookies have a size limit of 4KB per domain and can be sent with every HTTP request, which might cause performance issues in large applications.
To set a cookie in JavaScript:
document.cookie = "key=value; expires=Thu, 01 Jan 2025 00:00:00 UTC; path=/";
To read and use cookies:
function getCookie(key) {
let cookieArray = document.cookie.split(';');
for (let cookie of cookieArray) {
let keyValuePair = cookie.split('=');
if (keyValuePair[0].trim() === key) {
return keyValuePair[1];
}
}
}
let value = getCookie("key");
- Web Storage: Web Storage (consisting of localStorage and sessionStorage) is a more modern approach for storing key-value pairs in the user's browser with better performance and greater storage capacity (usually around 5-10 MB). localStorage persists data even after the browser is closed, while sessionStorage clears the data when the user closes the browser or a tab.
To set data in localStorage or sessionStorage:
localStorage.setItem('key', 'value');
sessionStorage.setItem('key', 'value');
To get data from localStorage or sessionStorage:
let value1 = localStorage.getItem('key');
let value2 = sessionStorage.getItem('key');
- IndexedDB: IndexedDB is a more advanced, client-side storage that allows you to store complex data structures like objects and files. It's especially useful for large data storage and provides features like indexing, querying, and transactional processing. However, IndexedDB involves a more complex API compared to the previous options.
To open and use IndexedDB, you can use the following example:
const dbName = "MyDatabase";
const objectStoreName = "MyData";
// Open or create the IndexedDB database
const openReq = indexedDB.open(dbName);
openReq.onupgradeneeded = function (event) {
const db = event.target.result;
db.createObjectStore(objectStoreName, { keyPath: "id" });
};
openReq.onsuccess = function (event) {
const db = event.target.result;
// Add data to the object store
const data = { id: 1, value: "example" };
const transaction = db.transaction([objectStoreName], "readwrite");
const objectStore = transaction.objectStore(objectStoreName);
const req = objectStore.add(data);
// Retrieve data from the object store
req.onsuccess = function () {
const getRequest = objectStore.get(1);
getRequest.onsuccess = function () {
console.log(getRequest.result);
};
};
};
Keep in mind that client-side storage solutions are limited to the user's browser and can be cleared by the user at any time. If you want to store data persistently on a server, you'll need backend support.
For a personal website with pure front-end technologies, you can use one of these client-side storage options based on your requirements and the amount and complexity of data you want to store.