Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

269
Visualizações
How to not fetch all of the comments from the database all the time (how to store arrays in localstorage)

Hello I'm making a comment section on my website but I don't want to fetch all of the comments from my database when someone refreshes the page I want to solve this problem by storing comments in the array but when I use

export const comments = writable(localStorage.getItem("comments") || []);

it doesn't create an array in my local storage is there even a way to store an array in local storage with svelte or should I handle this problem differently?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you want to store something in localstorage user:

localstorage.setItem('comments', JSON.stringify(arr_of_cmnts);

Now, let's assume when your page opens you check if your localstorage has comments by using something like:

//In svelte
let arr_of_cmnts = [];
export const comments = writable(localStorage.getItem('comments')|| JSON.stringify(arr_of_cmnts));
//js **normally**
const cmnts = JSON.parse(localstorage.getItem('comments'));
if(cmnts && cmnts.length > 0)
    //use this array
else 
    //call server to get comnts and store them in localstorage

In the if block how do you know if comments in localstorage are the latest??? Maybe someone has put a new comment from the last time you visited.

There maybe some solutions to this, like you inquiring db for latest comments timestamp and then checking whether the stored comments are stale or not. Another approach of the issue (not having to query your db always) is to use a Cache (redis maybe), to serve comments, and whenever someone writes a new comment, along with updating db you can update your cache as well. This makes read request faster.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda