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

@dbbs/next-cache-handler

v1.1.0

Published

Support to control cache behaviour for NextJS applications.

Downloads

8

Readme

Next-Cache-Handler

Introduction

next-cache-handler is designed to elevate the performance of Next.js applications by providing a robust caching solution that adapts to cookies, query parameters, and device types. This enables the delivery of content that is finely tuned to the context of each user, significantly improving load times and user experience.

Table of Contents

Features

  • Cache Segmentation: Dynamically segment cache storage based on cookies, query parameters, and device types.
  • Multiple Storage Options: Supports various caching mechanisms including file system cache, in-memory cache, and Redis.
  • Customizable: Flexible configuration options to tailor caching strategies to specific needs.

Quick Start

Installation

Ensure you have Node.js version 12 or higher installed.

npm i -s next-cache-handler
# or
yarn add next-cache-handler

Configuration

Create a cacheHandler.js in your project and configure as follows:

import { CacheHandler, FileSystemCache } from 'next-cache-handler'

CacheHandler
    .setCacheStrategy(new FileSystemCache())
    .addCookie('my-cookie-1')
    .addCookie('my-cookie-2')
    .addQuery('my-query-1')
    .addQuery('my-query-2')
    .addDeviceSplit()
    // Additional configuration...
export default CacheHandler    

Update your next.config.js to use the cache handler.

const nextConfig = {
    cacheHandler: require.resolve('./cacheHandler.js')
}

export default nextConfig

Cache Strategies

Explore various caching strategies to find the best fit for your application's needs:

  • MemoryCache: Ideal for short-lived data in applications with limited cache size requirements. By default limit of size is set to 512MB.
import { CacheHandler, MemoryCache } from 'next-cache-handler'

CacheHandler.setCacheStrategy(new MemoryCache())
import { CacheHandler, MemoryCache } from 'next-cache-handler'

// extending size limitation to 1024MB.
CacheHandler.addStrategy(new MemoryCache({ sizeLimit: 1024 }))
  • FileSystemCache: Suitable for persistent caching across application restarts.
import { CacheHandler, FileSystemCache } from 'next-cache-handler'

CacheHandler.setCacheStrategy(new FileSystemCache())
  • Redis: Perfect for distributed applications requiring shared cache access.
import { CacheHandler, RedisCache } from 'next-cache-handler'

// list of all accepted redis connection options.
const redisConnectionOpts = {
    url: 'redis://localhost:5843',
    ...
}

CacheHandler.setCacheStrategy(new RedisCache(redisConnectionOpts))

API Reference

addCookie

Cookie name what is going to be used to fragmentate cache based on browser cookies session.

CacheHandler.addCookie('my-cookie-to-split')

addQuery

Query name to fragmentate cache based on url query parameters.

CacheHandler.addQuery('my-query-to-split')

addDeviceSplit

Enables to fragmentate experience based on current User-Agent of the request.

CacheHandler.addDeviceSplit()

setCacheStrategy

Applies given strategy to cache page data.

CacheHandler.setCacheStrategy(new MemoryCache())

addNoCacheRoute

Add route to ignore cache list. All routes added here would be excluded from caching and will always render again.

Logging

Leverage the built-in logger for monitoring cache operations or integrate your custom logger for advanced logging capabilities.

import { BaseLogger, CacheHandler, FileSystemCache } from 'next-cache-handler'

class MyCustomLogger implements BaseLogger {
    constructor() {}

    info(data) {
        console.log(data)
    }

    error(data) {
        console.error(data)
    }
}

CacheHandler.setCacheStrategy(new FileSystemCache(new MyCustomLogger()))

Contributing

  • Code Contributions: When contributing code, ensure it adheres to the project's coding standards and write tests where applicable.
  • Documentation: If you are contributing to documentation, ensure your changes are clear, concise, and helpful for other users.
  • Bug Reports and Feature Requests: Use the GitHub Issues section to report bugs or suggest new features. Please provide as much detail as possible to help us understand the issue or feature.

License

The next-cache-handler is open-source software licensed under the MIT License.

Contact Information

We value your feedback and contributions to the next-cache-handler. If you have any questions or suggestions or need support, here are several ways to get in touch with us:

  • General Inquiries and Support: For any general questions about the platform or if you need assistance, please visit our website DBB Software and use the contact form provided.

  • GitHub Issues: For specific issues, feature requests, or bugs related to the platform, please use the GitHub Issues page. This is the fastest way to directly communicate with our development team and track the resolution of your issue.

  • Community Discussion and Contributions: Join our community discussions on GitHub Discussions for broader topics, ideas exchange, and collaborative discussions.

  • Social Media: Follow us on our social media channels for the latest news, updates, and insights:

  • Email Contact: For more formal or detailed inquiries, feel free to reach out to us via email at [email protected].

We're always here to help and are committed to ensuring you have the best experience with the next-cache-handler. Your input and participation drive the continuous improvement of our platform.