Skip to content

When Cache API is not available, dont clone response for cachePut #8416

Open

Description

@asheemmamoowala

The cacheGet method calls back to validateOrFetch with a null error if window.caches is not available. From what I can tell, this could happen when the Cache API is not available at all or if a page is served over http and the Cache API is not usable.

export function cacheGet(request: Request, callback: (error: ?any, response: ?Response, fresh: ?boolean) => void) {
if (!window.caches) return callback(null);
window.caches.open(CACHE_NAME)
.catch(callback)
.then(cache => {
cache.match(request, { ignoreSearch: true })
.catch(callback)
.then(response => {

In this case validateOrFetch still calls clones the response and attempts to place it in the cache.

window.fetch(request).then(response => {
if (response.ok) {
const cacheableResponse = cacheIgnoringSearch ? response.clone() : null;
return finishRequest(response, cacheableResponse, requestTime);

If it is known that window.caches is no available or usable, there is not need to clone the response or to load its contents:

const finishRequest = (response, cacheableResponse, requestTime) => {
(
requestParameters.type === 'arrayBuffer' ? response.arrayBuffer() :
requestParameters.type === 'json' ? response.json() :
response.text()
).then(result => {
if (cacheableResponse && requestTime) {
// The response needs to be inserted into the cache after it has completely loaded.

Activity

  1. ahk commented on Jul 8, 2019

    @ahk
    Contributor

    Didn't a fix for this get merged into our most recent release as a late patch, or do I misunderstand the extent of the bug?

  2. asheemmamoowala commented on Dec 11, 2019

    @asheemmamoowala
    ContributorAuthor

    @ahk This was not addressed. I can still reproduce this in the latest master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions