Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Duplicate HTML tags for already loaded fonts. #423

Open
SimonHoiberg opened this issue Jan 14, 2020 · 4 comments
Open

Bug: Duplicate HTML tags for already loaded fonts. #423

SimonHoiberg opened this issue Jan 14, 2020 · 4 comments

Comments

@SimonHoiberg
Copy link

I would consider this a bug, otherwise, please consider this a feature request 🙂

Scenario
Say I make a call to request a specific font at 3 different times during runtime.

WebFont.load({
 google: {
   families: ['Montserrat']
 }
});

Desired behavior
After the first load, the two subsequent calls to WebFont.load gets ignored.

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat" media="all">

Actual behavior
3 identical HTML tags and 2 reduntant requests.

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat" media="all">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat" media="all">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat" media="all">

If you work with webfontloader in a context where fonts are loaded conditionally during runtime, it becomes rather inconvenient to make all the checking before requesting a font.
I hope this will be fixed sometimes soon 😊

@kevryan2
Copy link

I don't think a "fix" would be necessary as proposed above, but a config option would be awesome. It should provide the ability to optionally include the urls object if the link to the stylesheet is not already included in the DOM. As it stands anybody facing this seems to have to engage in some form of hacked solution to avoid this...

@AGMETEOR
Copy link

AGMETEOR commented Sep 2, 2020

Anyone with a workaround for this?

@jwerre
Copy link

jwerre commented Mar 30, 2021

I guess you could do something like this:

const link = document.querySelector('link');
const fontUri =  'https://fonts.googleapis.com/css?family=Montserrat';
if (!link || !link.hasAttribute('href') || link.getAttribute('href') !== fontUri ) {
  WebFont.load({
     google: {
       families: ['Montserrat']
     }
   });
}

@mendes-develop
Copy link

mendes-develop commented Apr 2, 2021

If there are other links in the head

const fontURI = 'https://fonts.googleapis.com/css?family=Montserrat';

function checkForLinks() {
  const links = Array.from(document.head.getElementsByTagName('link'));
  if (!links || !links.length) return false;

  return links.find(
    link => link.hasAttribute('href') && link.getAttribute('href') === fontURI
  );
}

const fontLink = checkForLinks();

if (!fontLink) {
  WebFont.load({
    google: {
      families: ['Montserrat'],
    },
  });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants