|
| 1 | +--- |
| 2 | +title: Installing Unhead with Solid.js |
| 3 | +description: Learn how to start using Unhead with Solid.js. |
| 4 | +navigation: |
| 5 | + title: 'Installation' |
| 6 | +--- |
| 7 | + |
| 8 | +## Introduction |
| 9 | + |
| 10 | +Unhead has first-class support for Solid.js, allowing you to manage your head tags using the `useHead()`{lang="ts"} hook, and other ecosystem hooks. |
| 11 | + |
| 12 | +It can directly replace [`Solid-Meta`](https://docs.solidjs.com/solid-meta), handling a more diverse set of use cases from SEO to structured data. |
| 13 | + |
| 14 | +It's designed to work with any Solid.js setup, however this guide assumes you're following a similar structure to the [Vite: ssr-solid-js-ts](https://github.com/bluwy/create-vite-extra/tree/master/template-ssr-solid-js-ts) template |
| 15 | +or a similar SPA setup. |
| 16 | + |
| 17 | +### Demos |
| 18 | + |
| 19 | +- [StackBlitz - Unhead - Vite + Solid.js SSR](https://stackblitz.com/edit/github-5hqsxyid) |
| 20 | +- [StackBlitz - Unhead - Solid.js SPA](https://stackblitz.com/edit/vitejs-vite-ggqxj5nx) |
| 21 | + |
| 22 | +## Setup |
| 23 | + |
| 24 | +### 1. Add Dependency |
| 25 | + |
| 26 | +Install `@unhead/solid-js`{lang="bash"} dependency to your project. The `next` tag is for v2 of Unhead which is required for Solid.js. |
| 27 | + |
| 28 | +:ModuleInstall{name="@unhead/solid-js@next"} |
| 29 | + |
| 30 | +### 2. Setup Client-Side Rendering |
| 31 | + |
| 32 | +To begin with, we'll import the function to initialize Unhead in our _client_ Solid.js app from `@unhead/solid-js/client`{lang="bash"}. |
| 33 | + |
| 34 | +In Vite this entry file is typically named `entry-client.ts`{lang="bash"}. If you're not server-side rendering, you can add this to your main Solid.js app entry instead. |
| 35 | + |
| 36 | +```tsx {1,7,12,14} [src/entry-client.ts] |
| 37 | +import { createHead, UnheadContext } from '@unhead/solid-js/client' |
| 38 | +import { hydrate } from 'solid-js/web' |
| 39 | +import App from './App' |
| 40 | +/* @refresh reload */ |
| 41 | +import './index.css' |
| 42 | + |
| 43 | +hydrate(() => { |
| 44 | + const head = createHead() |
| 45 | + return (<UnheadContext.Provider value={head}><App /></UnheadContext.Provider>) |
| 46 | +}, document.getElementById('root')) |
| 47 | +``` |
| 48 | + |
| 49 | +### 3. Setup Server-Side Rendering |
| 50 | + |
| 51 | +::note |
| 52 | +Serving your app as an SPA? You can [skip](/docs/solid-js/installation#_4-your-first-tags) this step. |
| 53 | +:: |
| 54 | + |
| 55 | +Setting up server-side rendering is more complicated as it requires rendering out the tags to the HTML string before sending it to the client. |
| 56 | + |
| 57 | +We'll start with setting up the plugin in the _server_ entry this time. Make sure to import from `@unhead/solid-js/server`{lang="bash"} instead |
| 58 | +and add the `head` in the return object. |
| 59 | + |
| 60 | +```tsx {1,7,10,12,15} [src/entry-server.ts] |
| 61 | +import { createHead, UnheadContext } from '@unhead/solid-js/server' |
| 62 | +import { renderToString } from 'solid-js/web' |
| 63 | +import App from './App' |
| 64 | + |
| 65 | +export function render(_url: string) { |
| 66 | + const unhead = createHead() |
| 67 | + const html = renderToString(() => <UnheadContext.Provider value={unhead}><App /></UnheadContext.Provider>) |
| 68 | + return { html, unhead } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +Now we need to render out the head tags _after_ Solid.js has rendered the app. |
| 73 | + |
| 74 | +Within your `server.js` file or wherever you're handling the template logic, you need to transform the template data |
| 75 | +for the head tags using `transformHtmlTemplate()`{lang="ts"}. |
| 76 | + |
| 77 | +```ts {1,9-14} [server.ts] |
| 78 | +import { transformHtmlTemplate } from '@unhead/solid-js/server' |
| 79 | +// ... |
| 80 | + |
| 81 | +// Serve HTML |
| 82 | +app.use('*all', async (req, res) => { |
| 83 | + try { |
| 84 | + // ... |
| 85 | + |
| 86 | + const rendered = await render(url) |
| 87 | + |
| 88 | + const html = await transformHtmlTemplate( |
| 89 | + rendered.unhead, |
| 90 | + template |
| 91 | + .replace(`<!--app-head-->`, generateHydrationScript()) |
| 92 | + .replace(`<!--app-html-->`, rendered.html ?? '') |
| 93 | + ) |
| 94 | + |
| 95 | + res.status(200).set({ 'Content-Type': 'text/html' }).send(html) |
| 96 | + } |
| 97 | + catch (e) { |
| 98 | + // ... |
| 99 | + } |
| 100 | +}) |
| 101 | +// .. |
| 102 | +``` |
| 103 | + |
| 104 | +### 4. Your First Tags |
| 105 | + |
| 106 | +Done! Your app should now be rendering head tags on the server and client. |
| 107 | + |
| 108 | +To improve your apps stability, Unhead will now insert important default tags for you. |
| 109 | + |
| 110 | +- `<meta charset="utf-8">` |
| 111 | +- `<meta name="viewport" content="width=device-width, initial-scale=1">` |
| 112 | +- `<html lang="en">` |
| 113 | + |
| 114 | +You may need to change these for your app requirements, for example you may want to change the default language. Adding |
| 115 | +tags in your server entry means you won't add any weight to your client bundle. |
| 116 | + |
| 117 | +```tsx {2,6-8} [src/entry-server.ts] |
| 118 | +import { createHead, UnheadContext } from '@unhead/solid-js/server' |
| 119 | +import { renderToString } from 'solid-js/web' |
| 120 | +import App from './App' |
| 121 | + |
| 122 | +export function render(_url: string) { |
| 123 | + const unhead = createHead({ |
| 124 | + // change default initial lang |
| 125 | + init: [ |
| 126 | + { |
| 127 | + htmlAttrs: { lang: 'en' }, |
| 128 | + title: 'Default title', |
| 129 | + titleTemplate: '%s - My Site', |
| 130 | + }, |
| 131 | + ] |
| 132 | + }) |
| 133 | + const html = renderToString(() => <UnheadContext.Provider value={unhead}><App /></UnheadContext.Provider>) |
| 134 | + return { html, unhead } |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +For adding tags in your components, you can either use the `useHead()`{lang="ts"} hook. |
| 139 | + |
| 140 | +```tsx [App.tsx] |
| 141 | +import { Head, useHead } from '@unhead/solid-js' |
| 142 | + |
| 143 | +export default function App() { |
| 144 | + // a. use the hook |
| 145 | + useHead({ |
| 146 | + title: 'My Awesome Site', |
| 147 | + meta: [ |
| 148 | + { name: 'description', content: 'My awesome site description' } |
| 149 | + ] |
| 150 | + }) |
| 151 | + // b. use the component |
| 152 | + return ( |
| 153 | + <div> |
| 154 | + <Head> |
| 155 | + <title>My Awesome Site</title> |
| 156 | + <meta name="description" content="My awesome site description" /> |
| 157 | + </Head> |
| 158 | + <h1>Hello World</h1> |
| 159 | + </div> |
| 160 | + ) |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +### 5. Optional: Auto-Imports |
| 165 | + |
| 166 | +If you're using [unplugin-auto-import](https://github.com/antfu/unplugin-auto-import), you can automatically import the composables. |
| 167 | + |
| 168 | +```ts [vite.config.ts] |
| 169 | +import { hookImports } from '@unhead/solid-js' |
| 170 | +import AutoImport from 'unplugin-auto-import/vite' |
| 171 | + |
| 172 | +export default defineConfig({ |
| 173 | + plugins: [ |
| 174 | + AutoImport({ |
| 175 | + imports: [ |
| 176 | + hookImports, |
| 177 | + ], |
| 178 | + }), |
| 179 | + // ... |
| 180 | + ] |
| 181 | +}) |
| 182 | +``` |
| 183 | + |
| 184 | +## Next Steps |
| 185 | + |
| 186 | +Your Solid.js app is now setup for head management, congrats! 🎉 |
| 187 | + |
| 188 | +You can get started with any of the hooks or components: |
| 189 | +- [`useHead()`{lang="ts"}](/docs/api/use-head) |
| 190 | +- [`useSeoMeta()`{lang="ts"}](/docs/api/use-seo-meta) |
| 191 | + |
| 192 | +Or explore some of the optional extras: |
| 193 | + |
| 194 | +- Add [`useSchemaOrg()`{lang="ts"}](/docs/api/use-schema-org) for structured data |
| 195 | +- Use [`useScript()`{lang="ts"}](/docs/scripts/introduction) for performance optimized script loading |
0 commit comments