Automatic Revalidation Strategies
What you'll learn: How SWR keeps your data fresh automatically without you lifting a finger—and when that's brilliant vs overkill.
SWR doesn't just fetch data once and forget about it. By default, it actively watches for common scenarios where your data might have become stale and automatically refetches to keep things fresh. This is the "revalidate" part of stale-while-revalidate—SWR assumes cached data could be outdated and proactively checks for updates.
The Three Default Triggers
Out of the box, SWR revalidates in three scenarios:
- On window focus — User switches back to your tab? SWR refetches to show the latest data.
- On network reconnect — User's WiFi comes back online? SWR immediately updates.
- On interval — You can configure periodic polling (disabled by default, but easy to enable).
Think of it like a news app: when you return to the app after checking messages, it automatically refreshes to show new headlines. You didn't pull-to-refresh—it just knew you'd want fresh content.
When It's Beneficial
This behavior shines for:
- Real-time-ish data (social feeds, dashboards, notifications)
- Collaborative tools where others might change data
- Preventing stale data bugs without manual refetch logic
When It's Excessive
For static or slow-changing data (user profile, settings, archived content), constant revalidation wastes bandwidth and API calls. You'll want to dial it back with configuration options.
The beauty is SWR gives you sensible defaults for dynamic data while letting you opt out when needed—unlike manually managing refetch logic everywhere.
Key Takeaway: SWR's automatic revalidation on focus, reconnect, and intervals keeps data fresh by default, ideal for dynamic apps but configurable to prevent unnecessary requests for static content.