How to deal with mapping <link> elements in <Head> ? #16652
-
I have a Layout.tsx component and I want to preload fonts depends or what page is. I want to import font list from array and insert the HTML code into tags. My fonts are located in /public/fonts/ for now, later I want to export them on CDN but now it's not really important. This is my actual code and it's works but...:
As you can see it's start being a bit messy when i put this tag manually. I wanna do something like that:
Or maybe pass the array to props with fonts that I want to use on different page/component. Problem: Thanks guys and I just started my adventure with Next.js :) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
for a start, your font Name has space. |
Beta Was this translation helpful? Give feedback.
-
From the docs:
Now, it might be the case that a single I am also curious how many different fonts do you plan to import on the entire site? Maybe it is even more cost efficient to just preload all of them in a custom _document instead of importing in each page? |
Beta Was this translation helpful? Give feedback.
From the docs:
Now, it might be the case that a single
.map
might just work, try adding<></>
around the returned<link>
tags, as JSX expects only one element in returns. If it still does not work, you probably need to define each link as a direct child in Head. If it's only 3 fonts, it's not that bad either. Remember, sometimes readability outweighs "optimal". I mean, does a single map really help that much to clean up the code? Correct me, …