Skip to content

Commit

Permalink
perf: use recommended Google Fonts workflow
Browse files Browse the repository at this point in the history
Use what is currently the best workflow for importing variable web fonts
from the Google Fonts API:
- https://css-tricks.com/how-to-load-fonts-in-a-way-that-fights-fout-and-makes-lighthouse-happy/
- https://csswizardry.com/2020/05/the-fastest-google-fonts/

Perhaps in the future I'll look into self-hosting fonts, but Google
Fonts is nice because Chrome and other browsers are able to cache the
responses and reuse them across websites.

Related to: vercel/next.js#16065
  • Loading branch information
nicholaschiang committed Mar 28, 2021
1 parent fbbc45b commit 638361d
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 14 deletions.
Binary file not shown.
19 changes: 6 additions & 13 deletions components/page/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ReactNode, useEffect } from 'react';
import { GoogleFonts } from 'next-google-fonts';
import Head from 'next/head';
import cn from 'classnames';

Expand All @@ -7,6 +8,7 @@ import Segment from 'components/segment';

import { PageProps } from 'lib/page';

import segmentSnippet from './segment-snippet';
import styles from './page.module.scss';

export interface PageComponentProps extends PageProps {
Expand Down Expand Up @@ -141,20 +143,11 @@ export default function PageComponent({
content='/favicon/ms-icon-144x144.png'
/>
<meta name='msapplication-navbutton-color' content='#0070f3' />
<link
rel='preconnect'
crossOrigin='anonymous'
href='https://segment.tutorbook.org'
/>
<link
rel='preconnect'
crossOrigin='anonymous'
href='https://track.tutorbook.org'
/>
<script>{`!function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error('Segment snippet included twice.');else{analytics.invoked=!0;analytics.methods=['trackSubmit','trackClick','trackLink','trackForm','pageview','identify','reset','group','track','ready','alias','debug','page','once','off','on','addSourceMiddleware','addIntegrationMiddleware','setAnonymousId','addDestinationMiddleware'];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t<analytics.methods.length;t++){var e=analytics.methods[t];analytics[e]=analytics.factory(e)}analytics.load=function(t,e){var n=document.createElement('script');n.type='text/javascript';n.defer=!0;n.src='https://segment.tutorbook.org/analytics.js/v1/'+t+'/analytics.min.js';var a=document.getElementsByTagName('script')[0];a.parentNode.insertBefore(n,a);analytics._loadOptions=e};analytics.SNIPPET_VERSION='4.1.0';analytics.load('${
process.env.NEXT_PUBLIC_SEGMENT_KEY as string
}');}}();`}</script>
<link rel='preconnect' href='https://segment.tutorbook.org' />
<link rel='preconnect' href='https://track.tutorbook.org' />
<script>{segmentSnippet}</script>
</Head>
<GoogleFonts href='https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap' />
<div
data-cy='page'
className={cn(styles.wrapper, { [styles.borderless]: borderless })}
Expand Down
3 changes: 3 additions & 0 deletions components/page/segment-snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default `!function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error('Segment snippet included twice.');else{analytics.invoked=!0;analytics.methods=['trackSubmit','trackClick','trackLink','trackForm','pageview','identify','reset','group','track','ready','alias','debug','page','once','off','on','addSourceMiddleware','addIntegrationMiddleware','setAnonymousId','addDestinationMiddleware'];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t<analytics.methods.length;t++){var e=analytics.methods[t];analytics[e]=analytics.factory(e)}analytics.load=function(t,e){var n=document.createElement('script');n.type='text/javascript';n.defer=!0;n.src='https://segment.tutorbook.org/analytics.js/v1/'+t+'/analytics.min.js';var a=document.getElementsByTagName('script')[0];a.parentNode.insertBefore(n,a);analytics._loadOptions=e};analytics.SNIPPET_VERSION='4.1.0';analytics.load('${
process.env.NEXT_PUBLIC_SEGMENT_KEY as string
}');}}();`;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
"firebase-functions": "^3.13.0",
"nanoid": "^3.1.12",
"next": "^10.0.9",
"next-google-fonts": "^2.1.0",
"next-translate": "^0.17.1",
"nprogress": "^0.2.0",
"password-generator": "^2.3.2",
Expand Down
1 change: 0 additions & 1 deletion styles/global.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

@use '@rmwc/tooltip/tooltip';

@import url('https://fonts.googleapis.com/css?family=Inter:100,200,300,400,500,600,700,800,900&display=swap');
@import url('https://fonts.googleapis.com/icon?family=Material+Icons');
@import 'social-icons';
@import 'nprogress';
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13603,6 +13603,17 @@ fsevents@~2.3.1:
languageName: node
linkType: hard

"next-google-fonts@npm:^2.1.0":
version: 2.1.0
resolution: "next-google-fonts@npm:2.1.0"
peerDependencies:
next: ">= 10.0.7"
react: ">= 17.0.1"
react-dom: ">= 17.0.1"
checksum: 8beeb146ca2ed63cf3a889a07ec98989d5fd1669db97a3213092a5670980f50401b37cc7a6ba45ca4a60469e2b382ef421ae53c0f7bfdc1aa5654509af561399
languageName: node
linkType: hard

"next-images@npm:^1.6.2":
version: 1.6.2
resolution: "next-images@npm:1.6.2"
Expand Down Expand Up @@ -18452,6 +18463,7 @@ resolve@^1.18.1:
istanbul-lib-coverage: ^3.0.0
nanoid: ^3.1.12
next: ^10.0.9
next-google-fonts: ^2.1.0
next-images: ^1.6.2
next-translate: ^0.17.1
nprogress: ^0.2.0
Expand Down

0 comments on commit 638361d

Please sign in to comment.