Skip to content

Commit e7abda3

Browse files
committed
Implement delete all cached pages, if the paths to be cached array is empty every path is cached, update README.md
1 parent 0e20085 commit e7abda3

File tree

4 files changed

+35
-20
lines changed

4 files changed

+35
-20
lines changed

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Nuxt SSR Redis Cache
22

3-
:rocket: Blazing Fast Server Side Rendering using Redis.
3+
:rocket: Blazing Fast Nuxt Server Side Rendering using Redis.
4+
5+
Easily improves you Nuxt application performance.
46

57
## Setup
68

@@ -18,15 +20,15 @@ modules: [
1820
'@luispittagros/nuxt-ssr-redis-cache',
1921
{
2022
enabled: true,
21-
ttl: 60 * 60,
2223
client: {
2324
socket: {
2425
host: '127.0.0.1',
2526
port: 6379,
2627
},
2728
password: null,
2829
},
29-
paths: [/^\/$/, '/articles/'],
30+
paths: [/^\/$/, '/articles/'], // If empty or "/" is set all pages will be cached
31+
ttl: 60 * 60, // 1 minute
3032
cacheCleanEndpoint: {
3133
enabled: false,
3234
path: '/ssr-redis-cache',
@@ -43,15 +45,15 @@ or
4345
modules: ['@luispittagros/nuxt-ssr-redis-cache'],
4446
ssrRedisCache: {
4547
enabled: true,
46-
ttl: 60 * 60,
4748
client: {
4849
socket: {
4950
host: '127.0.0.1',
5051
port: 6379,
5152
},
5253
password: null,
5354
},
54-
paths: [/^\/$/, '/articles/'],
55+
paths: [/^\/$/, '/articles/'], // If empty or "/" is set all pages will be cached
56+
ttl: 60 * 60, // 1 minute
5557
cacheCleanEndpoint: {
5658
enabled: true,
5759
path: '/ssr-redis-cache',
@@ -68,16 +70,16 @@ For the client configuration check node-redis for reference [here](https://githu
6870

6971
Creates an endpoint that cleans cached paths, it's useful when you have content that might change often.
7072

71-
To call the endpoint you must make a POST request to your Nuxt appplication using the path you defined (defaults to '/ssr-redis-cache') with the following request body:
73+
To call the endpoint you must make a POST request to your Nuxt appplication using the path you defined (defaults to '/ssr-redis-cache') with the following JSON request body:
7274

7375
```json
7476
{
75-
"paths" : [ "/", "/example/" ]
77+
"paths" : [ "/", "/example/" ] // Use "*" as path to delete all cached pages
7678
}
7779
```
7880

79-
For CORS options check the express cors middleware options [here](https://expressjs.com/en/resources/middleware/cors.html).
81+
For the CORS options check the express cors middleware options [here](https://expressjs.com/en/resources/middleware/cors.html).
8082

8183
### Cached Pages
8284

83-
Cacheable pages have the HTTP response header "X-Cache" which may have a value of "HIT" or "MISS" accordingly.
85+
Cacheable/Cached pages have the HTTP response header "X-Cache" which have the value "HIT" or "MISS" accordingly.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@luispittagros/nuxt-ssr-redis-cache",
3-
"version": "1.0.8",
3+
"version": "1.1.0",
44
"description": "Blazing Fast SSR page renderings using Redis.",
55
"main": "src/cache.js",
66
"author": "Luís Pitta Grós",

src/cache.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,18 @@ export default async function nuxtRedisCache(moduleOptions) {
2828

2929
this.nuxt.hook('render:setupMiddleware', () => {
3030
const isCacheCleanEndpointEnabled = options.cacheCleanEndpoint && options.cacheCleanEndpoint.enabled !== false
31-
31+
3232
if (isCacheCleanEndpointEnabled) {
33-
this.addServerMiddleware({ path: options.cacheCleanEndpoint.path || '/ssr-redis-cache', handler: ssrRedisCacheMiddleware(client, options.cacheCleanEndpoint)})
33+
this.addServerMiddleware({
34+
path: options.cacheCleanEndpoint.path || '/ssr-redis-cache',
35+
handler: ssrRedisCacheMiddleware(client, options.cacheCleanEndpoint),
36+
})
3437
}
35-
36-
console.log('[Nuxt SSR Redis Cache]:', 'Cache clean endpoint is ' + (isCacheCleanEndpointEnabled ? 'enabled\x1b[32m ✔' : 'disabled\x1b[31m ✘') + '\n')
38+
39+
console.log(
40+
'[Nuxt SSR Redis Cache]:',
41+
'Cache clean endpoint is ' + (isCacheCleanEndpointEnabled ? 'enabled\x1b[32m ✔' : 'disabled\x1b[31m ✘') + '\n',
42+
)
3743
})
3844

3945
this.nuxt.hook('render:before', async (renderer) => {
@@ -72,7 +78,7 @@ export default async function nuxtRedisCache(moduleOptions) {
7278
context.res.setHeader('X-Cache', hitCache ? 'HIT' : 'MISS')
7379
}
7480

75-
if (!hitCache && isCacheable(url, options.paths)) {
81+
if (!hitCache && isCacheable(url, options.paths, context.req.headers['cache-control'])) {
7682
client.set('nuxt/route::' + url, serialize(result), {
7783
EX: options.ttl,
7884
})
@@ -93,17 +99,19 @@ function buildOptions(moduleOptions) {
9399
ttl: 60 * 60,
94100
paths: [],
95101
cacheCleanEndpoint: {
96-
enabled: false,
102+
enabled: false,
97103
path: '/ssr-redis-cache',
98104
cors: true,
99-
}
105+
},
100106
}
101107

102108
return Object.assign({}, defaultOptions, moduleOptions)
103109
}
104110

105111
function isCacheable(url, paths = [], cacheControl = null) {
106-
return cacheControl !== 'no-cache' && paths.some((path) => (path instanceof RegExp ? path.test(url) : url.startsWith(path)))
112+
return (
113+
cacheControl !== 'no-cache' && (!paths.length || paths.some((path) => (path instanceof RegExp ? path.test(url) : url.startsWith(path))))
114+
)
107115
}
108116

109117
module.exports.meta = require('../package.json')

src/serverMiddleware.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const express = require('express')
22
const { json } = require('body-parser')
33
const cors = require('cors')
44

5-
function ssrRedisCacheMiddleware(redisClient, options = {}) {
5+
function ssrRedisCacheMiddleware(client, options = {}) {
66
const app = express()
77

88
app.use(json())
@@ -16,7 +16,12 @@ function ssrRedisCacheMiddleware(redisClient, options = {}) {
1616
}
1717

1818
try {
19-
await Promise.all(paths.map((path) => redisClient.del('nuxt/route::' + path)))
19+
if (paths.length === 1 && paths[0] === '*') {
20+
const { keys = [] } = await client.scan(0, { MATCH: 'nuxt/route::*' });
21+
await Promise.all(keys.map((path) => client.del(path)))
22+
} else {
23+
await Promise.all(paths.map((path) => client.del('nuxt/route::' + path)))
24+
}
2025

2126
return res.status(200).json({ success: true })
2227
} catch (error) {

0 commit comments

Comments
 (0)