How to implement google fonts into shopify hydrogen?

Solved

How to implement google fonts into shopify hydrogen?

princessfiona
Shopify Partner
1 0 0
 
//app/root.tsx
export function links() { return [ { rel: 'preconnect', href: 'http://fonts.googleapis.com', referrerPolicy: 'origin', }, { rel: 'preconnect', href: 'http://fonts.gstatic.com', crossOrigin: 'anonymous', referrerPolicy: 'origin', }, { rel: 'stylesheet', href: 'http://fonts.googleapis.com/css2?family=Quicksand:wght@300..700&display=swap', crossOrigin: 'anonymous', referrerPolicy: 'origin', }, {rel: 'stylesheet', href: tailwindCss}, {rel: 'stylesheet', href: resetStyles}, {rel: 'stylesheet', href: appStyles}, { rel: 'preconnect', href: 'https://cdn.shopify.com', }, { rel: 'preconnect', href: 'https://shop.app', }, {rel: 'icon', type: 'image/svg+xml', href: favicon}, ]; }
 
On the Network tap, it said the status is blocked
Screenshot 2024-12-12 at 4.18.22 PM.png
Screenshot 2024-12-12 at 4.19.10 PM.png

I am trying to fetch google fonts, where I add the google fonts links to function links() and set the Referral Policy to origin, but I am still having trouble fetching the google fonts.

 
Accepted Solution (1)

Weaverse
Shopify Partner
82 27 38

This is an accepted solution.

Hi @princessfiona,

To implement Google Fonts in Shopify Hydrogen, ensure your Content Security Policy (CSP) includes fonts.googleapis.com and fonts.gstatic.com in fontSrc and styleSrc directives. This resolves blocked requests caused by restrictive CSP settings. You can update your CSP using the createContentSecurityPolicy utility. Check Shopify’s documentation for details: CSP Guide.

Alternatively, for better performance and to avoid CSP updates, consider using the Fontsource library to self-host fonts. It eliminates the need to fetch fonts externally and improves load times. See an example in the Pilot Theme.

Helping merchants build super unique, high-performance storefronts using Weaverse + Hydrogen.
Looking for Development & Agency partners.
If you find the answer helpful, give it a thumbs up!
Our App: Theme Customizer for Shopify Hydrogen
Join our Weaverse + Hydrogen community: Weaverse Community

View solution in original post

Reply 1 (1)

Weaverse
Shopify Partner
82 27 38

This is an accepted solution.

Hi @princessfiona,

To implement Google Fonts in Shopify Hydrogen, ensure your Content Security Policy (CSP) includes fonts.googleapis.com and fonts.gstatic.com in fontSrc and styleSrc directives. This resolves blocked requests caused by restrictive CSP settings. You can update your CSP using the createContentSecurityPolicy utility. Check Shopify’s documentation for details: CSP Guide.

Alternatively, for better performance and to avoid CSP updates, consider using the Fontsource library to self-host fonts. It eliminates the need to fetch fonts externally and improves load times. See an example in the Pilot Theme.

Helping merchants build super unique, high-performance storefronts using Weaverse + Hydrogen.
Looking for Development & Agency partners.
If you find the answer helpful, give it a thumbs up!
Our App: Theme Customizer for Shopify Hydrogen
Join our Weaverse + Hydrogen community: Weaverse Community