如何使Sentry与已经存在的异步函数nextConfig一起工作?

ct2axkht  于 2023-04-11  发布在  其他
关注(0)|答案(1)|浏览(84)

所以我有这个nextjs项目,我试图将Sentry添加到其中,但有一个问题,我在next.config.js中的nextConfig变量是一个异步函数,nextjs本身自v12.0.10以来接受为module.export,但哨兵没有您需要将module.export Package 到withSentryConfig中,该withSentryConfig的第一个参数必须是nextConfig,但如果它是这是一个异步函数。
我的next.config.js文件看起来像这样:

const { withSentryConfig } = require('@sentry/nextjs');

const prismic = require('@prismicio/client');

const sm = require('./sm.json');

/** @type {import('next').NextConfig} */
const nextConfig = async () => {
  const client = prismic.createClient(sm.apiEndpoint);

  const repository = await client.getRepository();
  const locales = repository.languages.map(lang => lang.id);

  return {
    reactStrictMode: true,
    swcMinify: true,
    compiler: {
      styledComponents: true,
    },
    i18n: {
      // These are all the locales you want to support in
      // your application
      locales: locales,
      // This is the default locale you want to be used when visiting
      // a non-locale prefixed path e.g. `/hello`
      defaultLocale: locales[0],
      localeDetection: false,
    },
  };
};

module.exports = withSentryConfig(
  nextConfig,
  { silent: true },
  { hideSourcemaps: true },
);

有没有变通办法?
“@sentry/nextjs”:“^7.42.0”
“下一页”:“13.1.2”
react”:“18.2.0”
我试过用多种方法导出它,比如把await放在nextConfig前面并调用它,用.then块代替async await,但似乎都不起作用。

module.exports = withSentryConfig(
  nextConfig(),
  { silent: true },
  { hideSourcemaps: true },
);
module.exports = withSentryConfig(
  await nextConfig(),
  { silent: true },
  { hideSourcemaps: true },
);

如果我把nextConfig转换成一个对象,然后手动输入locales变量,它就可以工作了,所以我知道async函数是这里的问题。

ffdz8vbo

ffdz8vbo1#

尝试创建一个名为buildConfig的异步函数,它返回一个用withSentryConfig Package 的对象。请参阅以下内容...

const buildConfig = async () => {
  const client = prismic.createClient(sm.apiEndpoint);

  const repository = await client.getRepository();
  const locales = repository.languages.map(lang => lang.id);

  return withSentryConfig({
    reactStrictMode: true,
    swcMinify: true,
    compiler: {
      styledComponents: true,
    },
    i18n: {
      // These are all the locales you want to support in
      // your application
      locales: locales,
      // This is the default locale you want to be used when visiting
      // a non-locale prefixed path e.g. `/hello`
      defaultLocale: locales[0],
      localeDetection: false,
    },
  }, {silent: true}, {hideSourceMaps: true});
}

module.exports = buildConfig();

相关问题