Skip to main content

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
This guide covers everything you need to know about fetching data in Weaverse section components.

Core Concepts

The Component Loader Pattern

Weaverse section components use the loader pattern for server-side data fetching:
The loader function:
  1. Receives arguments via ComponentLoaderArgs
  2. Accesses the Weaverse client and component data
  3. Fetches necessary data from APIs
  4. 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:
By specifying the input and output types, you get:
  • 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 the fetchWithCache utility:
The 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

The data 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

The shouldRevalidate 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.
When a merchant changes the sortOrder in the editor, Weaverse will:
  1. Update the component’s data with the new value
  2. Re-run the component’s loader function
  3. Refresh the component with the updated data
This creates a seamless experience where changes in the editor immediately update the displayed content.

Auto-Revalidating Inputs

Some input types automatically trigger revalidation without needing the shouldRevalidate property:
  • product - When selecting a different product
  • collection - When selecting a different collection
  • blog - When selecting a different blog
  • product-list - When selecting different products
  • collection-list - When selecting different collections
These inputs deal with Shopify resources that typically require fresh data when changed.

Custom Revalidation Rules

You can create powerful data-driven components by combining shouldRevalidate with component loaders:
In this example:
  • 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 to false
This optimizes performance by only reloading data when necessary, while ensuring that content stays fresh and relevant as merchants configure their components.

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

Custom Caching

For fine-tuned control over caching behavior:

Caching Best Practices

  1. Match cache duration to data volatility:
    • Product descriptions: CacheLong()
    • Prices and inventory: CacheShort()
    • User-specific content: CacheNone()
  2. Use stale-while-revalidate for a balance of freshness and performance
  3. Consider cache hierarchies when combining multiple data sources
  4. Be mindful of API rate limits when setting short cache durations
  5. Use cache debugging headers during development to verify caching behavior

Real-World Examples

E-commerce Examples

Product Recommendations Component

Content Integration

Blog Feed with Categories

Third-Party Services

Currency Converter Widget

Performance Optimization

To ensure your section components load quickly:
  1. Use parallel fetching with Promise.all() for independent data sources
  2. Implement appropriate caching strategies based on data freshness requirements
  3. Filter data server-side whenever possible to reduce payload size
  4. Consider data dependencies to avoid waterfall requests
  5. Return only what you need to minimize response size
  6. Handle errors gracefully with fallback content
  7. Monitor API response times and optimize slow requests

Troubleshooting

Common issues and their solutions: 1. Missing or undefined data
2. Type errors in loader data Use TypeScript to catch issues early:
3. API rate limiting issues Implement proper caching and error handling:
To further enhance your understanding of Weaverse’s data fetching capabilities and component development, explore these related guides: