Skip to main content

Built and signed on GitHub Actions

🌊 Lightweight HTML templating library for any-side rendering

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
4 days ago (0.7.6)
class Server

Server side renderer.

See mizu.sh documentation for more details.

Constructors

new
Server(options?: ServerOptions)

Server constructor.

Properties

writeonly
context: Record<PropertyKey, unknown>
readonly
context: Record<PropertyKey, any>

Default rendering context.

All properties assigned to this object are accessible during rendering.

Methods

Generate static files from various sources.

Options:

  • output: Specify the path to the output directory.
  • clean: Empty the output directory before generating files.

Supported sources:

  • StringSource: Generate content from raw strings.
  • GlobSource: Generate content from local files matching the provided glob patterns.
  • CallbackSource: Generate content from callback returns.
  • URLSource: Generate content from fetched URLs.

Each source can be templated using mizu rendering by passing a render option.

const mizu = new Server({ directives: ["@mizu/test"], generate: { output: "/fake/output" } })
await mizu.generate(
  [
    // Copy content from strings
    [ "<p>foo</p>", "string.html" ],
    [ "<p ~test.text='foo'></p>", "string_render.html", { render: { context: { foo: "bar" } } } ],
    // Copy content from local files
    [ "**\/*", "public", { directory: "/fake/static" } ],
    [ "*.html", "public", { directory: "/fake/partials", render: { context: { foo: "bar "} } } ],
    // Copy content from callback return
    [ () => JSON.stringify({ foo: "bar" }), "callback.json" ],
    [ () => `<p ~test.text="'foo'"></p>`, "callback.html", { render: { context: { foo: "bar" } } } ],
    // Copy content from URL
    [ new URL(`data:text/html,<p>foobar</p>`), "url.html" ],
    [ new URL(`data:text/html,<p ~test.text="foo"></p>`), "url_render.html", { render: { context: { foo: "bar" } } } ],
  ],
  // No-op: do not actually write files and directories
  { fs: { readdir: () => Promise.resolve([] as string[]), mkdir: () => null as any, write: () => null as any } },
)
render(
content: string | Arg<Renderer["render"]>,
options?: ServerRenderOptions & Pick<ServerOptions, "warn">,
): Promise<string>

Parse an HTML string and render all subtrees.

The *mizu attribute is only required if implicit is set to false.

const mizu = new Server({ context: { foo: "bar" } })
await mizu.render(`<html><body><a ~test.text="foo"></a></body></html>`)

Static Properties

readonly
default: Server

Default Server instance.

Default options for Server.

These default options are merged with the provided options when creating a new Server instance.

Add Package

deno add jsr:@mizu/render

Import symbol

import { Server } from "@mizu/render/server";

---- OR ----

Import directly with a jsr specifier

import { Server } from "jsr:@mizu/render/server";

Add Package

npx jsr add @mizu/render

Import symbol

import { Server } from "@mizu/render/server";

Add Package

yarn dlx jsr add @mizu/render

Import symbol

import { Server } from "@mizu/render/server";

Add Package

pnpm dlx jsr add @mizu/render

Import symbol

import { Server } from "@mizu/render/server";

Add Package

bunx jsr add @mizu/render

Import symbol

import { Server } from "@mizu/render/server";