Socket
Socket
Sign inDemoInstall

nextleton

Package Overview
Dependencies
0
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    nextleton


Version published
Maintainers
1
Install size
3.70 kB
Created

Readme

Source

nextleton

A super simple utility to create dev server safe singleton for your NextJS app.

Problem: When NextJS hot-reloads, it re-imports your files, re-creating instances of global variables. This is a problem if you want to create a singleton that is shared across your app. This is a problem for example if you want to create a database connection, or a logger, or a cache, or a service that you want to share across your app and you want the behaviour to be consistent across hot-reloads.

Solution: Use nextleton to create a singleton that is safe for hot-reloads.

Installation

npm install nextleton

or

yarn add nextleton

Usage

Instant Singleton

import { nextleton } from "nextleton";

export const myFoo = nextleton("my-foo", () => initializeMyFoo());

Lazy Singleton

import { lazyNextleton } from "nextleton";

export const getMyFoo = lazyNextleton("my-foo", () => initializeMyFoo());

// This can also be reset on demand (useful for tests)

getMyFoo.reset() // this will re-create the singleton

Caveats

If you change anything in your singleton'd code, you will need to restart your dev server for the changes to take effect, or simply change the key of your singleton, then the next hot-reload will re-create the singleton.

FAQs

Last updated on 23 Jun 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc