Skip to content

Replace maxage in load with more detailed cache option #4549

@Rich-Harris

Description

@Rich-Harris

Describe the problem

Pages can return a maxage property from load which a) sets a Cache-Control header on the server response, and b) causes the returned value to be cached in memory so that navigating back to the page won't result in a repeated load.

This mostly works, but has limitations:

  • determining whether to add public is based on a heuristic (no fetch without credentials: 'omit', and no session access) that doesn't cover every edge case
  • there's nowhere to express revalidate
  • some people want to be able to express s-maxage rather than maxage, though I'll confess I don't fully understand why

Describe the proposed solution

Replace maxage with a cache object:

<script context="module">
  /** @type {import('@sveltejs/kit').Load} */
  export async function load() {
    return {
      props: {...},
      cache: {
        maxage: 3600,
        private: false // if unset, uses existing heuristic
      }
    };
  }
</script>

In future, we could add revalidate and smaxage if those prove necessary.

Alternatives considered

No response

Importance

nice to have

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions