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

Better code for loading a font from Google Fonts #469

Open
eveneme opened this issue Mar 5, 2023 · 3 comments
Open

Better code for loading a font from Google Fonts #469

eveneme opened this issue Mar 5, 2023 · 3 comments

Comments

@eveneme
Copy link

eveneme commented Mar 5, 2023

Before:
<link href="https://fonts.googleapis.com/css?family=Inter:300,300i,400,400i,500,700,700i" rel="stylesheet" referrerpolicy="origin">

After:

<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;700&amp;display=swap" rel="stylesheet">
@sudo-do
Copy link
Contributor

sudo-do commented Mar 6, 2023

preconnect is a useful attribute for establishing an early connection with a domain. However, for the specific case of loading fonts from Google Fonts, using prefetch may be a better option. Since fonts have a long cache time and are unlikely to change frequently (I have never seen one updated), using prefetch could help to faster page load times for subsequent page views.

  • But I would like to add that browsers automatically cache frequently accessed resources, including fonts and stylesheets. That means even if you don't use prefetch, the browser will cache font files and load them faster on subsequent page loads!

These are my thoughts. However, as a high school student, I am open to hearing other opinions on this matter.

@adamus007p
Copy link

@eveneme why only fonts.gstatic.com has crossorigin?

@sudo-do
Copy link
Contributor

sudo-do commented Mar 9, 2023

@adamus007p fonts.googleapis.com is used only for retrieving information about the fonts

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

3 participants