Guide

Document head

<Head> from idlip puts elements in the document <head>. Holes in them follow the model like any other hole, so a title can show a count that a click changes.

#A title and a meta tag

import { Head } from 'idlip';
import type { JSX } from 'idlip/jsx-runtime';

export default function Inbox(): JSX.Element {
  let unread = 3;

  const title = `Inbox (${unread})`;

  function read() {
    unread = 0;
  }

  return (
    <main>
      <Head>
        <title>{title}</title>
        <meta name="description" content={`${unread} unread messages`} />
      </Head>
      <button on:click={read}>Mark all read</button>
    </main>
  );
}

A click on the button patches the title and the content of the meta tag, as it would patch a hole in the body.

#The head of the document

The root component renders <html>, and its <head> follows the same rules as <Head>. It is the first block of the page:

tsx
export default function Inbox(props: { unread: number }) {
  return (
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <title>Inbox</title>
      </head>
      <body>{/* ... */}</body>
    </html>
  );
}

#Rules

#Several blocks

A <Head> can sit anywhere in the view: in a condition, in a <For> row, in a <Try> or in a child component. Its elements are added when the block appears and removed when it leaves.

When more than one block has a title, the last one in page order wins:

import { Head } from 'idlip';
import type { JSX } from 'idlip/jsx-runtime';

export default function Editor(): JSX.Element {
  let count = 1;
  let editing = false;

  const title = `${count} notes`;

  function add() {
    count = count + 1;
  }
  function toggle() {
    editing = !editing;
  }

  return (
    <main>
      <Head>
        <title>{title}</title>
      </Head>
      <button on:click={add}>Add</button>
      <button on:click={toggle}>Edit</button>
      {editing && (
        <Head>
          <title>Editing</title>
          <meta name="robots" content="noindex" />
        </Head>
      )}
    </main>
  );
}

While editing is true the tab reads Editing. When it turns false, the second block leaves with its meta tag, and the tab reads 1 notes again, or the current count.

Page order is the order in which the server renders the blocks. A <Head> in a child component can rank after a later <Head> of its parent, so its title can win over that one.

#On the server

renderToResponse(), renderToStream() and streamParts() of idlip/server write the elements of every block into the <head> of the document component, so the entry writes nothing:

ts
import { renderToResponse } from 'idlip/server';
import Inbox from './inbox.tsx';

const server = {
  async fetch(): Promise<Response> {
    return renderToResponse(Inbox, { unread: 3 });
  },
};

export default server;

The title update in the browser runs one task after the patch, since the loader watches <head> for changes. A test that reads document.title waits one tick after settled().

See idlip and idlip/server for the signatures.