我有一个简单的单例命名为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
问题。如果有build
和start
,则只有一个console.log
。