npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@viatu/nuxt-multi-cache

v3.1.1

Published

SSR route, component and data cache for Nuxt.js

Downloads

97

Readme

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.