According to the specification:
The Object Cache represents a request Response list. Multiple Separate Objects implementing the Cache interface Across Documents and Workers can all be Associated with the same request Response list simultaneously.
In English free:
A Cache object represents a response list to the request. Multiple separate objects implementing the Cache interface across multiple documents and Workers can be associated with the same request response list simultaneously.
Answering your questions:
Does it serve exclusively for requests made with Request? Or could it be used with the standard Javascript Xtmlhttprequest?
No, only with Request. The cache is designed to be used in principle with Service Workers, and is part of its specification. The cache API only stores pairs of Request/Response objects, which respectively represent the HTTP request/Sponse protocol. The XMLHTTPRequest
is very primitive and in general I wouldn’t recommend using it for anything. The fetch
has plenty of support nowadays, plus a good polyfill.
In a way, the cache can be seen as a array
of Request objects, functioning as answer keys, which the browser then stores.
This class can be used to cache something other than the one mentioned in the first question?
Through the request cache you can store almost all static content of an application. The cache is virtually all the content of a basic Serviceworker, and with it you can store fonts, pages (see explanation below), images, Javascript files, CSS, etc. Example:
Note that you don’t necessarily need Service Worker to use the API, for example to store static resources, you don’t need to:
// Verifica que o navegador tem suporte para a API
if('caches' in window) {
// Faz o cache de `assets` como fonte e imagem
caches.open('meuCacheAqui').then(objetoDoCache => {
objetoDoCache.addAll([
'/assets/fonts/suaFonte.woff',
'/assets/img/imagemBemBonita.jpg'
])
.then(_ => console.log('Cache Realizado!'))
})
}
But if you want to store a page, then you would need a Service Worker to handle the browser request for offline resources. Couldn’t do it with the API Cache alone.
The reason you need a Serviceworker is that while a page can be read and written via a cache window.caches
, you would need a Serviceworker to intercept the page’s initial request so you can reply with a cache.
Where this cache is stored?
In the browser itself, within the profile of the user who accessed the cache, within the storage of the Service Worker. In Chromium-based browsers, it stays inside the folder Service Worker
. I can’t tell the others.
What is the support for this feature? If there is a lack of support, it is possible to do Polyfill?
For support, see the MDN. A very stable Polyfill can be found here.
Useful Links (English):
Who gave the negative can explain what can be improved?
– Wallace Maxters
Since you posted on Stackoverflow in Portuguese, I think reading the documentation in Portuguese might help you, have you looked at it? https://developer.mozilla.org/en-US/docs/Web/API/Cache
– Fellipe Sanches
@Fellipesanches I believe the question is about technical specifications regarding the implementation of the class
Cache
. So much so that the link you passed is included in the question.– Augusto Vasques