Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nextleton

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nextleton

  • 0.6.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
283
decreased by-15.77%
Maintainers
1
Weekly downloads
 
Created
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

Package last updated on 23 Jun 2023

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc