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
ἔρως
ἔρως12mo ago
do you have any caching being done on your side?
MD
MDOP12mo ago
not currently let me show you
ἔρως
ἔρως12mo ago
then implement some it isn't too hard
MD
MDOP12mo ago
MD
MDOP12mo 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
ἔρως
ἔρως12mo 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
MDOP12mo ago
ok so cache the api results
ἔρως
ἔρως12mo ago
yes, in the server now, you have to think about how you will cache it
MD
MDOP12mo ago
not sure how go does that
ἔρως
ἔρως12mo 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
MDOP12mo ago
really right now I have to see why my code stopped working completely
ἔρως
ἔρως12mo ago
good luck debugging it
MD
MDOP12mo ago
ill be back when I get that resolved
ἔρως
ἔρως12mo ago
alright, take your time
MD
MDOP12mo ago
Like usual it's the regex
ἔρως
ἔρως12mo ago
🤣 which regex?
MD
MDOP12mo ago
"City is not valid"
MD
MDOP12mo 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
MDOP12mo ago
The issue is I type "Lexington" or "Columbus" and it crashes
ἔρως
ἔρως12mo ago
because of the 2nd regex
MD
MDOP12mo ago
The if statement I need to evaluate that differently
ἔρως
ἔρως12mo ago
i don't know what you're trying to do, but, im not sure if it is even a good approach
MD
MDOP12mo 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
ἔρως
ἔρως12mo ago
did you tested it in regex101 or something?
MD
MDOP12mo ago
Yeah Regex works The if failed
ἔρως
ἔρως12mo ago
the if looks ... iffy
MD
MDOP12mo ago
Yeah I goofed it up I'll fix it first tomorrow then try the next step
ἔρως
ἔρως12mo ago
good luck
Want results from more Discord servers?
Add your server