Let's say you have a resource, could be an image, could be jQuery from a cdn. This resource is hosted at some 3rd party url, like https://example-cdn.com/resource.ext
. Let's also assume it is cacheable (whatever that means--let me know if that is a non-trivial detail).
When https://website-a.com
requests the resource (let's assume it was included in the html directly), it takes some time to load, but then the browser caches it for faster load next time.
Now, https://website-b.com
is also including that resource in its html, using the exact same url (https://example-cdn.com/resource.ext
).
My question is this: will the browser reach for the cached resource (because it was already fetched while loading https://website-a.com
), or is there some reason that it would not be able to find it in the cache and have to load it over the network all over again?
Edit: This stackexchange answer seems to contain some related information. Can anyone verify that this answer is correct in all its assertions about caching? https://webmasters.stackexchange.com/a/84685
Yes, the resource will be cached.
This follows from the semantics of HTTP and URLs. A URL is a Universal Resource Location: it provides the location of the resource, in a form that can be used anywhere, and which always indicates the same resource: in <a>
elements of different web sites, on business cards, on advertising posters. An HTTP client (a web browser) knows that a URL one web site has refers to the same resource if used on a different web site, and so it is safe to reuse a cached copy.
The exception to this is when a URL is a relative URL (your example uses absolute URLs). To make use of a relative URL the client must resolve the URL, using some context, to produce an absolute URL. Different web sites have different contexts and thus resolve to different absolute URLs. It is the absolute URL that the client must use to fetch resources and which is used as the key in its cache.