1.0.2 • Published 1 year ago

nuxt-elastic-cache v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

nuxt-elastic-cache

npm version npm downloads License Nuxt

Flexible fully transparent cache middleware for Nuxt3 SSR rendering with Redis support

Quick Setup

  1. Add nuxt-elastic-cache dependency to your project
# Using pnpm
pnpm add -D nuxt-elastic-cache

# Using yarn
yarn add --dev nuxt-elastic-cache

# Using npm
npm install --save-dev nuxt-elastic-cache
  1. Add nuxt-elastic-cache to the modules section of nuxt.config.ts
export default defineNuxtConfig({
  modules: [
    'nuxt-elastic-cache'
  ],
  elasticCache: {
    enabled: true,
    storage: {
      type: 'redis',
      redis: {
        url: 'redis://localhost:6379',
        /* Or */
        host: 'localhost',
        port: 6379,
        username: '..',
        password: '..',
        db: 0
      },
    },
    pages: [
      /*
       * Powered by https://github.com/isaacs/minimatch
       * Also supports RegExp
      */
      '/about/*',
      '/about/**/*ending'
    ],
    key (req) {
      return `c-${yourCustomLogic(req)}`
    }
  },
})

Configuration

Top level configuration

OptionTyperequiredDescriptionDefault
enabledbooleanNoTo enable/disable the SSR cachetrue
storageStorageNoConfig storage for cacheundefined
pagesstring\|RegExp\| Array<string\| RegExp>NoPages to be cached[]
key(req: IncomingMessage) => string \| falseNoCan be used to generate custom key. Return falsy value to bypass cache(req) => req.url

Storage configuration

OptionTypeRequiredDescriptionDefault
type'memory' \| 'redis'YesCache storage'memory'
memory.ttlnumberNoNumber in seconds to store page in cache1 hour
memory.maxnumberNoMax number of pages to store in cache. If limit is reached, least recently used page is removed500
redis.urlstringNoString to Redis instance. This option has priority over options below.undefined
redis.ttlnumberNoNumber in seconds to store page in cache1 hour
redis.hoststringNoRedis instance hostundefined
redis.portnumberNoRedis instance portundefined
redis.usernamestringNoRedis instance usernameundefined
redis.passwordstringNoRedis instance passwordundefined
redis.dbnumberNoRedis instance dbundefined

That's it! You can now use nuxt-elastic-cache in your Nuxt app ✨

Development

# Install dependencies
npm install

# Generate type stubs
npm run dev:prepare

# Develop with the playground
npm run dev

# Build the playground
npm run dev:build

# Run ESLint
npm run lint

# Run Vitest
npm run test
npm run test:watch

# Release new version
npm run release

Caveat

important security warning: don't load secret keys such as user credential on the server for cached pages. this is because they will cache for all users!

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago