From 78c52b0cb114dfb6e59943eb3bf40614d7ac1378 Mon Sep 17 00:00:00 2001 From: Julien Calixte Date: Sat, 6 Apr 2024 15:40:32 +0200 Subject: [PATCH] =?UTF-8?q?perf:=20=E2=9A=A1=EF=B8=8F=20google=20font?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit only create link if the link doesn't exist --- src/utils/downloadGoogleFont.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/utils/downloadGoogleFont.ts b/src/utils/downloadGoogleFont.ts index 5a899a7..119aad0 100644 --- a/src/utils/downloadGoogleFont.ts +++ b/src/utils/downloadGoogleFont.ts @@ -3,11 +3,18 @@ import FontFaceObserver from 'fontfaceobserver' export const downloadGoogleFont = async (font: string): Promise => { const href = assembleFontLink(font) - const link = document.createElement('link') - link.href = href - link.rel = 'stylesheet' + // check if the href already exists + const existingLink = document.querySelector(`link[href="${href}"]`) - document.head.appendChild(link) + console.log({ existingLink }) + + if (!existingLink) { + const link = document.createElement('link') + link.href = href + link.rel = 'stylesheet' + + document.head.appendChild(link) + } await new FontFaceObserver(font).load()