Section Data Fetching
Introduction
Weaverse’s section components can fetch their own data, which provides several key advantages:- Modularity: Each component manages its own data dependencies
- Performance: Only fetch what’s needed when it’s needed
- Maintainability: Data fetching logic lives with the component that uses it
- Reusability: Components can be used in multiple contexts with different data
Core Concepts
The Component Loader Pattern
Weaverse section components use the loader pattern for server-side data fetching:- Receives arguments via
ComponentLoaderArgs
- Accesses the Weaverse client and component data
- Fetches necessary data from APIs
- Returns data that will be automatically passed to the component as
props.loaderData
Type Safety with TypeScript
Using TypeScript with your component loaders provides several benefits:- Auto-completion in your IDE
- Type checking during development
- Better documentation for component usage
- Clearer contract between components and their data
Data Sources
Shopify Storefront API
The most common data source for Weaverse components is Shopify’s Storefront API:External APIs
Weaverse components can also fetch data from any external API using thefetchWithCache
utility:
fetchWithCache
function:
- Works like the standard
fetch
API - Adds Hydrogen’s caching capabilities
- Makes external API calls more efficient
- Provides a consistent interface for all data fetching
Component Data and Settings
Thedata
argument in ComponentLoaderArgs
contains all the component’s settings configured by merchants in the Weaverse editor:
Implementation Patterns
Basic Data Fetching
The simplest pattern is direct data fetching based on component settings:Conditional Fetching
Often you’ll need to conditionally fetch data based on component settings:Parallel Data Fetching
For optimal performance, fetch multiple data sources in parallel:Error Handling
Robust error handling ensures your components degrade gracefully when APIs fail:Dependent Queries
Sometimes you need to fetch data sequentially, where one request depends on the results of another:Data Revalidation
Weaverse provides a powerful mechanism to automatically refresh component data when specific settings change. This ensures that the displayed content always reflects the current configuration.Understanding shouldRevalidate
TheshouldRevalidate
property, when added to an input in your component schema, tells Weaverse to reload the component’s data from its loader function whenever that specific input changes.
sortOrder
in the editor, Weaverse will:
- Update the component’s data with the new value
- Re-run the component’s loader function
- Refresh the component with the updated data
Auto-Revalidating Inputs
Some input types automatically trigger revalidation without needing theshouldRevalidate
property:
product
- When selecting a different productcollection
- When selecting a different collectionblog
- When selecting a different blogproduct-list
- When selecting different productscollection-list
- When selecting different collections
Custom Revalidation Rules
You can create powerful data-driven components by combiningshouldRevalidate
with component loaders:
- Changing the collection, sort order, or number of products triggers a data reload
- Changing the view style doesn’t require new data, so
shouldRevalidate
is set tofalse
Caching Strategies
Weaverse components inherit Hydrogen’s powerful caching system, allowing you to optimize performance based on how frequently your data changes.Available Caching Options
Strategy | Cache Control Header | Best Used For |
---|---|---|
CacheShort() | public, max-age=1, stale-while-revalidate=9 | Frequently changing data (price, inventory) |
CacheLong() | public, max-age=3600, stale-while-revalidate=82800 | Rarely changing data (product details, images) |
CacheNone() | no-store | Personalized or uncacheable data |
CacheCustom() | Custom defined | Special caching requirements |
Custom Caching
For fine-tuned control over caching behavior:Caching Best Practices
-
Match cache duration to data volatility:
- Product descriptions:
CacheLong()
- Prices and inventory:
CacheShort()
- User-specific content:
CacheNone()
- Product descriptions:
- Use stale-while-revalidate for a balance of freshness and performance
- Consider cache hierarchies when combining multiple data sources
- Be mindful of API rate limits when setting short cache durations
- Use cache debugging headers during development to verify caching behavior
Real-World Examples
E-commerce Examples
Product Recommendations ComponentContent Integration
Blog Feed with CategoriesThird-Party Services
Currency Converter WidgetPerformance Optimization
To ensure your section components load quickly:-
Use parallel fetching with
Promise.all()
for independent data sources - Implement appropriate caching strategies based on data freshness requirements
- Filter data server-side whenever possible to reduce payload size
- Consider data dependencies to avoid waterfall requests
- Return only what you need to minimize response size
- Handle errors gracefully with fallback content
- Monitor API response times and optimize slow requests
Troubleshooting
Common issues and their solutions: 1. Missing or undefined dataRelated Documents
To further enhance your understanding of Weaverse’s data fetching capabilities and component development, explore these related guides:- Component Schema - Learn how to define component schemas, including the
shouldRevalidate
property - Input Settings - Comprehensive guide to all input types available in Weaverse
- Rendering a Weaverse Page - Understanding how Weaverse pages are rendered
- Project Structure - Learn how the project is organized