# Persistent Handlebars context

**URL:** <https://forum.kirupa.com/t/persistent-handlebars-context/641837>\
**Category:** web dev\
**Created:** [February 20, 2020, 8:44pm UTC](https://forum.kirupa.com/t/persistent-handlebars-context/641837 "2020-02-20T20:44:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dannywolf](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/dannywolf/32/10471_2.png) [@dannywolf](https://forum.kirupa.com/u/dannywolf)\
**Post date:** [February 20, 2020, 8:44pm UTC](https://forum.kirupa.com/t/persistent-handlebars-context/641837/1 "2020-02-20T20:44:23Z")

</div>

I have an ExpressJS app which contacts a remote API and passes the results to a Handlebars template. That works nicely but the API is contacted every time the page is displayed, which is quite unnecessary.

For example: imagine the API returns user data (address, phone number etc). Once it has been retrieved and displayed, I should not need to hit the API again next time the user navigates to the same page.

Is there a way to check if the data has been previously retrieved from the API and have the template use a local copy, say, retrieved from a sessionStorage?

Cheers

---

<div class="post-metadata">

**Author:** ![dannywolf](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/dannywolf/32/10471_2.png) [@dannywolf](https://forum.kirupa.com/u/dannywolf)\
**Post date:** [February 26, 2020, 6:43pm UTC](https://forum.kirupa.com/t/persistent-handlebars-context/641837/2 "2020-02-26T18:43:33Z")

</div>

As it turns out, I had part of the answer already. I was looking at the issue the wrong way. I wanted my Handlebars template to do something is it not meant to do.

The answer is indeed to use sessionStorage, but that is set in my Node/Express code. So, the first time the page is loaded, there is no sessionStorage set, the route executes code to fetch data from the API, passes it to my Handlebars template and creates the sessionStorage.

Next time the page is accessed, the sessionStorage is already set and Node/Express sends that info to Handlebars without accessing the API.

In the end, all I needed was a nudge to the right direction. 🤷‍♂️

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [February 26, 2020, 6:53pm UTC](https://forum.kirupa.com/t/persistent-handlebars-context/641837/3 "2020-02-26T18:53:13Z")

</div>

Agreed! That is probably the best way to accomplish it. If you want to persist the data longer, you could also use `localStorage`. Also, are you checking to see if the the data on the server is more recent and _ **forcing** _ an API call if the data is indeed more recent on the server?

🏝

---

<div class="post-metadata">

**Author:** ![dannywolf](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/dannywolf/32/10471_2.png) [@dannywolf](https://forum.kirupa.com/u/dannywolf)\
**Post date:** [March 2, 2020, 6:14pm UTC](https://forum.kirupa.com/t/persistent-handlebars-context/641837/4 "2020-03-02T18:14:50Z")

</div>

For the sake of those with similar doubts,I should add something else I have learned: **localStorage** and **sessionStorage** are _browser only_ storages. They are used in the front-end and **NOT** something we set on the server side using Node/Express.

I managed to solve my problem using the _express-session_ module for NodeJs, first alone, storing the info in memory for testing purposes, and then coupling it with the _connect-mongo_ module to store it in MongoDB.

We live and learn. To me, that is the fun part of programming! ¯\_(ツ)\_/¯
