How does browser caching currently work?

There are files in my website (.css, .js, .html) that are going to change constantly but at the same time there are many others (with the same extensions mentioned above) that will always be static so I want to tell the browser which files it is allowed to cache. How do I do it using Go?

On the other hand, I read that browsers always cache .html files, if that is true, how the hell are dynamic websites going to work? that would force to use a lot of javascript and would not allow the use of techniques such as templates. It is worth mentioning that I read such information in old posts and I could not find current information, but my instinct tells me that currently any browser caches .html files, am I right?

Static pages are rendered as they are by Go.
Static pages with dynamic data where the data is passed to the template to {{.}} and rendered by Go.
Fully dynamic pages is normally built by Javascript and rendered by the browser.

And there are pages rendered in Go and parts of the content is updated using Javascript and innerHTML.

That is how I interpret it. But the term “dynamic” is floating.

1 Like

This is my case, so should I worry about the browser caching the .html?

Browser is caching the pages to enhance the speed next time you load the same content. What do you worry about? Normally caching is a good thing. Hence I use Cloudflare to increase the cache further…

Cache is useful but not if the content changes on the server side.

The cache will then always be out of date.

For dynamic HTML content, you typically generate or render HTML content based on user requests or business logic. In the example, a simple dynamic HTML content is generated directly within the request handler function.

Add Cache-Control header through your ResponseWriter.

w.Header().Set("Cache-Control", "no cache")
1 Like

If I do that, what will happen to the .css and .js files in the html? Will those also stop being saved in the cache or are they independent of the origin header?

It should be simple enough to code your handler to add appropriate cache control for each file to meet your criteria a la

if !thisFileShouldNotBeCached(file) {
  # Set cache control no cache here
}

Oh sorry, I was referring to the client side, if I tell the browser not to cache the .html, what will happen to the .css and .js (which are links in the body of the .html)? Will the same rules I put in the header for the .html apply to them or is that independent for each file?

Don’t do anything on the client side. The server indicates the caching control. The browser knows what to do from the cache control header for every resource it retrieves.

Here’s what you can do on the client, but I would avoid it and just set the appropriate header on the server.

Here are more cache related links:

1 Like

if I tell the browser (using the header)* not to cache the .html