Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowed to bypass cache-control parameter #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
To install the `nuxt-ssr-redis-cache` module in your project, use the following:

```
npm install @luispittagros/nuxt-ssr-redis-cache --save
npm install nuxt-ssr-redis-cache --save
```

## Configuration
Expand All @@ -19,7 +19,7 @@ You can configure `nuxt-ssr-redis-cache` in your `nuxt.config.js` file:
```js
modules: [
[
'@luispittagros/nuxt-ssr-redis-cache',
'nuxt-ssr-redis-cache',
{
enabled: true,
client: {
Expand All @@ -44,7 +44,7 @@ modules: [
Alternatively, you can set it up as follows:

```js
modules: ['@luispittagros/nuxt-ssr-redis-cache'],
modules: ['nuxt-ssr-redis-cache'],
ssrRedisCache: {
enabled: true,
client: {
Expand Down
176 changes: 43 additions & 133 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 43 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
{
"name": "@luispittagros/nuxt-ssr-redis-cache",
"version": "1.1.5",
"description": "Blazing Fast SSR page renderings using Redis.",
"main": "src/cache.js",
"author": "Luís Pitta Grós",
"license": "MIT",
"_from": "nuxt-ssr-redis-cache",
"_inBundle": false,
"_integrity": "sha512-5liQ6H+LQLlPHFG/cUH8qR7YUl7cUXKNAI1lod+7zyAKa0VK0fzk4yDT2tSeeR5E551Y2rVybXZZRgs4/NAmdA==",
"_location": "/nuxt-ssr-redis-cache",
"_phantomChildren": {},
"_requested": {
"type": "tag",
"registry": true,
"raw": "nuxt-ssr-redis-cache",
"name": "nuxt-ssr-redis-cache",
"escapedName": "nuxt-ssr-redis-cache",
"rawSpec": "",
"saveSpec": null,
"fetchSpec": "latest"
},
"_requiredBy": [
"#USER",
"/"
],
"_resolved": "https://registry.npmjs.org/nuxt-ssr-redis-cache/-/nuxt-ssr-redis-cache-1.0.0.tgz",
"_shasum": "c90d9d8b8d1245141f96d925542ef4063bf2f187",
"_spec": "nuxt-ssr-redis-cache",
"_where": "/Users/erencan/WebstormProjects/biletdukkani-b2c-web",
"author": {
"name": "Eren Can"
},
"bugs": {
"url": "https://github.com/erencan/nuxt-ssr-redis-cache/issues"
},
"bundleDependencies": false,
"dependencies": {
"cors": "^2.8.5",
"redis": "^4.1.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/luispittagros/nuxt-ssr-redis-cache.git"
},
"deprecated": false,
"description": "Blazing Fast SSR page renderings using Redis.",
"homepage": "https://github.com/erencan/nuxt-ssr-redis-cache#readme",
"keywords": [
"Nuxt",
"SSR",
Expand All @@ -21,8 +44,15 @@
"Page",
"Fast"
],
"bugs": {
"url": "https://github.com/luispittagros/nuxt-ssr-redis-cache/issues"
"license": "MIT",
"main": "src/cache.js",
"name": "nuxt-ssr-redis-cache",
"repository": {
"type": "git",
"url": "git+https://github.com/erencan/nuxt-ssr-redis-cache.git"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"homepage": "https://github.com/luispittagros/nuxt-ssr-redis-cache#readme"
"version": "1.2.0"
}
8 changes: 4 additions & 4 deletions src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default async function nuxtRedisCache(moduleOptions) {

renderer.renderRoute = async function (route, context) {
// Check if the route is cacheable, if not, just render the route (cache-control: no-cache is set when the browser cache is disabled)
if (!isCacheable(route, options.paths, context.req.headers['cache-control'])) {
if (!isCacheable(route, options.paths, context.req.headers['cache-control'], options.ignoreCacheControl)) {
return renderRoute(route, context)
}

Expand All @@ -72,7 +72,7 @@ export default async function nuxtRedisCache(moduleOptions) {
})

this.nuxt.hook('render:route', async (url, result, context) => {
if (!context.req.hitCache && isCacheable(url, options.paths, context.req.headers['cache-control'])) {
if (!context.req.hitCache && isCacheable(url, options.paths, context.req.headers['cache-control'], options.ignoreCacheControl)) {
client.set('nuxt/route::' + url, serialize(result), {
EX: options.ttl,
})
Expand Down Expand Up @@ -102,9 +102,9 @@ function buildOptions(moduleOptions) {
return Object.assign({}, defaultOptions, moduleOptions)
}

function isCacheable(url, paths = [], cacheControl = null) {
function isCacheable(url, paths = [], cacheControl = null, ignoreCacheControl = false) {
return (
cacheControl !== 'no-cache' && cacheControl !== 'no-store'
ignoreCacheControl?true:(cacheControl !== 'no-cache' && cacheControl !== 'no-store')
&& (!paths.length || paths.some((path) => (path instanceof RegExp ? path.test(url) : url.startsWith(path))))
)
}
Expand Down