jsbisht / nextjs-middleware-singleton

Test why singleton is getting reset in nextjs middleware but not page api routes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

I have the following utility function that is just a singleton class for storing a counter.

export class CounterSingleton {
  private static instance: CounterSingleton;
  private count: number;

  private constructor() {
    this.count = 0;
  }

  public static getInstance(): CounterSingleton {
    if (!CounterSingleton.instance) {
      CounterSingleton.instance = new CounterSingleton();
    }
    return CounterSingleton.instance;
  }

  public increment(): void {
    this.count++;
  }

  public getCount(): number {
    return this.count;
  }
}

I want the counter class instance to be accessible within api routes. I have no problem with this.

// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import { CounterSingleton } from "@/utility/counter";
import type { NextApiRequest, NextApiResponse } from "next";

type Data = {
  counter: number;
};

export default function handler(
  req: NextApiRequest,
  res: NextApiResponse<Data>
) {
  const counter = CounterSingleton.getInstance();
  counter.increment();
  res.status(200).json({ counter: counter.getCount() });
}

First api call: { counter: 1 } Second api call: { counter: 2 }

When i access the same counter class within the middleware. It always gives me counter as 0.

import { NextResponse } from "next/server";
import { CounterSingleton } from "./utility/counter";

export function middleware() {
  const counter = CounterSingleton.getInstance();
  console.log(counter.getCount());
  return NextResponse.next();
}

About

Test why singleton is getting reset in nextjs middleware but not page api routes


Languages

Language:CSS 54.1%Language:TypeScript 44.8%Language:JavaScript 1.0%