|
| 1 | +--- |
| 2 | +# These are optional metadata elements. Feel free to remove any of them. |
| 3 | +status: "accepted" |
| 4 | +date: 2025-09-18 |
| 5 | +decision-makers: @gadomski @AliceR |
| 6 | +--- |
| 7 | + |
| 8 | +# Use a fetch library for caching |
| 9 | + |
| 10 | +## Context and Problem Statement |
| 11 | + |
| 12 | +Currently, `stac-react` uses the native `fetch` API for all STAC requests, with no built-in caching or request deduplication. As the library is intended for use in applications that may navigate between many STAC resources, efficient caching and request management are important for performance and developer experience. |
| 13 | + |
| 14 | +## Decision Drivers |
| 15 | + |
| 16 | +- Improve performance by caching repeated requests. |
| 17 | +- Reduce network usage and latency. |
| 18 | +- Provide a more robust API for request state, error handling, and background updates. |
| 19 | +- Align with common React ecosystem practices. |
| 20 | + |
| 21 | +## Considered Options |
| 22 | + |
| 23 | +- Continue using native `fetch` with custom caching logic. |
| 24 | +- Use TanStack Query (`@tanstack/react-query`) for fetching and caching. |
| 25 | +- Use another fetch/caching library (e.g., SWR, Axios with custom cache). |
| 26 | + |
| 27 | +## Decision Outcome |
| 28 | + |
| 29 | +**Chosen option:** Use TanStack Query (`@tanstack/react-query`). |
| 30 | + |
| 31 | +**Justification:** |
| 32 | +TanStack Query is widely adopted, well-documented, and provides robust caching, request deduplication, background refetching, and React integration. It will make `stac-react` more attractive to downstream applications and reduce the need for custom caching logic. |
| 33 | + |
| 34 | +### Consequences |
| 35 | + |
| 36 | +- **Good:** Improved performance and developer experience; less custom code for caching and request state. |
| 37 | +- **Bad:** Adds a new dependency and requires refactoring existing hooks to use TanStack Query. |
| 38 | + |
| 39 | +### Confirmation |
| 40 | + |
| 41 | +- Implementation will be confirmed by refactoring hooks to use TanStack Query and verifying caching behavior in tests and example app. |
| 42 | +- Code review will ensure correct usage and integration. |
| 43 | + |
| 44 | +## Pros and Cons of the Options |
| 45 | + |
| 46 | +### TanStack Query |
| 47 | + |
| 48 | +- **Good:** Robust caching, request deduplication, background updates, React integration. |
| 49 | +- **Good:** Well-supported and documented. |
| 50 | +- **Neutral:** Adds a dependency, but it is widely used. |
| 51 | +- **Bad:** Requires refactoring and learning curve for maintainers. |
| 52 | + |
| 53 | +### Native Fetch |
| 54 | + |
| 55 | +- **Good:** No new dependencies. |
| 56 | +- **Bad:** No built-in caching, more custom code required, less robust for complex scenarios. |
| 57 | + |
| 58 | +### Other Libraries (SWR, Axios) |
| 59 | + |
| 60 | +- **Good:** Some provide caching, but less feature-rich or less adopted for React. |
| 61 | +- **Bad:** May require more custom integration. |
| 62 | + |
| 63 | +## More Information |
| 64 | + |
| 65 | +- [TanStack Query documentation](https://tanstack.com/query/latest/docs/framework/react/overview) |
| 66 | +- This ADR will be revisited if TanStack Query no longer meets project needs or if a better alternative emerges. |
0 commit comments