Prevent spamming of api calls when Refresh is pressed or back + forward continuously

What I need to do is store the user's results of the first call and then either cache or localstorage / sessionstorage so when the user presses the buttons mentioned above, the page will load that data instead of doing multiple calls. I also need a timer to do another call after an hour to update for any weather changes. Any ideas on this?
28 Replies
ἔρως
ἔρως13mo ago
do you have any caching being done on your side?
MD
MDOP13mo ago
not currently let me show you
ἔρως
ἔρως13mo ago
then implement some it isn't too hard
MD
MDOP13mo ago
MD
MDOP13mo ago
right now I just have backend code no js yet just something im working on with js in this file called storedData.js
// adding code to store the user's session
const pageReloadCheck = (
(window.performance
.getEntriesByType('navigation')
.map((nav) => nav.type)
.includes('reload')
)
);

alert(pageReloadCheck)
// adding code to store the user's session
const pageReloadCheck = (
(window.performance
.getEntriesByType('navigation')
.map((nav) => nav.type)
.includes('reload')
)
);

alert(pageReloadCheck)
test code but in theory it should be able to check for reload and back + forward then I write a function to convert the table data into json to stringify it so I can make a key value pairing for the storage tbh I never worked with caching much espeically in web
ἔρως
ἔρως13mo ago
you really should add some caching, even if it is an horrible globa variable to store what you fetch from the api take care of the server first, then think about the client
MD
MDOP13mo ago
ok so cache the api results
ἔρως
ἔρως13mo ago
yes, in the server now, you have to think about how you will cache it
MD
MDOP13mo ago
not sure how go does that
ἔρως
ἔρως13mo ago
one thing you can do is to check if whatever you're using to get the stuff from the api is using http 1.1 or higher
MD
MDOP13mo ago
really right now I have to see why my code stopped working completely
ἔρως
ἔρως13mo ago
good luck debugging it
MD
MDOP13mo ago
ill be back when I get that resolved
ἔρως
ἔρως13mo ago
alright, take your time
MD
MDOP13mo ago
Like usual it's the regex
ἔρως
ἔρως13mo ago
🤣 which regex?
MD
MDOP13mo ago
"City is not valid"
MD
MDOP13mo ago
GitHub
Weather-App/src/server/validateInput/validateInput.go at main · MD-...
A weather app using a third party api with the goal being a build upon project with additional features as time goes on. - MD-2016/Weather-App
MD
MDOP13mo ago
The issue is I type "Lexington" or "Columbus" and it crashes
ἔρως
ἔρως13mo ago
because of the 2nd regex
MD
MDOP13mo ago
The if statement I need to evaluate that differently
ἔρως
ἔρως13mo ago
i don't know what you're trying to do, but, im not sure if it is even a good approach
MD
MDOP13mo ago
It handles the case of entering a city like Lexington then the other is for those same city different state like Charleston, WV vs Charleston, SC
ἔρως
ἔρως13mo ago
did you tested it in regex101 or something?
MD
MDOP13mo ago
Yeah Regex works The if failed
ἔρως
ἔρως13mo ago
the if looks ... iffy
MD
MDOP13mo ago
Yeah I goofed it up I'll fix it first tomorrow then try the next step
ἔρως
ἔρως13mo ago
good luck
Want results from more Discord servers?
Add your server