Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit 634bdc1

Browse files
authored
Merge pull request #6 from GrabarzUndPartner/feature/ignore-lighthouse
feat(lighthouse): add option to ignore prefetches in lighthouse
2 parents 53d3d24 + 5b5eab8 commit 634bdc1

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# nuxt-font-loader-strategy
22

3+
[![Grabarz & Partner - Module][grabarz-partner-module-src]][grabarz-partner-href]
4+
35
[![Build Status][travis-build-status-src]][travis-build-status-href]
46
[![npm version][npm-version-src]][npm-version-href]
57
[![npm downloads][npm-downloads-src]][npm-downloads-href]
8+
[![Renovate - Status][renovate-status-src]][renovate-status-href]
69
[![License][license-src]][license-href]
710

811
> Helps to load the fonts and activate them by preloading.
@@ -38,6 +41,7 @@ yarn add nuxt-font-loader-strategy # or npm install nuxt-font-loader-strategy
3841
modules: [
3942

4043
['nuxt-font-loader-strategy', {
44+
ignoreLighthouse: true,
4145
ignoredEffectiveTypes: ['2g', 'slow-2g'],
4246
fonts: [
4347
// Font
@@ -99,7 +103,8 @@ yarn add nuxt-font-loader-strategy # or npm install nuxt-font-loader-strategy
99103

100104
| Property | Type | Description | Default |
101105
| ----------------------- | ---------- | ------------------------------------------------------------------------------------------------------------------------- | --------------------------- |
102-
| `useWorker` | `Boolean` | If set, the non-preloads are loaded via WebWorker. | `false` |
106+
| `useWorker` | `Boolean` | If set, the non-preloads (prefetches) are loaded via WebWorker. | `false` |
107+
| `ignoreLighthouse` | `Boolean` | If set, the non-preloads (prefetches) in Lighthouse are disabled (ignored). | `false` |
103108
| `classPattern` | `Boolean` | Font css class pattern. | `[family]_[weight]_[style]` |
104109
| `importPathResolve` | `Function` | Path resolve for font `src: url(fontPath)` | Replace `@/` to `~` |
105110
| `requirePathResolve` | `Function` | Path resolve for `require(fontPath)` | no changes |
@@ -253,6 +258,12 @@ Connection speed dependent font loading, requires the support of `navigator.conn
253258

254259
<!-- Badges -->
255260

261+
[grabarz-partner-module-src]: <https://img.shields.io/badge/Grabarz%20&%20Partner-Module-d19700>
262+
[grabarz-partner-href]: <https://grabarzundpartner.de>
263+
264+
[renovate-status-src]: <https://img.shields.io/badge/renovate-enabled-brightgreen>
265+
[renovate-status-href]: <https://renovate.whitesourcesoftware.com/>
266+
256267
[travis-build-status-src]: <https://travis-ci.org/GrabarzUndPartner/nuxt-font-loader-strategy.svg?branch=master>
257268
[travis-build-status-href]: <https://travis-ci.org/GrabarzUndPartner/nuxt-font-loader-strategy>
258269

example/nuxt.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ module.exports = {
5454
modules: [
5555
[
5656
resolve(__dirname, '..'), {
57+
ignoreLighthouse: true,
5758
prefetchCount: 2,
5859
fonts: [
5960
{

lib/module.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ module.exports = async function (moduleOptions) {
1616
/** If set, the non-preloads are loaded via WebWorker. */
1717
useWorker: false,
1818

19+
/** If set, the non-preloads (prefetches) in Lighthouse are disabled (ignored). */
20+
ignoreLighthouse: false,
21+
1922
/**
2023
* List of excluded connection types.
2124
*/
@@ -100,6 +103,7 @@ function addPlugins (moduleScope, fonts, options) {
100103
fontFaceCSS: PATH_FONT_FACE_CSS,
101104
loadFontsOptions: {
102105
ignoredEffectiveTypes: options.ignoredEffectiveTypes,
106+
ignoreLighthouse: options.ignoreLighthouse,
103107
unlockDelay: options.unlockDelay,
104108
prefetchCount: options.prefetchCount
105109
}

lib/utils/fontLoader.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
export function loadFonts (options) {
22
options = Object.assign({
33
ignoredEffectiveTypes: [],
4+
ignoreLighthouse: false,
45
unlockDelay: 0,
56
prefetchCount: 2
67
}, options)
78

89
if (isSlowConnection(options.ignoredEffectiveTypes)) { return }
10+
if (options.ignoreLighthouse && isLighthouse()) { return }
911

1012
if (linkFeaturePreload() && linkFeaturePrefetch()) {
1113
const preloads = document.querySelectorAll('link[rel=\'delay-prefetch\']')
@@ -67,6 +69,14 @@ export function isSlowConnection (ignoredEffectiveTypes) {
6769
return navigator.connection && (ignoredEffectiveTypes.find(type => navigator.connection.effectiveType === type))
6870
}
6971

72+
/**
73+
* Checks if user-agent on Google Pagespeed (Lighthouse)
74+
* @return Boolean
75+
*/
76+
export function isLighthouse () {
77+
return new RegExp('(Speed Insights)|(Chrome-Lighthouse)').test(window.navigator.userAgent)
78+
}
79+
7080
export function linkFeaturePrefetch () {
7181
const link = document.createElement('link')
7282
return link.relList && link.relList.supports('prefetch')

0 commit comments

Comments
 (0)