> Portal Navigation: > > - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version. > - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages). > - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`). > - Top-level index of all portals: https://dev.wix.com/docs/llms.txt > - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt ## Resource: Implement Router Caching ## Article: Implement Router Caching ## Article Link: https://dev.wix.com/docs/develop-websites-sdk/code-your-site/best-practices/caching/implement-router-caching.md ## Article Content: # Implement Router Caching [Router caching](https://dev.wix.com/docs/develop-websites-sdk/code-your-site/best-practices/caching/about-router-caching.md) in an effective strategy to improve your site's performance by temporarily storing the return values from the `ok()` method, which contain the same page data that's sent as the response to a router request. When visitors access a router page, the system can quickly deliver the cached data, enabling faster page loads and a better overall user experience. This article guides you through the process of implementing router caching and how to invalidate it when necessary. ## Step 1 | Set up your cache You must assign identifiers to router caches and can optionally define a cache's Time To Live (TTL). These required identifiers, called tags, allow you to easily identify caches that may need invalidation. For example, you can invalidate all caches that have the tag "dress-page." To set up caching for your routers: 1. Open the [router.js](https://dev.wix.com/docs/develop-websites-sdk/code-your-site/code-with-java-script/about-code-placement.md#routers) file in your backend code and find the routers you want to cache. 1. In the request for each router page you want to cache, create a variable representing the `options` parameter to store the `cache` object. Include the following fields in the `cache` object: - `tags`: One or more identifiers of the cached return value. - `ttl`: The cache's time-to-live in seconds. If omitted, defaults to 1 week (`604800` seconds). 1. In the return statement of the router request, pass the variable holding the `cache` object to the [`ok()` method](https://dev.wix.com/docs/velo/events-service-plugins/routers/service-plugins/wix-router/ok.md). ```js import { ok } from "wix-router"; export function myRouter_Router(request) { let headOptions = {}; let cacheOptions = { cache: { tags: ["routerPage"], ttl: 518400, }, }; return ok("router-page", dataObj, headOptions, cacheOptions); } ```
**Important:** The `tags` field is required for caching. If you omit `tags`, nothing is cached.
## Step 2 | Invalidate caches Invalidating or clearing your caches triggers the routing and rendering process upon the following next, ensuring that your caches hold the most up-to-date page data. To invalidate caches, use the `invalidateCache()` method from the [Cache API](https://dev.wix.com/docs/sdk/backend-modules/cache/cache/invalidate-cache.md). This method accepts the `invalidationMethods` parameter, which specifies the tags previously assigned to the caches you want to invalidate.
**Important:** - You must specify `invalidationMethods` when invalidating your cache. - A cache can be assigned multiple tags. Any cache with at least 1 tag specified in the `invalidationMethods` object will be invalidated when `invalidateCache()` is called. - The `invalidateCache()` method must be [elevated](https://dev.wix.com/docs/sdk/core-modules/essentials/auth.md#elevate) and wrapped in a [web method](https://dev.wix.com/docs/sdk/core-modules/web-methods/introduction.md).
To invalidate caches: 1. Add the necessary imports in your backend files. ```js import { Permissions, webMethod } from "@wix/web-methods"; import { auth } from "@wix/essentials"; import { cache } from "@wix/cache"; ``` 1. Create a variable to store the tags assigned to the cache. Then call `auth.elevate()` and store its returned value in another variable. ```js const invalidationMethods = [{ tag: "math" }, { tag: "multiply" }]; const elevatedInvalidateCache = auth.elevate(cache.invalidateCache); ``` 1. Call `webMethod()` and store its return value in an exported variable. Pass a permissions value and the elevated invalidation method. ```js export const invalidateCache = webMethod(Permissions.Admin, async () => { try { await elevatedInvalidateCache(invalidationMethods); console.log("Successfully invalidated cache."); } catch (error) { console.error(error); } }); ``` > **Note:** To invalidate the server side rendering (SSR) cache of your site, refer to the [`invalidateCache()`](https://dev.wix.com/docs/velo/apis/wix-site-backend/invalidate-cache.md) method in the `wix-site-backend` module. ## See also - [About Caching](https://dev.wix.com/docs/develop-websites-sdk/code-your-site/best-practices/caching/about-caching.md) - [About Router Caching](https://dev.wix.com/docs/develop-websites-sdk/code-your-site/best-practices/caching/about-router-caching.md) - [About Routers](https://dev.wix.com/docs/develop-websites-sdk/code-your-site/build-a-custom-backend/routers/about-routers.md) - [Create a Router](https://dev.wix.com/docs/develop-websites-sdk/code-your-site/build-a-custom-backend/routers/create-a-router.md) - [wix-router API](https://dev.wix.com/docs/velo/events-service-plugins/routers/service-plugins/wix-router/introduction.md) - [Cache API](https://dev.wix.com/docs/sdk/backend-modules/cache/cache/introduction.md)