In the ReactQL file structure, is there a recommended place to store a custom font, and how would you reference that font?
For example, I've tried adding the font into ./static, and referencing it in styles.global.css, but no such luck.
Any recommendations?
You can reference fonts (or images) in three ways:
1. From within your JS code
You can import a font/img just like any other code, using ES6 module syntax:
In this case,
someFont
will equal a string of the font file path that's processed by Webpack, and dumped in the resultingdist
folder. That's done for you automatically; you don't need to put the fonts or images anywhere special.You can then reference this in your React components wherever you'd normally need the full file path; the string will be publicly accessible from the ReactQL web server:
In this case,
logoImage
might wind up as/assets/img/logo.d41d8cd98f00b204e980.png
after Webpack has processed it and dumped the constant in the resulting Javascript bundle.By convention, I'd recommend you keep static assets in the same folder as the calling React component. If you have multiple components pointing to the same assets, just choose a sensible name - like
src/fonts/
orsrc/images
, or even just/images
.2. From within your CSS
Inside any
.css|sass|scss|less|
or*.global.
versions of the same, you can import using the regular@import
syntax:styles.global.css
To ensure the font is resolved correctly, import relative to the CSS file. In this case,
Starjout.ttf
resides in the same folder asstyles.global.css
.3. Inside
/static
Anything you put in
/static
will be placed in the resultingdist/public
folder as-is. It won't be processed by Webpack- this means, images won't be compressed in production and no.gz
or.br
versions will be created. They'll simply be copied over.To reference the file, you can then use the full URL inside your CSS. If you use a relative path, Webpack will expect to find the file relative to the source CSS and it won't work. Likewise, putting things in
/static
means the file will be copied over in its original form, so importing via JS won't work properly either.The basic rule is: Any static file you want to consider 'source', put in
src/
or anywhere else in your project route. Anything you want to copy without processing or being able to import dynamically, put in/static