Skip to content

Latest commit

 

History

History

seo

README.md

@rootnative/seo

What a search engine and a link preview read from a React Native web app: the document head, the structured data, the heading outline, the image text, the HTML shell, the sitemap, and the robots file.

The package holds the parts that don't depend on your data. Your app keeps the mapping from its data to the meta. The same package serves an Expo Router static export, a Vite and react-native-web build, and an Electron renderer, because the core has no dependency on React Native or on a router.

Install

pnpm add @rootnative/seo
Entry Needs What it holds
@rootnative/seo nothing defineSite, toHeadTags, toMetadata, headTagsHtml, clipDescription, isoDuration, the types
@rootnative/seo/schema nothing JSON-LD builders: webSite, breadcrumbList, organization, article, product, movie, person, event, faqPage
@rootnative/seo/react react, react-native SeoProvider, Heading, imageLabel, useBrowserThemeColor
@rootnative/seo/expo-router expo-router PageHead, RouteHead, shellTags
@rootnative/seo/expo-image expo-image Picture: a plain <img> on the web, expo-image on native
@rootnative/seo/dom react, react-native PageHead for an app with no Expo Router: writes the same tags to document.head
@rootnative/seo/node Node writeSeoFiles, auditExport, browseExport, serveExport, the rootnative-seo command, readExport, writeSitemap, writeRobots, writeManifest

Quick start (Expo Router)

// src/site.ts
import { defineSite } from '@rootnative/seo'

export const site = defineSite({
  name: 'Trek',
  url: 'https://example.com/app',
})
// app/_layout.tsx
import { SeoProvider } from '@rootnative/seo/react'
import { Stack } from 'expo-router'
import { site } from '../src/site'

export default function RootLayout() {
  return (
    <SeoProvider site={site}>
      <Stack />
    </SeoProvider>
  )
}
// app/movies/[id].tsx
import { PageHead } from '@rootnative/seo/expo-router'
import { movie } from '@rootnative/seo/schema'

<PageHead
  title={data.title}
  description={data.overview}
  url={`/movies/${data.id}`}
  image={data.posterUrl}
  type="video.movie"
  jsonLd={[movie({ name: data.title, url: `https://example.com/app/movies/${data.id}` })]}
/>

A screen with no PageHead gets its head from RouteHead, after the navigator in the root layout. It takes a function from the pathname to the page meta, and a PageHead on the route wins the whole page:

<SeoProvider site={site}>
  <Stack />
  <RouteHead defaults={(pathname) => findPage(pathname)} />
</SeoProvider>

Expo SDK 58 and later. The static export writes the head from the generateMetadata export of the route, not from PageHead or RouteHead. Export it from each route with the same meta, and keep the PageHead, which writes the JSON-LD into the page body and the head after a client navigation:

// app/movies/[id].tsx
import { toMetadata } from '@rootnative/seo'

export function generateMetadata(_request: unknown, params: { id: string }) {
  return toMetadata(site, movieMeta(params.id))
}

A custom app/+html.tsx on SDK 58 must render headNodes and bodyNodes from useServerDocumentContext() in expo-router/html, or the page has no head tags and no JavaScript.

Set web.output to static in app.json, and the head of each page is in its HTML file at export time. server output works too, and the commands below write the files into dist/client. On Expo SDK 58 a server export has no page file, because the server renders each page at request time. Run rootnative-seo render dist first: it starts expo serve, writes each page to dist/rendered, and the other commands read the pages there. The server export does not run generateStaticParams, so pass the entries of each dynamic route with --params <file>. On SDK 57 and earlier the pages are in dist/server already.

After the export, write the sitemap and robots.txt from the exported pages:

expo export --platform web && rootnative-seo dist

A page goes in the sitemap with its canonical link, when that link is under the site and the page has no noindex. The command prints each page it leaves out, with the reason. It also sets lang on <html> of each page whose own language, from its hreflang link to itself or else its og:locale, is not the language of the shell: Expo Router writes the <html> of app/+html.tsx on every page, so without this the German version of an English site says lang="en". PageHead sets the same lang after hydration. --no-lang skips the step. A server export renders each page at request time, so it keeps the lang of the shell until hydration.

Then check the export, in CI. rootnative-seo audit dist reads every page and fails on one of these:

  • a missing title
  • a page with no canonical link and no noindex
  • not exactly one <h1> or <main>
  • no lang on <html>, or a lang that is not the language of the page
  • an <img> with no alt
  • hreflang links that the other language versions don't match
  • a sitemap that disagrees with the pages

rootnative-seo browse dist loads every page in headless Chrome and fails on an uncaught exception, a console.error, or a failed request. That is where a hydration mismatch shows.

The rules the head follows

  • The tab title uses the site template. The Open Graph title does not, because the card shows the site name on its own line.
  • A page with no url gets no canonical link and no share card. That is the correct result for a noindex overlay.
  • alternates writes one hreflang link per language version, and an x-default from the first entry when the list has none. Pass the same list to each version, this page included.
  • og:image:width and og:image:height are written only with an image.
  • A JSON-LD builder leaves an empty field out. It never writes "", null, or [].
  • The JSON-LD text escapes <, so a </script> in a title cannot end the tag.

Docs

License

MIT