I have a question regarding handling responses in localStorage. When someone visits URLs like /products/1 or /products/2, the frontend automatically sends the corresponding id (1 or 2) to the server. The server then responds with data for that particular id. My goal is to store this response in localStorage, but I'm facing an issue. Every time a new URL is visited, such as /products/2, the previous data in localStorage gets replaced instead of being added to it. How can I ensure that new data is continuously added to localStorage without overriding the existing value?
fetch("/apps/proxy/sendid", {
method: "POST",
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(jspid)
}).then(function (response) {
// The API call was successful!
console.log(response);
return response.json();
}).then(function (data) {
// This is the JSON from our response
console.log(data);
const productsDetails = JSON.parse(localStorage.getItem('future')) || [];
productsDetails.push(data);
localStorage.setItem('future', JSON.stringify(productsDetails));
}).catch(function (err) {
// There was an error
console.warn('Something went wrong.', err);
});
}