Lighthouse自定义响应对象

时间:2019-03-15 11:44:48

标签: lighthouse

有什么方法可以指定应在lighthouse响应对象中添加哪些属性?

例如,我要排除lhr.i18nartifacts属性。这是灯塔对象的当前结构(更有可能是除这些属性之外的其他属性):

{
  "lhr": {
    "userAgent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/72.0.3626.121 Safari/537.36",
    "environment": {
      "networkUserAgent": "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3694.0 Mobile Safari/537.36 Chrome-Lighthouse",
      "hostUserAgent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/72.0.3626.121 Safari/537.36",
      "benchmarkIndex": 424
    },
    "lighthouseVersion": "4.2.0",
    "fetchTime": "2019-03-14T14:30:25.256Z",
    "requestedUrl": "https://www.realtor.com/",
    "finalUrl": "https://www.realtor.com/",
    "runWarnings": [],
    "audits": { ... },
    "i18n": { ... },
  }
  "artifacts": { ... }
} 

我尝试使用自定义配置来实现这一目标:

const UIStrings = {
  performanceCategoryTitle: 'Performance',
  metricGroupTitle: 'Metrics',
  loadOpportunitiesGroupTitle: 'Opportunities',
  loadOpportunitiesGroupDescription: 'These optimizations can speed up your page load.',
  diagnosticsGroupTitle: 'Diagnostics',
  diagnosticsGroupDescription: 'More information about the performance of your application.',
};

/** @type {LH.Config.Json} */
const customLighthouseConfig = {
  passes: [{
    passName: 'defaultPass',
    recordTrace: true,
    useThrottling: true,
    pauseAfterLoadMs: 1000,
    networkQuietThresholdMs: 1000,
    cpuQuietThresholdMs: 1000,
    gatherers: [
      'scripts',
      'css-usage',
      'viewport-dimensions',
      'runtime-exceptions',
      'chrome-console-messages',
      'accessibility',
      'anchor-elements',
      'image-elements',
      'link-elements',
      'meta-elements',
      'dobetterweb/appcache',
      'dobetterweb/doctype',
      'dobetterweb/domstats',
      'dobetterweb/js-libraries',
      'dobetterweb/optimized-images',
      'dobetterweb/password-inputs-with-prevented-paste',
      'dobetterweb/response-compression',
      'dobetterweb/tags-blocking-first-paint',
    ],
  },
  {
    passName: 'offlinePass',
    gatherers: [
      'service-worker',
      'offline',
      'start-url',
    ],
  },
  {
    passName: 'redirectPass',
    // Speed up the redirect pass by blocking stylesheets, fonts, and images
    blockedUrlPatterns: ['*.css', '*.jpg', '*.jpeg', '*.png', '*.gif', '*.svg', '*.ttf', '*.woff', '*.woff2'],
    gatherers: [
      'http-redirect',
      'html-without-javascript',
    ],
  }],
  audits: [
    'metrics/first-contentful-paint',
    'metrics/first-meaningful-paint',
    'metrics/speed-index',
    'metrics/estimated-input-latency',
    'time-to-first-byte',
    'metrics/first-cpu-idle',
    'metrics/interactive',
    'mainthread-work-breakdown',
    'bootup-time',
    'uses-rel-preload',
    'uses-rel-preconnect',
    'metrics',
    'byte-efficiency/uses-text-compression',
    'byte-efficiency/uses-long-cache-ttl',
    'byte-efficiency/total-byte-weight',
    'byte-efficiency/render-blocking-resources',
    'byte-efficiency/unminified-javascript',
  ],

  groups: {
    'metrics': {
      title: UIStrings.metricGroupTitle,
    },
    'load-opportunities': {
      title: UIStrings.loadOpportunitiesGroupTitle,
      description: UIStrings.loadOpportunitiesGroupDescription,
    },
    'diagnostics': {
      title: UIStrings.diagnosticsGroupTitle,
      description: UIStrings.diagnosticsGroupDescription,
    },
  },
  categories: {
    'performance': {
      title: UIStrings.performanceCategoryTitle,
      auditRefs: [
        {id: 'first-contentful-paint', weight: 3, group: 'metrics'},
        {id: 'first-meaningful-paint', weight: 1, group: 'metrics'},
        {id: 'speed-index', weight: 4, group: 'metrics'},
        {id: 'interactive', weight: 5, group: 'metrics'},
        {id: 'first-cpu-idle', weight: 2, group: 'metrics'},
        {id: 'estimated-input-latency', weight: 0, group: 'metrics'},
        {id: 'unminified-javascript', weight: 0, group: 'load-opportunities'},
        {id: 'uses-text-compression', weight: 0, group: 'load-opportunities'},
        {id: 'uses-rel-preconnect', weight: 0, group: 'load-opportunities'},
        {id: 'uses-rel-preload', weight: 0, group: 'load-opportunities'},
        {id: 'total-byte-weight', weight: 0, group: 'diagnostics'},
        {id: 'bootup-time', weight: 0, group: 'diagnostics'},
        {id: 'mainthread-work-breakdown', weight: 0, group: 'diagnostics'},
        {id: 'render-blocking-resources', weight: 0, group: 'load-opportunities'},
        {id: 'uses-long-cache-ttl', weight: 0, group: 'diagnostics'},
      ],
    }
  }
};

0 个答案:

没有答案