Skip to content

Commit

Permalink
refactor(SLB-222): preload fonts by directory
Browse files Browse the repository at this point in the history
  • Loading branch information
pmelab committed Apr 19, 2024
1 parent 6de4958 commit 9cdaf73
Show file tree
Hide file tree
Showing 14 changed files with 240 additions and 53 deletions.
25 changes: 13 additions & 12 deletions apps/website/gatsby-ssr.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Locale } from '@custom/schema';
import { loadFonts } from '@custom/ui/fonts-async';
import { fonts } from '@custom/ui/fonts';
import { GatsbySSR } from 'gatsby';
import React from 'react';

Expand All @@ -26,15 +26,16 @@ export const onRenderBody: GatsbySSR['onRenderBody'] = ({
}
}

setHeadComponents([
// here you will map config file consumed
<link
rel="preload"
href="/fonts/NotoSansGurmukhi-Regular.woff2"
as="font"
type="font/woff2"
crossOrigin="anonymous"
key="NotoSansGurmukhi"
/>,
]);
fonts.forEach((font) => {
setHeadComponents([
<link
rel="preload"
href={font}
as="font"
type="font/woff2"
crossOrigin="anonymous"
key="NotoSansGurmukhi"
/>,
]);
});
};
2 changes: 1 addition & 1 deletion packages/schema/src/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ type Hero {
}

union PageContent @resolveEditorBlockType =
BlockMarkup
| BlockMarkup
| BlockMedia
| BlockForm
| BlockImageTeasers
Expand Down
1 change: 1 addition & 0 deletions packages/ui/fonts.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const fonts: string[];
7 changes: 7 additions & 0 deletions packages/ui/fonts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { readdirSync } from 'fs';

export const fonts = readdirSync('./static/public/fonts/preload').map(
(font) => {
return `/fonts/preload/${font}`;
},
);
8 changes: 5 additions & 3 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@
"./styles.css": [
"./build/styles.css"
],
"./fonts-async": [
"./build/fonts-async.js"
"./fonts": [
"./fonts.js"
]
},
"typesVersions": {
"*": {
"routes/*": [
"build/components/Routes/*.d.ts"
],
"fonts": [
"fonts.d.ts"
]
}
},
Expand Down Expand Up @@ -96,7 +99,6 @@
"nyc": "^15.1.0",
"postcss": "^8.4.32",
"postcss-cli": "^11.0.0",
"postcss-font-magician": "^4.0.0",
"postcss-import": "^16.0.0",
"postcss-import-ext-glob": "^2.1.1",
"postcss-prefix-selector": "^1.16.0",
Expand Down
35 changes: 35 additions & 0 deletions packages/ui/src/components/Atoms/Fonts.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
@font-face {
font-family: 'Noto Sans';
font-style: normal;
font-weight: 300;
src: url('/fonts/preload/NotoSansGurmukhi-Light.woff2') format('woff2');
}

@font-face {
font-family: 'Noto Sans';
font-style: normal;
font-weight: 400;
src: url('/fonts/preload/NotoSansGurmukhi-Regular.woff2') format('woff2');
}

@font-face {
font-family: 'Noto Sans';
font-style: normal;
font-weight: 500;
src: url('/fonts/preload/NotoSansGurmukhi-Medium.woff2') format('woff2');
}

@font-face {
font-family: 'Noto Sans';
font-style: normal;
font-weight: 600;
src: url('/fonts/preload/NotoSansGurmukhi-SemiBold.woff2') format('woff2');
}

@font-face {
font-family: 'Noto Sans';
font-style: normal;
font-weight: 700;
src: url('/fonts/preload/NotoSansGurmukhi-Bold.woff2') format('woff2');
}

html, body {
font-family: 'Noto Sans', 'Helvetica', sans-serif;
}
4 changes: 0 additions & 4 deletions packages/ui/src/postcss.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,5 @@ module.exports = {
'.gutenberg__editor .edit-post-visual-editor__content-area',
}[process.env.PREFIX] || '',
},
'postcss-font-magician': {
hosted: ['../static/public/fonts'],
// async: '../static/public/fonts-async.js',
}
},
};
7 changes: 0 additions & 7 deletions packages/ui/static/public/fonts-async.js

This file was deleted.

Loading

0 comments on commit 9cdaf73

Please sign in to comment.