Signleton构造函数在NextJS 13中间件中执行多次



我有一个简单的单例命名为Paths:

export default class Paths {
private static _instance: Paths;
private constructor() {
console.log('paths constructor');
}
public static get Instance() {
console.log(this._instance);
return this._instance || (this._instance = new this());
}
}
和一个简单的NextJS中间件:
import { NextRequest, NextResponse } from 'next/server';
import Paths from './lib/features/paths';
export default async function middleware(req: NextRequest) {
const paths = Paths.Instance;
return NextResponse.next();
}

但是,当我刷新页面时,服务器控制台记录如下:

undefined
paths constructor
undefined
paths constructor
undefined
paths constructor
undefined
paths constructor
undefined
paths constructor
undefined
paths constructor
undefined
paths constructor
undefined
paths constructor
undefined
paths constructor

表示构造函数被执行多次。

原因是什么?如何解决?

[SOLVED]

这是一个next dev问题。如果有buildstart,则只有一个console.log

相关内容

  • 没有找到相关文章

最新更新