Going further
Pages without JavaScript
Send public pages as plain HTML and CSS, hydrate the admin, and hydrate single interactive bricks as islands.
A content site rarely needs JavaScript on its public pages. Mortar renders on the server (brick loaders included), so you can send those pages as plain HTML and hydrate only the admin.
createRender from @mortar-kit/core/render does it. Use it as your server entry, and put <ClientScripts /> in your document's head in place of the hydration script and client entry:
// src/entry-server.tsx
import { createRender } from '@mortar-kit/core/render';
import manifest from 'virtual:solid-manifest';
import { Root } from './Root.tsx';
export const render = createRender({ manifest, app: () => <Root /> });
// src/entry-client.tsx
import { hydrate } from '@solidjs/web';
import { Root } from './Root.tsx';
hydrate(() => <Root />, document);
// src/Document.tsx (Root renders it around your App)
import { ClientScripts } from '@mortar-kit/core';
export default function Document(props) {
return (
<html lang="en">
<head>
<meta charset="utf-8" />
<ClientScripts />
</head>
<body>{props.children}</body>
</html>
);
}
hydrate picks the pages that load JavaScript. The default is ['/admin'], so the editor and its preview hydrate. It also takes path prefixes (matched by whole segments, in any case), true, false, or a function of the path. On every other page:
- the render runs under
<NoHydration>withnoScripts, so there are no hydration keys and no hydration data; <ClientScripts>renders nothing, so there's no client entry (unless the page has islands);- the manifest gives stylesheets only, so the browser doesn't preload any JavaScript either;
- the page is always sent as one settled document, since streamed content needs a script.
When the plugin generates your entries, every page hydrates. Authoring the two entries above is what switches that off. createRender puts the request's CSP nonce on the scripts of hydrating pages.
Your own scripts
useRenderedPage() tells a component how the page is sent: hydrate (whether it loads your JavaScript) and nonce (the request's CSP nonce, on every page). Put the nonce on scripts of your own, such as analytics, so the Content-Security-Policy lets them run, and leave out what only works when the page hydrates:
import { useRenderedPage } from '@mortar-kit/core';
function Analytics() {
const page = useRenderedPage();
return <script src="https://stats.example.com/script.js" nonce={page.nonce} defer />;
}
In the browser it says the page hydrates (only hydrating code runs there).
The result: the example site's home page is about 1.3 kB of HTML plus two stylesheets, and no scripts.
Islands
A brick that needs the browser on a public page (a form that answers in place, a gallery, a calculator) says so:
export const contactForm = defineBrick({
name: 'contact-form',
interactive: true,
// schema, action, component as usual
});
The page knows its bricks before it's sent, so nothing else has to change on the page. When a page without JavaScript shows an interactive brick:
- the brick renders in an island: an element around its own
<Hydration>zone, carrying its data, what itsloadreturned and its latest form result as JSON; createRenderadds Solid's hydration bootstrap and your client entry to that page only. Pages without an interactive brick stay script-free;- in the browser, only the islands hydrate. The rest of the page is never touched.
Your client entry decides between the admin and islands. Importing each side lazily keeps the admin out of public pages:
// src/entry-client.tsx
import { hasIslands } from '@mortar-kit/core';
import { hydrate } from '@solidjs/web';
if (hasIslands()) {
void import('./islands.ts').then((islands) => islands.start());
} else {
void import('./Root.tsx').then(({ Root }) => hydrate(() => <Root />, document));
}
// src/islands.ts
import { hydrateIslands, islandActions, lazyBricks } from '@mortar-kit/core';
import { brickActions } from './actions.ts';
import { runBrickAction } from './server/content.ts';
export function start() {
void hydrateIslands({
bricks: lazyBricks(import.meta.glob('./bricks/*.tsx')),
actions: islandActions({ action: brickActions.action, run: runBrickAction }),
});
}
lazyBricks makes each brick its own chunk, so a page downloads only the bricks its islands use. It expects each file to be named after its brick: bricks/contact-form.tsx exports the brick named contact-form. hydrateIslands also takes the plain list of bricks, if you'd rather ship them all in one chunk.
islandActions is what lets an island's <ActionForm> post without the router: it calls your action's server function in the background and hands the brick each pending and result, so only the brick updates (see Handling forms). Without JavaScript, the same form still posts plainly.
In the admin, interactive bricks render like any other brick, since the whole page hydrates there.
Keep in mind:
- An island's data is in the page's HTML. Mark fields only the server needs
private: true(see Fields), and they're left out. The contact form's "Send to" address is one. loadruns on the server only. The island gets its result and doesn't load again in the browser, so return plain JSON.- Do async work in
load, not in the component: an island has no hydration data of its own to wait for.
hydrateIslandsfunction
import { hydrateIslands } from '@mortar-kit/core'
function hydrateIslands(options: HydrateIslandsOptions): Promise<() => void>Hydrates every island on the page: each interactive brick the server
rendered on a page without JavaScript, on its own. Call it from the
client entry on pages that have islands (hasIslands()), instead of
hydrating the whole document. With loaders for bricks, it loads the
bricks the page's islands use first, then hydrates them all at once.
Resolves to a function that disposes them.
Parameters
options: HydrateIslandsOptions
Returns
Promise<() => void>
Example
// src/entry-client.tsx
if (hasIslands()) {
hydrateIslands({
bricks: lazyBricks(import.meta.glob('./bricks/*.tsx')),
actions: islandActions({ action: brickActions.action, run: runBrickAction }),
});
} else {
const { Root } = await import('./Root.tsx');
hydrate(() => <Root />, document);
}
islandActionsfunction
import { islandActions } from '@mortar-kit/core'
function islandActions(options: IslandActionsOptions): BrickActionsBrickActions for islands (interactive bricks on pages without the
router): a form post calls run in the background and only its brick
updates, pending until the result (or error) is back. Pass it to
hydrateIslands.
Parameters
options: IslandActionsOptions
Returns
Example
// src/islands.ts; runBrickAction is the "use server" function brickActions posts to.
hydrateIslands({ bricks, actions: islandActions({ action: brickActions.action, run: runBrickAction }) });
hasIslandsfunction
import { hasIslands } from '@mortar-kit/core'
function hasIslands(root?: ParentNode): booleanTrue when the page (or root) has islands: the client entry's test for calling hydrateIslands.
Parameters
root?: ParentNode
Returns
boolean
lazyBricksfunction
import { lazyBricks } from '@mortar-kit/core'
function lazyBricks(modules: Readonly<Record<string, () => Promise<unknown>>>): Record<string, BrickLoader>Loaders by brick name, from your brick modules as import.meta.glob
gives them (unloaded): each file is named after its brick
(bricks/contact-form.tsx exports the brick named contact-form).
With hydrateIslands, each brick becomes its own chunk, loaded only on
pages that show it.
Parameters
modules: Readonly<Record<string, () => Promise<unknown>>>
Returns
Record<string, BrickLoader>
Example
hydrateIslands({ bricks: lazyBricks(import.meta.glob('./bricks/*.tsx')) });
useRenderedPagefunction
import { useRenderedPage } from '@mortar-kit/core'
function useRenderedPage(): RenderedPageHow the page being rendered is sent: whether it hydrates, and the CSP nonce for its scripts, so the app can put the nonce on its own scripts (analytics, an inline snippet) and leave out what only works with JavaScript. Read it in a component or layout.
On the server it's what createRender decided for the request (outside
createRender: hydrating, with the request's nonce). In the browser it
says the page hydrates, since only hydrating code runs there, with the
nonce the page's scripts carry.
Returns
Example
function Analytics() {
const page = useRenderedPage();
return <script src="https://stats.example.com/script.js" nonce={page.nonce} defer />;
}