Missing SSR component after client side navigation (help with icons library SSR)
Hey all,
I'm currently building a port of phosphor icons to solid, based on their react package. I wanted to also add SSR support, but I'm not very experienced in solid start. I was thinking about separate exports, like in their react package, and bundle it with babel-solid preset using ssr true. This kinda works, as I'm not getting any hydration errors, but after changing pages on client side (home -> about -> home) the icon disappears. I'm thinking that maybe in case of Solidjs it would be better to use isServer flag. Since I don't have much experience, wanted to ask for an advise, what would be the best way to export icons with good SSR support. In client side icons I'm using context for global theming of icons (like in react version). If anyone would like to share opinion or tip, I would be thankful! ๐
13 Replies
I'm not sure but maybe I figured it out, it seems that I can do 2 builds for SSR, one with ssr and one with dom. Then in package.json I can export dom version for browser and by default and ssr for node/deno etc. For now it works fine, not sure if this is best approach ๐
You can see how https://github.com/x64Bits/solid-icons did it. There's some special hackery with
ssr
built-in that I didn't know about...I'd suggest building your library on top of this: https://github.com/solidjs-community/solid-lib-starter
When building Solid libraries you usually want to also provide the uncompiled JSX under the
solid
export condition, this lets the end user's bundler take care of the client/server differences. That template will setup the right exports for you automaticallyGitHub
GitHub - solidjs-community/solid-lib-starter: SolidJS library start...
SolidJS library starter template. Use it to create your own solid package. - solidjs-community/solid-lib-starter
Thanks both for the input, the solid-icons has some interesting approach. I've seen it before, but the approach is different than what I wanted to achieve, so I haven't use it like that. I haven't seen the solid lib starter before, it's good to know for the future. I think, that it would be best for me to try to push my changes and share a link to the repo. Maybe someone would be so kind to test it.
But in short, I have 2 different exports for one icon. One is client-side only, that uses context and 2nd is from "<lib name>/ssr" that is not using context and supports ssr.
For the ssr build, I'm building 2 versions from ssr input. 1 with generate for ssr, and one with generate for dom. Resulting in something like this in package.json:
Apparently, this works really well with solid-start. I mean, no hydration errors at least ๐
For ssr, it generates a huge file, with content like this:
For ssr-dom:
For client side bundle, it generates an index with:
And icons like that:
@Brendonovich I need to look at the compiled fiels from the example repo. I wanted to avoid having one export, due to context usage, as in react version. I think that for SSR, it would be bad to start using context on client side, change of icon style. So wanted to be explicit and provide 2 options. Most libraries/examples covers just one export, so I had some troubles figuring it out.
Will play around with it to see how it can work and look like with
solid
exportsSolid doesn't have server components, so I don't think you'll need to worry about context not being available
Also have you seen unplugin-icons ? I use it + autoimport to access all sorts of icons including phosphor, means you don't need a dedicated package for each icon set
GitHub
GitHub - unplugin/unplugin-icons: ๐คน Access thousands of icons as co...
๐คน Access thousands of icons as components on-demand universally. - unplugin/unplugin-icons
Oh... That's cool, I haven't seen it. Thanks!
Anyways, it fun to build the library, and probably will publish it anyways. Even if solid does not have server components. While using ssr rendering or pre-redenring or static rendering or whatever happens there the context is not available in this case I guess, cause I had some errors on solid start about using client side api on server. So I thought that this may cause some issues or at least inconsistency in initial render.
Ah context is available on the server, I think that error you got is just a vite 6 comatability thing
Upgrade vite + vinxi to latest and context during ssr should work fine
๐ฎ Gonna try it, thanks!
Wow, that's really cool. Thanks for the link!
best way to consume third party + custom icons imo
I am impressed it even offers auto-import
I think that the problem with this library may be that it's hard to find it, if you don't know about it. I was searching for solidjs phosphor icons, and I couldn't find this library. Only one outdated community package. Which means that many people may have the same issue.
Also, this is personal opinion, but if I would need a single icon library, I would possibly prefer their official library than this one (based on simplicity and specific instructions, it feels easier, at least for me, if I read phosphor react docs and unplugin icons docs).
On the other hand, I think that if I would start using unplugin icons now, I would probably use it in future in all the cases ๐