facebook / facebook/relay

Query loaded with @preloadable not retained in store for long

Open
#4,735 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
19k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

## Context
Currently, I am using `loadQuery()` to prefetch a query for another page when the user hovers over navigation items. The preloaded reference is purely to prime the relay store with the query results optimistically (I'm currently using `useLazyloadQuery` on those pages). The `loadQuery()` response is disposed in the `onMouseLeave()` for this button to allow it to be GCed, but currently if I hover the button again it does not fire a new query due to a fetchPolicy of 'store-or-network' (and the prefetch being retained in the store for a bit, even though I've disposed the reference). I currently use the concrete request to load this query (to avoid impacting bundle sizes, this prefetching file would do an async import for the concrete request file).

With support for @preloadable in new versions of relay, I wanted to change this pre-fetch query to just directly import the preloadable concrete request to simplify logic and avoid the async import of the large concrete request. However, I'm seeing some unexpected behavior when using it.

## Issue
When using `loadQuery()` with a preloadable concrete request (from `'..Query$parameters`), repeated calls to `loadQuery(env, preloadableRequest, {}, {fetchPolicy: 'store-or-network')` each trigger a new network request immediately once the reference is disposed. This seems to be different behavior from a non-preloadable request, which would be satisfied by the relay store on subsequent triggers even if disposed (for some window of time at least).

I can probably delay the disposal of the prefetch to avoid these repeated network calls in my case, but I'm wondering if this is expected behavior. It seems odd for the preload query to be disposed faster than a regular query, but maybe I've missed somewhere this can be configured.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.