• Stars
    star
    127
  • Rank 282,790 (Top 6 %)
  • Language
    TypeScript
  • Created over 3 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Write meta tags to the document head

Solid Meta

Solid Meta npm Version

Asynchronous SSR-ready Document Head management for Solid based on React Head

For Solid 1.0 use 0.27.x or greater. For versions of Solid 0.x use 0.26.x.

Motivation

This module allows you to define document.head tags anywhere in your component hierarchy. The motivations are similar to react-helmet in that you may only have the information for certain tags contextually deep in your component hiearchy. There are no dependencies and it should work fine with asynchronous rendering.

Installation

npm i @solidjs/meta

How it works

  1. You wrap your App with <MetaProvider />
  2. To insert head tags within your app, just render one of <Title />, <Meta />, <Style />, <Link />, and <Base /> components as often as needed.
  3. One the server if you render the <head> element using SolidJS in JSX you are all good. Otherwise use getAssets from solid-js/web to insert the assets where you want.

On the server, the tags are collected, and then on the client the server-generated tags are removed in favor of the client-rendered tags so that SPAs still work as expected (e.g. in cases where subsequent page loads need to change the head tags).

Server setup

Wrap your app with <MetaProvider /> on the server, using a tags[] array to pass down as part of your server-rendered payload. When rendered, the component mutates this array to contain the tags.

import { renderToString, getAssets } from 'solid-js/web';
import { MetaProvider } from '@solidjs/meta';
import App from './App';

// ... within the context of a request ...
const app = renderToString(() =>
  <MetaProvider>
    <App />
  </MetaProvider>
);

res.send(`
  <!doctype html>
  <html>
    <head>
      ${getAssets()}
    </head>
    <body>
      <div id="root">${app}</div>
    </body>
  </html>
`);

Client setup

There is nothing special required on the client, just render one of head tag components whenever you want to inject a tag in the <head />.

import { MetaProvider, Title, Link, Meta } from '@solidjs/meta';

const App = () => (
  <MetaProvider>
    <div class="Home">
      <Title>Title of page</Title>
      <Link rel="canonical" href="http://solidjs.com/" />
      <Meta name="example" content="whatever" />
      // ...
    </div>
  </MetaProvider>
);

More Repositories

1

solid

A declarative, efficient, and flexible JavaScript library for building user interfaces.
TypeScript
31,893
star
2

solid-start

SolidStart, the Solid app framework
TypeScript
5,117
star
3

solid-router

A universal router for Solid inspired by Ember and React Router
TypeScript
1,146
star
4

templates

Vite + solid templates
TypeScript
434
star
5

vite-plugin-solid

A simple integration to run solid-js with vite
TypeScript
433
star
6

solid-styled-components

A 1kb Styled Components library for Solid
TypeScript
283
star
7

solid-realworld

A Solid Implementation of the Realworld Example App
JavaScript
228
star
8

solid-docs

SolidJS Docs.
MDX
214
star
9

solid-testing-library

Simple and complete Solid testing utilities that encourage good testing practices.
TypeScript
195
star
10

solid-playground

Quickly discover what the solid compiler will generate from your JSX template
TypeScript
195
star
11

react-solid-state

Auto tracking state management for modern React
TypeScript
188
star
12

solid-site

Code that powers the SolidJS.com platform.
TypeScript
159
star
13

solid-hackernews

Solid implementation of Hacker News
JavaScript
158
star
14

signals

TypeScript
134
star
15

solid-refresh

TypeScript
84
star
16

solid-workgroup

Workgroup for future Solid Releases
54
star
17

solid-styled-jsx

A Styled JSX wrapper for Solid
JavaScript
47
star
18

solid-jest

Jest preset for SolidJS
JavaScript
34
star
19

create-solid

Set up a modern web app by running one command
JavaScript
32
star
20

solid-todomvc

Solid implementation of TodoMVC
TypeScript
29
star
21

solidhack-submissions

A repository for collecting SolidHack submissions.
JavaScript
20
star
22

solid-repl

A REPL for SolidJS
TypeScript
14
star
23

solid-service-api

Code that powers Solid Service API on Cloudflare Workers.
JavaScript
13
star
24

solidex

Solidex is a list of SolidJS ecosystem resources and packages.
TypeScript
11
star
25

solid-scripts

No configuration CLI tools to bootstrap Solid applications
JavaScript
10
star
26

solid-assets

Access official dynamic and static assets from SolidJS. Made for the community! 🌟
TypeScript
7
star
27

solid-hot-loader

Webpack Loader with Hot Module Reloading for SolidJS
JavaScript
5
star
28

.github

Community health files for the @solidjs organization
3
star
29

solidhack

Website and contest portal for SolidHack.
TypeScript
2
star