Git Product home page Git Product logo

waynegibson / nuxt-multi-cache Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dulnan/nuxt-multi-cache

0.0 0.0 0.0 2.35 MB

Advanced caching of components, routes and data for Nuxt 3. Dynamically define CDN cache control headers. Provides cache management API for purging items by key or using cache tags.

Home Page: https://nuxt-multi-cache.dulnan.net

License: MIT License

JavaScript 0.38% TypeScript 91.90% Vue 7.72%

nuxt-multi-cache's Introduction

nuxt-multi-cache banner

Nuxt Multi Cache for Nuxt 3

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

  • SSR component caching (caches rendered markup of a component)
  • Route caching (pages, API routes)
  • Data Caching (generic cache for anything)
  • CDN cache control headers (e.g. Cloudflare, Fastly, Varnish)
  • API for cache management
  • Cache tag based invalidation

Documentation - NPM

Nuxt 2

Version 1.x (which is in maintenance mode) supports Nuxt 2.

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.

Features

Component caching

Use the <RenderCacheable> wrapper component to cache the markup of the default slot:

<template>
  <div>
    <RenderCacheable cache-key="navbar_de" :max-age="3600">
      <Navbar />
    </RenderCacheable>
  </div>
</template>

The component is only rendered once and its markup cached. Afterwards the markup is directly returned.

Route caching

Cache rendered pages or custom API responses:

<script lang="ts" setup>
useRouteCache((route) => {
  // Mark the page as cacheable for 1 hour and add a cache tag.
  route.setCacheable().setMaxAge(3600).addTags(['page:2'])
})
</script>

CDN cache control headers

Manage the cacheability for the current response. This will set the correct cache control and cache tags headers for Cloudflare, Fastly and other cache providers:

<script lang="ts" setup>
useCDNHeaders((helper) => {
  helper
    .public()
    .setNumeric('maxAge', 3600)
    .setNumeric('staleWhileRevalidate', 21600)
    .set('mustRevalidate', true)
    .addTags(['page:2', 'image:342'])
})
</script>

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

The optional API provides endpoints to manage the caches.

nuxt-multi-cache's People

Contributors

dulnan avatar jpsc avatar bgondy avatar applelo avatar valajan avatar blackyuzia avatar niddu85 avatar hpawa avatar or2e avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.