{"id":2413,"date":"2023-09-19T09:53:36","date_gmt":"2023-09-19T02:53:36","guid":{"rendered":"https:\/\/dev.artru.io.vn\/?p=2413"},"modified":"2024-01-03T12:14:12","modified_gmt":"2024-01-03T05:14:12","slug":"toi-uu-google-font-bang-javascript","status":"publish","type":"post","link":"https:\/\/artru.net\/en\/toi-uu-google-font-bang-javascript\/","title":{"rendered":"Optimize Google Font with JavaScript"},"content":{"rendered":"<p>A beautiful website also needs a good font set. You can use Google Font to get the Font set you like. However, this more or less affects the loading speed of the website and it also reduces the internal performance score <a href=\"https:\/\/pagespeed.web.dev\/\" target=\"_blank\" rel=\"noreferrer noopener\">PageSpeed Insights<\/a>.<\/p>\n\n\n\n<p>There are many articles on how to optimize Google Font using attributes <code data-no-translation=\"\" data-no-auto-translation=\"\">font-display: swap<\/code>. But it only fixes the &quot;webfont load&quot; error and does not increase performance score.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"458\" height=\"580\" src=\"https:\/\/cdn.artru.eu.org\/wp-content\/uploads\/2023\/09\/Loi-webfont-load.png\" alt=\"Webfont loading error\" class=\"wp-image-2418\"\/><figcaption class=\"wp-element-caption\">Webfont loading error<\/figcaption><\/figure>\n<\/div>\n\n\n<p>In this article, I will show you how to optimize Google Fonts with JavaScript so you can freely choose fonts for your website without worrying about speed anymore.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Select Google Fonts<\/h2>\n\n\n\n<p>To choose Font you need to access <a href=\"https:\/\/fonts.google.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Google Fonts<\/a>.<\/p>\n\n\n\n<p>As my example, the Font &quot;Montserrat&quot; has 2 font sizes &quot;400&quot; and &quot;700&quot;.<\/p>\n\n\n\n<p>You can also choose many other font styles and import them into a single url.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"804\" height=\"600\" src=\"https:\/\/cdn.artru.eu.org\/wp-content\/uploads\/2023\/09\/Google-Font-Montserrat.png\" alt=\"Google Font Montserrat\" class=\"wp-image-2423\"\/><figcaption class=\"wp-element-caption\">Google Font Montserrat<\/figcaption><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">Use JavaScript to optimize Google Font<\/h2>\n\n\n\n<p>These are 2 ways to download Google&#039;s default Fonts.<\/p>\n\n\n\n<pre class=\"wp-block-code\" data-no-translation=\"\" data-no-auto-translation=\"\"><code data-no-translation=\"\" data-no-auto-translation=\"\">&lt;!-- 1. &lt;link&gt; --&gt;\n&lt;link rel=\"preconnect\" href=\"https:\/\/fonts.googleapis.com\"&gt;\n&lt;link rel=\"preconnect\" href=\"https:\/\/fonts.gstatic.com\" crossorigin&gt;\n&lt;link href=\"https:\/\/fonts.googleapis.com\/css2?family=Montserrat:wght@400;700&amp;display=swap\" rel=\"stylesheet\"&gt;\n\n&lt;!-- 2. @import --&gt;\n&lt;style&gt;\n  @import url('https:\/\/fonts.googleapis.com\/css2?family=Montserrat:wght@400;700&amp;display=swap');\n&lt;\/style&gt;<\/code><\/pre>\n\n\n\n<p>Even though Google Font has added properties <code data-no-translation=\"\" data-no-auto-translation=\"\">font-display: swap<\/code>. But when testing performance on PageSpeed Insights, I still get errors related to FCP (First Contentful Paint) and LCP (Largest Contentful Paint).<\/p>\n\n\n\n<p>To fix this problem, I will load Google Font with JavaScript code and place it at the bottom of the page (Footer).<\/p>\n\n\n\n<pre class=\"wp-block-code\" data-no-translation=\"\" data-no-auto-translation=\"\"><code data-no-translation=\"\" data-no-auto-translation=\"\">&lt;script>\n    document.addEventListener(\"DOMContentLoaded\", () => {\n        const script_font = document.createElement('link');\n        script_font.href = 'https:\/\/fonts.googleapis.com\/css2?family=Montserrat:wght@400;700&amp;display=swap';\n        script_font.setAttribute('rel', 'stylesheet');\n        document.body.appendChild(script_font);\n    });\n&lt;\/script><\/code><\/pre>\n\n\n\n<p>At the line <code data-no-translation=\"\" data-no-auto-translation=\"\">script_font.href<\/code> Please get the url from the item <code data-no-translation=\"\" data-no-auto-translation=\"\">@import<\/code> that Google provides.<\/p>\n\n\n\n<p>After changing this way of loading Fonts, check your website speed to see if it has improved.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"504\" src=\"https:\/\/cdn.artru.eu.org\/wp-content\/uploads\/2023\/09\/Toc-do-sau-khi-toi-uu-Google-Font.png\" alt=\"Speed after optimizing Google Font\" class=\"wp-image-2435\"\/><figcaption class=\"wp-element-caption\">Speed after optimizing Google Font<\/figcaption><\/figure>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>M\u1ed9t website \u0111\u1eb9p c\u0169ng c\u1ea7n c\u00f3 m\u1ed9t b\u1ed9 Font \u1ed5n. B\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng Google Font \u0111\u1ec3 c\u00f3 \u0111\u01b0\u1ee3c b\u1ed9 Font \u01b0ng \u00fd. Tuy nhi\u00ean, \u0111i\u1ec1u n\u00e0y \u00edt nhi\u1ec1u [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2438,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-2413","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-website"],"_links":{"self":[{"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/posts\/2413","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/comments?post=2413"}],"version-history":[{"count":0,"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/posts\/2413\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/media\/2438"}],"wp:attachment":[{"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/media?parent=2413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/categories?post=2413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/tags?post=2413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}