Site color

Choose your site’s color palette.

Saved on this device.

PageScript

PageScriptDOM enhancement

Include a page-specific browser entry point with SSG path resolution, bundling and native script attributes.

Browser entry points

Enhance existing content

Move the reading-speed slider to recalculate the estimate. This plain DOM script enhances existing server-rendered content; the paragraph and labelled input remain available before the script runs.

PageScript in contextCOMPOSITION

PureStack brings content, components and theme styles together. A small browser entry point can enhance existing HTML when a full component app is unnecessary.

Reading-time estimate appears when the module runs.

Existing HTML gains a scoped event binding and a live calculation. The source tab shows both parts.

<section data-reading-example aria-label="Reading-time example">
  <p data-reading-content>PureStack brings content, components and theme styles together. A small browser entry point can enhance existing HTML when a full component app is unnecessary.</p>
  <label for="reading-rate">Reading speed · words per minute</label>
  <input id="reading-rate" data-reading-rate type="range" min="10" max="300" step="10" value="200" class="w-full"/>
  <output id="reading-time-result" data-reading-result aria-live="polite">Reading-time estimate appears when the module runs.</output>
</section>
<PageScript id="reading-time-entry" src="./reading-time.ts"/>
// Query each enhancement root; do not bind one global input to several widgets.
for (const region of document.querySelectorAll<HTMLElement>(
  '[data-reading-example]',
)) {
  const text = region.querySelector<HTMLElement>('[data-reading-content]')
  const output = region.querySelector<HTMLOutputElement>(
    '[data-reading-result]',
  )
  const rate = region.querySelector<HTMLInputElement>('[data-reading-rate]')
  if (!text || !output || !rate) continue
  const update = () => {
    const words = (text.textContent ?? '')
      .trim()
      .split(/\s+/)
      .filter(Boolean).length
    const wordsPerMinute = Math.max(1, Number(rate.value) || 200)
    output.textContent = `${words} words · about ${Math.max(1, Math.ceil(words / wordsPerMinute))} minute${Math.ceil(words / wordsPerMinute) > 1 ? 's' : ''} to read`
  }
  rate.addEventListener('input', update)
  update()
}

See the emitted script

The receipt below reads the actual script element in the browser. Its src is the build’s emitted JavaScript URL, and its default destination is body.

Source path → emitted URL → native tagCOMPOSITION
Inspecting the emitted script…

The hashed filename may change between builds. Module scripts are deferred by browser semantics.

Choose a placement target

teleport accepts a DOM selector. This module is inserted into a named example target and updates its nearby output. Keep the target unique and present in the rendered document.

An explicitly placed moduleCOMPOSITION
Waiting for the placed module.
Inspecting the emitted script…

The receipt reports the actual parent, so placement is observable rather than inferred from the MDX.

<div id="script-placement-target"></div>
<output id="placed-script-result" aria-live="polite">Waiting for the placed module.</output>
<PageScript id="placed-script-entry" src="./placed.ts" teleport="#script-placement-target"/>
const output = document.querySelector<HTMLOutputElement>(
  '#placed-script-result',
)
if (output)
  output.textContent = 'This module ran from the named placement target.'

Every loading flag

async, defer, and noModule are native script attributes. These receipts show their exact emitted presence. The async and defer comparison tags are inert data blocks, so they demonstrate forwarding without changing execution order on this guide. noModule is paired with a classic script; modern module-capable browsers skip that fallback.

Default, async, defer, and noModuleCOMPOSITION
Flags omitted

No async, defer, or nomodule attribute is requested.

Inspecting the emitted script…
async

Execution does not wait for the normal deferred order.

Inspecting the emitted script…
defer

Useful for classic scripts; module scripts already defer.

Inspecting the emitted script…
noModule

A classic fallback for browsers without module support.

Inspecting the emitted script…

Omitting a boolean attribute differs from emitting it. Bind booleans with a colon; a literal false string is still a present HTML attribute.

<PageScript src="./entry.ts"/>
<PageScript src="./entry.ts" :async="true"/>
<PageScript src="./classic.ts" type="text/javascript" :defer="true"/>
<PageScript src="./classic-fallback.ts" type="text/javascript" :noModule="true"/>

Script type and metadata

The live enhancements use type=module. The classic fallback above uses text/javascript. The metadata comparisons below use application/json, an inert data-block type; they do not execute or fetch their src. The integrity and nonce strings are illustrative values, not production configuration.

Integrity, nonce, and both cross-origin valuesCOMPOSITION
integrity

Supply the exact digest of the resource bytes you intend to load.

Inspecting the emitted script…
nonce

The nonce must match the policy for the actual response.

Inspecting the emitted script…
crossOrigin: anonymous

Forward the native anonymous request setting.

Inspecting the emitted script…
crossOrigin: use-credentials

Forward the native credentialed request setting.

Inspecting the emitted script…

These receipts display actual attributes without loading a third-party script.

Every referrer policy

All native referrerPolicy values are visible together. Empty string leaves the browser’s default policy in effect. These inert tags demonstrate attribute forwarding rather than making requests.

Nine policy valuesCOMPOSITION
Empty string · browser default

Inspect the emitted referrerpolicy attribute.

Inspecting the emitted script…
no-referrer

Inspect the emitted referrerpolicy attribute.

Inspecting the emitted script…
no-referrer-when-downgrade

Inspect the emitted referrerpolicy attribute.

Inspecting the emitted script…
origin

Inspect the emitted referrerpolicy attribute.

Inspecting the emitted script…
origin-when-cross-origin

Inspect the emitted referrerpolicy attribute.

Inspecting the emitted script…
same-origin

Inspect the emitted referrerpolicy attribute.

Inspecting the emitted script…
strict-origin

Inspect the emitted referrerpolicy attribute.

Inspecting the emitted script…
strict-origin-when-cross-origin

Inspect the emitted referrerpolicy attribute.

Inspecting the emitted script…
unsafe-url

Inspect the emitted referrerpolicy attribute.

Inspecting the emitted script…

The component forwards strings to the native attribute; request behavior belongs to the browser.

Every source form

Local entries are bundled; HTTP(S) and protocol-relative URLs remain external. A leading slash means the content source root, rather than the public output root. Query strings and fragments survive resolution. sourceRelPath supplies a different owner directory.

Five ways to resolve the entryCOMPOSITION
Colocatedsrc="./reading-time.ts"

Resolves beside this MDX page.

Sibling directorysrc="../regor-app/playground.ts"

Resolves from this source directory into its sibling.

Content rootsrc="/components/runtime/page-script/reading-time.ts"

Resolves from the configured content root.

Different owner:sourceRelPath="'components/runtime/page-script/page-script.mdx'"

Uses that file’s directory when resolving a relative src.

External URLsrc="https://example.com/widget.js"

Remains external; the SSG does not bundle its bytes. Protocol-relative URLs are also treated as external.

The external URL below is a source example only; this guide does not load it.

<PageScript src="./reading-time.ts?mode=docs#boot"/>
<PageScript src="/components/runtime/page-script/reading-time.ts"/>
<PageScript
  src="./reading-time.ts"
  :sourceRelPath="'components/runtime/page-script/page-script.mdx'"
/>

Inspection entry point

The comparison receipts are populated by this small module. Each receipt names one script ID; the module reads its native attributes and parent without altering the page’s scripts.

// Inspect the actual emitted tags, not a hand-written approximation of the output.
for (const sample of document.querySelectorAll<HTMLElement>(
  '[data-script-receipt]',
)) {
  const script = document.getElementById(sample.dataset.scriptReceipt ?? '')
  const code = sample.querySelector('code')
  if (!(script instanceof HTMLScriptElement) || !code) continue
  const attributes = [...script.attributes].map((attribute) => {
    const value = attribute.name === 'nonce' ? script.nonce : attribute.value
    return value ? `${attribute.name}="${value}"` : attribute.name
  })
  code.textContent = `<script\n  ${attributes.join('\n  ')}\n></script>`
  const placement = sample.querySelector('[data-script-placement]')
  if (placement)
    placement.textContent = `Actual parent: ${script.parentElement?.id ? `#${script.parentElement.id}` : script.parentElement?.tagName.toLowerCase()}`
}

Feature map

Public feature Visible example
src Reading-time enhancement and actual emitted URL receipt.
sourceRelPath Source-form comparison and explicit owner recipe.
teleport A working module inside the named placement target.
type Live module, classic noModule fallback, and inert application/json metadata tags.
async, defer, noModule Four loading-flag receipts and copyable source.
integrity, nonce Actual emitted metadata receipts with illustrative values.
crossOrigin Both anonymous and use-credentials receipts.
referrerPolicy All nine native values shown together.
Native attributes Unique script IDs let the inspection module read the real output.

PageScript has no tone, variant, or variantMode props and no content slot. It emits a script element; the surrounding HTML and the browser behavior supply the visible UI.

Behavior and accessibility

The default script type is module and the default teleport destination is body. Local entries are resolved from the MDX source and bundled; external URLs remain external script URLs. Keep IDs and event bindings scoped when a page has several independent enhancements.

API reference

PageScript contract

Props below are the public template API. RefOrValue accepts a literal or a reactive ref; bind refs with a colon-prefixed attribute. Native attributes and events can be passed through the component root.

teleport

string
Default
body

DOM selector receiving the script.

src

string
Default
required

Browser entry point. A relative path resolves from the current MDX page. SSG records and bundles local scripts.

sourceRelPath

string
Default
detected from the current page

Optional source-resolution override. Omit it for ordinary colocated MDX and TypeScript files.

type

string
Default
module

Native script type. Module scripts have browser module loading semantics.

async

unknown
Default
not set

Native async attribute. Use only when the entry point is independent of execution order.

defer

unknown
Default
not set

Native defer attribute. Modules already defer by default.

integrity

string
Default
not set

Subresource integrity metadata for a resource whose bytes you control.

nonce

string
Default
not set

Content Security Policy nonce forwarded to the script.

crossOrigin

string
Default
not set

Native crossorigin request setting.

referrerPolicy

string
Default
not set

Native script referrerpolicy value.

noModule

unknown
Default
not set

Native nomodule attribute for scripts intended for non-module browsers.

Composition

No slots. This is an SSG component. For a Regor component application, use RegorApp instead of manually pairing a mount element with PageScript.