项目作者: GrabarzUndPartner

项目描述 :
Helps to load fonts and activate them by preloading.
高级语言: JavaScript
项目地址: git://github.com/GrabarzUndPartner/nuxt-font-loader-strategy.git
创建时间: 2020-02-20T21:47:42Z
项目社区:https://github.com/GrabarzUndPartner/nuxt-font-loader-strategy

开源协议:MIT License

下载


nuxt-font-loader-strategy

:warning: This project is no longer maintained, because the concept is not suitable for global management of many fonts in larger projects. For this reason we have developed a new concept that guarantees smart, efficient and performant component-based font management even in larger websites. Please visit: https://github.com/GrabarzUndPartner/nuxt-speedkit

Grabarz & Partner - Module

Main

npm version
npm downloads
[Renovate - Status][renovate-status-href]
License

Helps to load the fonts and activate them by preloading.

nuxt-font-loader-strategy helps loading the fonts and provides a loading strategy based on preloads.

Define yourself which fonts will be unlocked first.
This gives the best experience in the initial viewport of the website.

Features:

  • Use preload to prevent font flashs.
  • Generates the @font-face definitions automatically and includes them in the layout.
  • Increases the Pagespeed Insight Score 🎉
  • Take the fonts from Minimize critical request depth and load them via WebWorker.
  • Deactivate fonts at low connection. (Show Browser-Support)

⚠️ Configuration of the fonts must be included only in the module settings.

📖 Release Notes

Setup

  1. Add nuxt-font-loader-strategy dependency to your project
  1. yarn add nuxt-font-loader-strategy # or npm install nuxt-font-loader-strategy
  1. Add nuxt-font-loader-strategy to the modules section of nuxt.config.js
  1. {
  2. modules: [
  3. ['nuxt-font-loader-strategy', {
  4. ignoreLighthouse: true,
  5. ignoredEffectiveTypes: ['2g', 'slow-2g'],
  6. fonts: [
  7. // Font
  8. {
  9. fileExtensions: ['woff2', 'woff'],
  10. fontFamily: 'Font A',
  11. fontFaces: [
  12. // Font-Face
  13. {
  14. preload: true,
  15. localSrc: ['Font A', 'FontA-Regular'],
  16. src: '@/assets/fonts/font-a-regular',
  17. fontWeight: 400,
  18. fontStyle: 'normal'
  19. },
  20. // Font-Face
  21. {
  22. localSrc: ['Font A Light', 'FontA-Light'],
  23. src: '@/assets/fonts/font-a-300',
  24. fontWeight: 300,
  25. fontStyle: 'normal'
  26. },
  27. // Font-Face
  28. {
  29. localSrc: ['Font A Light Italic', 'FontA-LightItalic'],
  30. src: '@/assets/fonts/font-a-300Italic',
  31. fontWeight: 300,
  32. fontStyle: 'Italic'
  33. }
  34. ]
  35. },
  36. // Font
  37. {
  38. fileExtensions: ['woff2', 'woff'],
  39. fontFamily: 'Font B',
  40. fontFaces: [
  41. // Font-Face
  42. {
  43. preload: true,
  44. src: '@/assets/fonts/font-b-regular',
  45. fontWeight: 400,
  46. fontStyle: 'normal'
  47. },
  48. // Font-Face
  49. {
  50. src: '@/assets/fonts/font-b-700',
  51. fontWeight: 700,
  52. fontStyle: 'normal'
  53. }
  54. ]
  55. }
  56. ]
  57. }]
  58. ]
  59. }

Options

Property Type Description Default
useWorker Boolean If set, the non-preloads (prefetches) are loaded via WebWorker. false
ignoreLighthouse Boolean If set, the non-preloads (prefetches) in Lighthouse are disabled (ignored). false
classPattern Boolean Font css class pattern. [family]_[weight]_[style]
importPathResolve Function Path resolve for font src: url(fontPath) Replace @/ to ~
requirePathResolve Function Path resolve for require(fontPath) no changes
ignoredEffectiveTypes Array List of excluded connection types. []
fonts Array List of included fonts. []
unlockDelay Number Delay in milliseconds for unlock prefetched fonts. 0
prefetchCount Number Defines how many fonts are prefetched at the same time.
Important: Lower than zero, everything is loaded at once.
2

Maximum expression classPattern

[family]_[variant]_[featureSettings]_[stretch]_[weight]_[style]

WebWorker useWorker

Look for compactability at https://github.com/webpack-contrib/worker-loader.

WebWorker is executed with the setting inline to reduce the script loads.

Font

Property Type Description Default
fileExtensions Array Font-Family Name ['woff2', 'woff']
fontFamily String Font-Family Name ['2g', 'slow-2g']
fontFaces Array Font-Faces []

Font-Face

Property Type Description Default
preload Boolean Specifies whether font is loaded as preload. false
local Array List of local font names (System, etc.). []
src Array File Path without extension. null
fontVariant String CSS-Prop. font-variant 'normal'
fontFeatureSettings String CSS-Prop. font-feature-settings 'normal'
fontStretch String CSS-Prop. font-stretch 'normal'
fontWeight Number CSS-Prop. font-weight 'normal'
fontStyle String CSS-Prop. font-style 'normal'
fontDisplay String CSS-Prop. font-display 'swap'

⚠️ The first fileExtensions entry is used as preload.

Usage

On the HTML tag a class is set for each font file. This class then activates the set styles in the CSS.

The name of the font is specified in SnakeCase. (Example: Open Sans -> open_sans)

It is recommended to normalize the used tags.

Example: h1 has font-weight: bold as standard.

  1. p {
  2. font-family: sans-serif;
  3. }
  4. html.font_open_sans p {
  5. font-family: 'Roboto', sans-serif;
  6. }

For additional FontFaces, classes switch with the options weight and style.

  1. p {
  2. font-family: sans-serif;
  3. }
  4. html.font_roboto_400_normal p.bold {
  5. font-family: 'Roboto', sans-serif;
  6. font-style: normal;
  7. font-weight: 400;
  8. }
  9. p.bold {
  10. font-family: sans-serif;
  11. font-style: normal;
  12. font-weight: 700;
  13. }
  14. html.font_roboto_700_normal p.bold {
  15. font-family: 'Roboto', sans-serif;
  16. }
  17. p.light {
  18. font-family: sans-serif;
  19. font-style: normal;
  20. font-weight: 300;
  21. }
  22. html.font_roboto_300_normal p.light {
  23. font-family: 'Roboto', sans-serif;
  24. }
  25. p.italic {
  26. font-family: sans-serif;
  27. font-style: italic;
  28. font-weight: 400;
  29. }
  30. html.font_roboto_400_italic p.italic {
  31. font-family: 'Roboto', sans-serif;
  32. }

Browser Performance

alt text

Preview

Development

  1. Clone this repository
  2. Install dependencies using yarn install or npm install
  3. Start development server using npm run dev

Browser-Support

Preload Fonts

The options preload and prefetch are required for the link tag.

Not all browsers support this:

If not supported, all fonts are activated.

Deactivate fonts at low connection

Connection speed dependent font loading, requires the support of navigator.connection.effectiveType.

Can I use - effectivetype

License

MIT License

[renovate-status-href]: https://renovate.whitesourcesoftware.com