Skip to content

We should have default strategies for caching #73

Closed

Description

@ovflowd

As pointed out by @MattIPv4 (https://openjs-foundation.slack.com/archives/C05TRU8PZL6/p1699990976027789) we should be explicit about caching strategies.

And we should also:

  • Only cache 2XX responses, and if they're get Requests (explicit cache)
  • Our default responses (on the constants) should have explicit headers for no-cache/no-store
  • 5XX responses also should not have cache
  • We should extract many other implicit constants as mentioned fix: return a 400 for malformed URIs #64 (comment) to proper constant files

Activity

  1. self-assigned this
    on Nov 15, 2023
  2. flakey5 commented on Nov 15, 2023

    @flakey5
    Member

    We should extract many other implicit constants ... to proper constant files

    Do we want to just move the object to a constants file like this

    // src/constants/r2Prefixes.ts
    export function getUrlToBucketPathMap(url) {
      return {
        dist: DIST_PATH_PREFIX + (url.pathname.substring('/dist'.length) || '/'),
        download:
          DOWNLOAD_PATH_PREFIX +
          (url.pathname.substring('/download'.length) || '/'),
        docs: DOCS_PATH_PREFIX + (url.pathname.substring('/docs'.length) || '/'),
        api: API_PATH_PREFIX + (url.pathname.substring('/api'.length) || '/'),
        metrics: url.pathname.substring(1), // substring to cut off the /
      }
    }
    
    // src/util.ts
    const urlToBucketPathMap = getUrlToBucketPathMap(url);
    // ...
    } else if (basePath in urlToBucketPathMap) {
      bucketPath = urlToBucketPathMap[basePath];
    }

    or can we do something like this, where we only construct the url we need rather than all of them

    // src/constants/r2Prefixes.ts
    export function constructDistPath(url) {
      return DIST_PATH_PREFIX + (url.pathname.substring('/dist'.length) || '/');
    }
    // constructDownloadPath, constructDocsPath, ...
    
    // src/util.ts
    } else {
      switch (basePath) {
        case 'dist': {
          bucketPath = constructDistPath(url)
          break;
        }
        // download, docs, ...
        default: {
          if (env.DIRECTORY_LISTING === 'restricted') {
            return undefined;
          }
          bucketPath = url.pathname.substring(1);
        }
      }
    }
    ...

    or was there something else you were thinking about?

  3. ovflowd commented on Nov 20, 2023

    @ovflowd
    MemberAuthor
    // src/constants/r2Prefixes.ts
    export function getUrlToBucketPathMap(url) {
      return {
        dist: () => `${DIST_PATH_PREFIX}${url.pathname.substring('/dist'.length) || '/'}`,
        download: () => `${DOWNLOAD_PATH_PREFIX}${url.pathname.substring('/download'.length) || '/'}`,
        ...
      }
    }
    
    // src/util.ts
    const urlToBucketPathMap = getUrlToBucketPathMap(url);
    // ...
    } else if (basePath in urlToBucketPathMap) {
      bucketPath = urlToBucketPathMap[basePath];
    }
  4. ovflowd commented on Nov 20, 2023

    @ovflowd
    MemberAuthor

    I think it's important to mention that we have numerous constant files and it would make sense to have similar constants grouped together (ie. assume we have multiple constants for dist related things or download/ related things, it makes sense to have all these paths and logic to be close together

  5. flakey5 commented on Nov 20, 2023

    @flakey5
    Member

    Is there a reason behind why dist: () => `${DIST_PATH_PREFIX}${url.pathname.substring('/dist'.length) || '/'}`, instead of dist: DIST_PATH_PREFIX + (url.pathname.substring('/dist'.length) || '/'),?

  6. ovflowd commented on Nov 20, 2023

    @ovflowd
    MemberAuthor
    // src/constants/r2Prefixes.ts
    const BUCKET_TO_PATHNAME_MAP = {
      dist: ({ pathname }) => `${DIST_PATH_PREFIX}${pathname.substring('/dist'.length) || '/'}`,
      download: ({ pathname }) => `${DOWNLOAD_PATH_PREFIX}${pathname.substring('/download'.length) || '/'}`,
      ...
    };
    
    // ...
    } else if (basePath in BUCKET_TO_PATHNAME_MAP) {
      bucketPath = BUCKET_TO_PATHNAME_MAP[basePath](url);
    }
  7. ovflowd commented on Nov 20, 2023

    @ovflowd
    MemberAuthor

    The main reasoning behind is: You don't calculate and do all these string interpolation at once...

  8. flakey5 commented on Nov 21, 2023

    @flakey5
    Member

    Do we want to tell clients to locally cache HEAD requests? It's what we're doing now, but given we don't cache them on the worker's end I wanted to double check

    'cache-control': cacheControl,

  9. added a commit that references this issue on Nov 21, 2023
    c53e585
  10. linked a pull request that will close this issuefix: caching #75on Nov 21, 2023
  11. added 3 commits that reference this issue on Nov 21, 2023
    694c535
    996da0f
    794ef8f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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