Skip to content

Viewport prefetching #3633

Closed
Closed
@bluwy

Description

@bluwy

Describe the problem

Currently sveltekit:prefetch works by requesting data on hover or before click only. In some cases, it may be desirable to prefetch immediately when the link enters the viewport, similar to how nextjs works today.

#3041 also highlighted some points there.

Describe the proposed solution

Provide a way to define the prefetching strategy, either by viewport or hover. This can be achieved by passing a value to the attribute, e.g. sveltekit:prefetch="<strategy>". By default with no value, it would fallback to the hover strategy.

For viewport prefetching, IntersectionObservers can't be used as anchor tags change often and to detect that MutationObservers are needed, but that imposes a lot of potential overhead. An alternative is to query select anchor tags and prefetch them in short intervals. (Ideas welcomed)

To detect when to invalidate the data, maxage can be used. To avoid discouraging short maxage (which indirectly cause repeated prefetching), a syntax like sveltekit:prefetch="viewport:1000" could be supported to denote "invalidate after 1s". Or a config option can be used to set a minimum prefetch interval (similar #790)

Alternatives considered

Refer users to implement their own viewport prefetch logic using actions. Caveat is that links in contents from CDN can't take advantage of the declarative prefetching syntax.

Importance

nice to have

Additional Information

  1. Rich noted that prefetch invalidation may not always be wasteful as the modules requested are usually reusable, only the data is stale.
  2. Adding viewport prefetch implementation may require some infrastructure refactoring.
  3. I might've forgotten some points from the last maintainer's meeting.

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature / enhancementNew feature or requestp2-nice-to-haveSvelteKit cannot be used by a small number of people, quality of life improvements, etc.prefetch

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions