/    Sign up×
Community /Pin to ProfileBookmark

Javascript and cache

OK, I normally do C# and stay on the server side, but I have been attempting to get a little more proficient with javascript.

This may be a dumb question, but is javascript sometimes cached along with the page by the browser? Twice I have had something like this happen using VSCode and Live Server: spend a lot of time working on something. Make changes, save and reload in browser; sometimes close the browser to wipe session variables. Everything works as expected. Then I run into a problem that I can’t fix. Work on it for a couple of hours or more. Nothing works. Come back to it the next day and fire everything back up, and the same code goes right to work with me **making no changes whatsoever**.

As an example, I spent about three hours working on some code that threw an error every single way I tried it, but it looked like it should work. I posted a question on stackoverflow and the first response was that it looked fine to them and should work.

I go and try the block of code again and it works on the first try. At this point I feel kind of stupid, and I wasted a lot of time in the process.

This really never happens to me in C#, other than a handful of times. The issue in these cases was more of an IIS issue than a C# issue, though. I mean, I can kind of offer a rational explanation when it happened in these cases.

With javascript, I’m just left wondering how I managed to waste so much time on code that seemed like it was really working all along.

So is there some pro trick I should know that would save me some time? Clear the browser cache more? Some other secret?

Also, can’t really search for this, because the keywords turn up a metric ton of how to store variables in session/local storage!

Thanks,

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@sibertJan 22.2022 — > @jay8anks#1641813 This may be a dumb question, but is javascript sometimes cached along with the page by the browser?

This happens to me every day. Annoying, though I know the reason.

The web page can be cached at 2 levels.
  • 1. The browser caches the old code until you clear the cache (different depending on the browser)

  • 2. If you use some kind of CDN (like Cloudflare), the pages are cached also there for speed reasons.


  • So in this case, you have to clear the cache on both places. But after a while it clears itself automatically. It can take minutes and it can take days.

    There is a trick if you use Chrome that I use. Right click on the page and select "Inspect". When you are in this mode, right click on the redraw icon and select "Clear cache" from the popup menu. This way it clears only the current page. Nothing else.

    If you use Cloudflare, there is a "developer mode" and you can "purge cache".
    Copy linkTweet thisAlerts:
    @SempervivumJan 22.2022 — There are some measures to overcome these issues:
  • - Use Ctrl+F5 to reload, this will bypass the cache and force loading of JS and CSS files being included.

  • - Add a version number to the path of a file being included, e. g. `src="js/yourscript.js?v=3"</C>, and increase it every time the file has been modified.</LI>
    <LI>- When loading a file by use of Ajax add a timestamp to the URL: <br/>
    <C>
    fetch('https://your-domain.com/your-script.php?t=' + Date.now()).then`
  • Copy linkTweet thisAlerts:
    @jay8anksauthorJan 23.2022 — Thanks guys. In asp.net, a lot of it is compiled on the server side, so for the most part, when I reload a page, whatever code changes I have made are reflected instantly. The only time I run into a situation like this is when I upload class files that are in use at the time and IIS doesn't immediately start serving the updated code. Annoying, but even this happens pretty infrequently.

    I must admit, I love working on the server side. It's so much easier. A little slower and a little clunkier, but way easier.

    I like that Ctrl+F5 tip. That's one I'll start using today.



    ×

    Success!

    Help @jay8anks spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.19,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...