Router caching can significantly enhance your site's performance. When a site visitor successfully navigates to a page handled by a router, the router processes the request and retrieves the corresponding router page data. The response, which includes this data, is sent back to the visitor using the ok()
method in the wix-router
module. With router caching implemented, the return value of the ok()
method — which contains the same router page data — is temporarily stored on Wix's infrastructure. This allows for immediate retrieval of the cached router page data the next time the same page is requested, reducing the need for the full router and rendering process, and ultimately improving load times for subsequent visitors.
You can manage caches by specifying a duration, known as Time To Live (TTL), which defines how long the return values should be stored. Additionally, you can assign identifying tags to the cache, allowing for precise cache invalidation when necessary. Caching improves overall performance by delivering content faster, reducing the server load, and enabling your site to accommodate more concurrent visitors. Caches also serve as a reliable backup if the original data source becomes temporarily unavailable.
Note: Router pages, unlike regular site pages, aren't cached automatically.
You can implement router caching using:
Router caching is particularly beneficial for:
For instance, in an online department store, when a site visitor first accesses the "Dresses" category (located at clothing/womens-clothing/dresses
), the page data is retrieved through the router, cached, and then rendered. As a result, subsequent visitors requesting the "Dresses" page experience significantly reduced loading times. Similarly, for router pages like popular articles on a news website, caching helps efficiently handle high view counts.
Conversely, router caching is less effective for requests that depend on:
The personalized nature of these scenarios makes caching less practical. For example, caching the router response for daily weather updates on a page for a visitor in London would be irrelevant for someone in California. Additionally, caching responses based on unique user filters or search terms is inefficient, as traffic for those exact queries is generally minimal.
Cache invalidation is important for maintaining the accuracy and freshness of your site. It involves clearing caches to allow for the retrieval of updated content before the cache is renewed. Router caches are cleared when:
To invalidate caches in your code, you can use the invalidateCache()
method from wix-cache-backend
. This should be done when the content on your router page changes or when the page displays time sensitive information, such as updates in a clothing catalog, or weekly updates to a calendar.
By invalidating caches, you maintain data integrity, prevent site visitors from accessing outdated information, and optimize your site's performance and responsiveness.