Skip to content
This repository was archived by the owner on Oct 12, 2021. It is now read-only.

Offline dynamic content not loading using freshness caching strategyΒ #179

@THLAP

Description

@THLAP

I am having issues around caching dynamic content on beta16. I found with the "freshness" strategy none of my api requests were hitting the cache. This was due to the fetch method making a request and resolving the promise with the actual response from the server which was 503. Because of this the fetchFromCache code block is never executed and you are left with 503s. See explanation below.

freshness.ts

The call to fetchAndCache fails but returns a response object with a 503 status code.

const unrestrictedFetch = group
      // Make a request to the network and cache the result, irrespective of the
      // timeout.
      .fetchAndCache(req, delegate)
      // If this operation fails (note that a failed HTTP status code is still
      // counted as success, treat it as an unavailable response.
      // TODO: allow more control over what constitutes request failure and
      // what happens in the case of failure.
      .catch(() => ({response: null} as ResponseWithSideEffect));

// By default, wait for the network request indefinitely.
let networkFetch = unrestrictedFetch;

When networkFetch resolves the first if block checks if response is null and if it is executes. In this case we have a valid response object so the cache isn't hit.

return networkFetch
      .then(rse => {
        if (rse.response === null) {
          // Network request failed or timed out. Check the cache to see if
          // this request is available there.
          return group
            .fetchFromCache(req)
            .then(cacheRse => {
              // Regardless of whether the cache hit, the network request may
              // still be going, so set up a side effect that runs the cache
              // effect first and the network effect following. This ensures
              // the network result will be cached if/when it comes back.
              const sideEffect = () => maybeRun(cacheRse.sideEffect)
                .then(() => unrestrictedFetch)
                .then(netRse => maybeRun(netRse.sideEffect));

I changed the condition as below and it works for me now.

if (rse.response === null || !rse.response.ok) {

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions