如何关闭NextJS中Sanity fetch()产生的冗余控制台日志?
关闭Next.js中Sanity fetch产生的冗余日志
这些冗余日志是Next.js默认记录的fetch请求日志,由于next-sanity的客户端在Next.js环境中会复用框架内置的fetch实现,因此会触发这类日志输出。以下是几种关闭方式:
1. 针对单个Sanity fetch请求禁用日志
在调用fetch方法时,通过第三个参数传入Next.js的fetch配置,添加log: false即可关闭当前请求的日志:
export async function getPeople(): Promise<Person[]> { return createClient(clientConfig).fetch( groq`*[_type == "person"] | order(priority asc, lastName asc){ ... }`, {}, // 查询参数(无参数时留空对象) { next: { log: false } } // 禁用当前请求的日志 ); }
2. 全局禁用所有fetch请求日志
如果需要全局关闭这类日志,修改next.config.js(或next.config.ts)添加配置:
/** @type {import('next').NextConfig} */ const nextConfig = { experimental: { logging: { fetches: 'none', }, }, }; module.exports = nextConfig;
该配置会完全关闭Next.js对所有fetch请求的日志记录。
3. 仅在生产环境关闭日志
若希望保留开发环境的日志以便调试,仅在生产环境禁用,可通过环境变量判断配置:
/** @type {import('next').NextConfig} */ const nextConfig = {}; if (process.env.NODE_ENV === 'production') { nextConfig.experimental = { logging: { fetches: 'none', }, }; } module.exports = nextConfig;
内容的提问来源于stack exchange,提问作者lukasmaxim
相关产品推荐
相关产品推荐

