Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .ncurc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"upgrade": false,
"reject": ["@types/node"]
}
79 changes: 50 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
![nuxt-multi-cache banner](./docs/public/banner.jpg?raw=true 'nuxt-multi-cache for Nuxt 3')
![nuxt-multi-cache banner](./docs/public/banner.jpg?raw=true 'nuxt-multi-cache for Nuxt')

# Nuxt Multi Cache for Nuxt 3
# Nuxt Multi Cache

This module provides several layers of server-side caching for your Nuxt 3 app:
This module provides several layers of server-side caching for your Nuxt app:

- SSR component caching (caches rendered markup of a component)
- Route caching (pages, API routes)
Expand All @@ -14,36 +14,28 @@ This module provides several layers of server-side caching for your Nuxt 3 app:
**[Documentation](https://nuxt-multi-cache.dulnan.net)** -
**[NPM](https://www.npmjs.com/package/nuxt-multi-cache)**

## Nuxt 2
It is compatible with Nuxt 3 and Nuxt 4.

[Version 1.x](https://github.com/dulnan/nuxt-multi-cache/tree/1.x) (which is in
maintenance mode) supports Nuxt 2.
## Features

## Why?

Does your Nuxt app serve thousands of pages from a CMS? Does it have tens of
thousands of requests per day? Does the data change frequently? Does rendering a
single page require fetching data from multiple APIs? If you've answered any of
these questions with "yes", then this module might be for you.
### [Data Cache](https://nuxt-multi-cache.dulnan.net/features/data-cache)

I work fulltime on building frontends with Nuxt for large CMS sites. Rendering a
single page might require up to 10 API calls to get all the data: Menu, footer,
translations, route, page data, user state, additional data... It all adds up
and doing that for every request can quickly become a bottleneck. Maybe you can
work around this problem by getting all the data in a single API call, but I
didn't like to have components dependent on global state.
Cache data directly in your Nuxt app or Nitro event handlers using
`useDataCache`:

Instead my solution was to cache the API responses locally on the server. Either
for a fixed amount of time, like 5 minutes, or until the cache entry is being
invalidated. In addition, I cache components that appear on most pages, like
menu or footer.
```typescript
const { value, addToCache } = await useDataCache<WeatherResponse>('weather')
if (value) {
return value
}

The hardest thing in IT is cache invalidation, so I also added a way to
invalidate cache entries by key or using cache tags.
const response = await $fetch<WeatherResponse>('/api/getWeather')
await addToCache(response)

# Features
return response
```

## Component caching
### [Component Cache](https://nuxt-multi-cache.dulnan.net/features/component-cache)

Use the `<RenderCacheable>` wrapper component to cache the markup of the default
slot:
Expand All @@ -61,7 +53,7 @@ slot:
The component is only rendered once and its markup cached. Afterwards the markup
is directly returned.

## Route caching
### [Route Cache](https://nuxt-multi-cache.dulnan.net/features/route-cache)

Cache rendered pages or custom API responses:

Expand All @@ -74,7 +66,7 @@ useRouteCache((route) => {
</script>
```

## CDN cache control headers
### [CDN Cache Control Headers](https://nuxt-multi-cache.dulnan.net/features/cdn-cache-control)

Manage the cacheability for the current response. This will set the correct
cache control and cache tags headers for Cloudflare, Fastly and other cache
Expand All @@ -97,6 +89,35 @@ The state is managed inside the current request and can be changed for the
entire duration of the request. The headers are generated right before the
response is sent.

## API
### [API](https://nuxt-multi-cache.dulnan.net/features/api)

The optional API provides endpoints to manage the caches.

```bash [curl]
curl -X POST -i \
-H "x-nuxt-multi-cache-token: hunter2" \
--data '["Navbar::de--chf"]' \
http://localhost:3000/__nuxt_multi_cache/purge/component
```

## Why?

Does your Nuxt app serve thousands of pages from a CMS? Does it have tens of
thousands of requests per day? Does the data change frequently? Does rendering a
single page require fetching data from multiple APIs? If you've answered any of
these questions with "yes", then this module might be for you.

I work fulltime on building frontends with Nuxt for large CMS sites. Rendering a
single page might require up to 10 API calls to get all the data: Menu, footer,
translations, route, page data, user state, additional data... It all adds up
and doing that for every request can quickly become a bottleneck. Maybe you can
work around this problem by getting all the data in a single API call, but I
didn't like to have components dependent on global state.

Instead my solution was to cache the API responses locally on the server. Either
for a fixed amount of time, like 5 minutes, or until the cache entry is being
invalidated. In addition, I cache components that appear on most pages, like
menu or footer.

The hardest thing in IT is cache invalidation, so I also added a way to
invalidate cache entries by key or using cache tags.
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
---
layout: home

title: Nuxt Multi Cache for Nuxt 3
title: Nuxt Multi Cache for Nuxt

hero:
name: Advanced Caching for Nuxt 3
name: Advanced Caching for Nuxt
tagline:
Seamless caching of components, routes and data. Dynamically define CDN
cache control headers. Provides cache management API for purging items by
Expand Down
Binary file modified docs/public/banner.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading