Skip to main content

Built and signed on GitHub Actions

Works with
This package works with Browsers, DenoIt is unknown whether this package works with Node.js, Cloudflare Workers, Bun
This package works with Deno
This package works with Browsers
It is unknown whether this package works with Node.js
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Bun
•JSR Score100%•
License
Apache-2.0
•
Downloads306/wk
•
Publisheda day ago (0.0.10)

The Fresh 2 web layer for NetScript: typed route contracts, fluent page builders, managed forms, query and durable-stream islands, and deferred streaming SSR — exposed as focused subpath imports.

@netscript/fresh

JSR CI Docs

The Fresh 2 web layer for NetScript: typed route contracts, fluent page builders, managed forms, query and durable-stream islands, and deferred streaming SSR — exposed as focused subpath imports.

Fresh gives you file-based routes and islands; this package gives those routes a type system. You declare what a page needs — its path and search params, its metadata, its form handlers, its data layers — and definePage() composes all of it into a Fresh route where every piece is checked against the route's contract. Links, redirects, and navigation helpers are derived from the same contract, so a renamed param or a changed path breaks the build, not production.

The rest of the surface covers what a real app hits next: progressively enhanced server-validated forms, TanStack Query islands hydrated from server caches, live queries over durable streams, Suspense-powered streaming SSR with deferred regions, and an error vocabulary shared between server handlers and client displays. Each capability lives on its own subpath, so a page that never streams never imports the streaming runtime.

Why teams use it

  • Typed route contracts — defineRouteContract, paginationSearchSchema, and bindRoutePattern give path and search params one typed source consumed by pages, links, and navigation helpers alike.
  • Fluent page builders — definePage() and definePartial() compose route, metadata, handlers, data layers, and forms into a Fresh route through a chainable, fully inferred builder.
  • Codegen-owned route bindings — with the NetScript Vite plugin enabled, the binding between a page module and its route pattern is generated from the file's path under routes/; you author only the contract body.
  • Managed forms — the Form component plus createStandardSchemaAdapter, CSRF helpers, and intent encoding deliver progressively enhanced, server-validated forms over any Standard Schema.
  • Query and stream islands — QueryIsland with TanStack Query hooks and createNetScriptStreamDB with live-query hooks wire cache-first and durable-stream data into client islands.
  • Streaming and defer — defineFreshApp bootstraps the app, renderToStream powers Suspense SSR, and DeferPage/Deferred defer page regions under a resolvable freshness policy.
  • Desktop RPC — bindDesktopRpcWindow on ./desktop binds an existing oRPC router to one Deno Desktop window while remaining inert in browser and Aspire processes.
  • Ordered partial navigation — installPartialNavigationCoordinator drains superseded Fresh responses without applying them, while KeyedPartial remounts name-changing partial regions.

Architecture

flowchart LR
    C["Route contract<br/>(path + search schemas)"] --> B["definePage()<br/>builder"]
    B --> R["Fresh route<br/>(handler + component)"]
    C --> L["Typed links &amp;<br/>navigation"]
    B --> F["Managed forms<br/>(CSRF, intents)"]
    R --> S["renderToStream<br/>Suspense SSR"]
    S --> D["DeferPage / Deferred<br/>regions"]
    R --> Q["QueryIsland /<br/>stream islands"]

Install

deno add jsr:@netscript/fresh@<version>

Pin <version> to match your installed CLI; bare jsr:@netscript/* specifiers do not resolve on the pre-release line. Inside a scaffolded NetScript workspace the import map already carries the correct pinned entry.

Quick example

With the NetScript Vite plugin enabled, route bindings are generated from the page module's path — you write only the contract body:

import { z } from 'zod';
import { definePage } from '@netscript/fresh/builders';

// routes/orders/[id].tsx — the plugin inserts the route binding for you.
export const ordersDetailPage = definePage()
  .withRouteContract({
    pathSchema: z.object({ id: z.string().min(1) }),
  })
  .withMeta(() => ({ title: 'Order' }))
  .build();

Outside the codegen flow — or to see what the generator emits under the hood — bind the contract to a pattern yourself:

import { definePage } from '@netscript/fresh/builders';
import {
  bindRoutePattern,
  defineRouteContract,
  paginationSearchSchema,
} from '@netscript/fresh/route';

const ordersRoute = bindRoutePattern(
  defineRouteContract({
    searchSchema: paginationSearchSchema({
      defaultLimit: 20,
      defaultSort: 'createdAt',
      defaultOrder: 'desc',
    }),
  }),
  '/orders',
);

export const ordersPage = definePage()
  .withRoute(ordersRoute)
  .withMeta(() => ({
    title: 'Orders',
    description: 'Browse the current order queue.',
  }))
  .build();

The built page exposes the Fresh route pieces (page, handler, route, nav, hooks), and the bound route carries typed href, safeParseSearch, and a contract-aware Link component.

A named partial binds to the same generated reference in one call. Its loader and method handlers receive contract-parsed path and search state; missing path state throws 404, while search state that cannot parse or fall back to contract defaults throws 400.

import { definePartial } from '@netscript/fresh/builders';
// `@app/*` is the scaffolded application's own alias, so it resolves in your app, not here.
import { routes } from '@app/router.ts';

declare function loadOrder(id: string): Promise<{ total: number }>;

export const orderSummaryPartial = definePartial({
  name: 'order-summary',
  route: routes.partials.orders.$id.$route,
  loader: async (ctx) => ({
    order: await loadOrder(ctx.path.id),
  }),
  component: ({ order }) => <aside>{order.total}</aside>,
});

export const { handler } = orderSummaryPartial;
export default orderSummaryPartial.default;

Desktop RPC composition

Deno Desktop composition roots can bind an existing oRPC router to one native window. Browser and Aspire processes return an explicit disabled lifecycle without registering a binding:

import { bindDesktopRpcWindow } from '@netscript/fresh/desktop';
import type { DesktopBindableWindow, DesktopRpcRouter } from '@netscript/fresh/desktop';

declare const desktopWindow: DesktopBindableWindow;
declare const ordersRouter: DesktopRpcRouter;

const desktopRpc = bindDesktopRpcWindow({
  window: desktopWindow,
  router: ordersRouter,
  context: {},
});

// Safe for both active and disabled lifecycles.
await desktopRpc.close();

Each call owns isolated per-window transport state and unbinds exactly once during cleanup. Pair it with createDesktopServiceClient({ contract }) from @netscript/sdk/desktop in the webview; both sides reuse the same oRPC contract instead of a hand-maintained bindings declaration file.

Ordered partial navigation

Install the browser lifecycle explicitly from client code. Repeated installs in one document share the same coordinator; every caller owns a handle and the final dispose() restores only wrappers that are still package-owned.

import { installPartialNavigationCoordinator, KeyedPartial } from '@netscript/fresh/navigation';
import type { Signal } from '@preact/signals';
import type { ComponentChild } from 'preact';

declare const routeEvents: Signal<readonly string[]>;
declare function OrderSummary(props: { orderId: string }): ComponentChild;

const navigation = installPartialNavigationCoordinator();
const unsubscribe = navigation.subscribe(({ kind, url }) => {
  routeEvents.value = [...routeEvents.value, `${kind}:${url.pathname}`];
});

navigation.navigate('/orders');

export function OrderRegion({ orderId }: { orderId: string }) {
  return (
    <KeyedPartial name={`order-${orderId}`}>
      <OrderSummary orderId={orderId} />
    </KeyedPartial>
  );
}

// Client cleanup waits for every superseded finite HTML body to reach EOF.
unsubscribe();
await navigation.dispose();

This compatibility adapter targets Fresh 2.3.3's current partial-fetch and history sequence. It never aborts or cancels a superseded transport: stale bodies are read to EOF and discarded so Vite development servers do not surface transport aborts as overlays. Draining can briefly retain an HTTP/1.1 development-server connection slot, bounded by the finite response body's time to EOF. Fresh normalizes colons to underscores when serializing a VNode key into its marker; the wrapper uses the unmodified region name as the native Preact key and does not rewrite server HTML markers.

Subpaths at a glance

Subpath What it gives you
. Cross-cutting page-loader cache helpers (hasAllCacheEntries, minCachedAt)
./builders definePage, definePartial, defineStatsPartial — the fluent page builders
./route defineRouteContract, bindRoutePattern, paginationSearchSchema, route references
./form The Form component, Standard Schema adapter, CSRF and intent helpers
./defer DeferPage, Deferred, defer policies and decision helpers
./query QueryIsland, hydration, query hooks, server-cache invalidation helper
./server defineFreshApp, standard cache route, streaming response helpers
./desktop bindDesktopRpcWindow — oRPC over one Deno Desktop window, inert elsewhere
./streams createNetScriptStreamDB, useLiveQuery, useLiveSuspenseQuery
./ai Chat connection and stream-proxy helpers for AI-backed pages
./interactive usePromise and promise helpers for interactive islands
./navigation Ordered partial navigation lifecycle, route events, and keyed Fresh boundaries
./vite createNetScriptVitePlugin — codegen for routes and bindings
./error ErrorDisplay, errorHandler, typed error classification and extraction
./testing Mock route contexts and defer policies for page tests

The always-current symbol list is deno doc jsr:@netscript/fresh@<version>.

Preserve server cache age during hydration

When a loader supplies initialData to an island query, also pass the timestamp at which that snapshot was loaded as initialDataUpdatedAt. The public useQuery wrapper seeds both the value and that timestamp into the shared client, so staleTime is measured from the server load instead of from browser hydration:

import { useQuery } from '@netscript/fresh/query';

useQuery({
  queryKey: ordersQueries.list.clientKey(input),
  queryFn: () => ordersClient.list(input),
  initialData: props.initialOrders,
  initialDataUpdatedAt: props.cachedAt,
  staleTime: 15_000,
});

type Order = { readonly id: number; readonly name: string };
type OrdersInput = { readonly limit: number; readonly page: number };

declare const ordersQueries: {
  readonly list: { clientKey(input: OrdersInput): readonly unknown[] };
};
declare const ordersClient: {
  list(input: OrdersInput): Promise<readonly Order[]>;
};
declare const input: OrdersInput;
declare const props: {
  readonly initialOrders: readonly Order[];
  readonly cachedAt: number;
};

An older server snapshot can therefore refetch immediately on hydration, while a snapshot still inside staleTime remains fresh. Omitting initialDataUpdatedAt makes TanStack Query treat the snapshot as newly loaded in the browser and discards its real cache age.

Docs

Compatibility

Runs on Deno 2.x with Fresh 2 and Preact; island hooks hydrate in any modern browser. Type-checking entrypoints should include --unstable-kv, since the streaming server helpers expose KV-aware types.

License

Apache-2.0 — see LICENSE. Published to JSR with cryptographically verified provenance.

Supported Collection runtime

SDK query collections and Fresh live queries share TanStack DB 0.13.0 with @tanstack/query-db-collection 1.4.3, @tanstack/react-db 0.5.8, and @durable-streams/state 0.3.2. These exact declarations are intentional: compatible version ranges alone can admit different Collection constructors. Upgrade the family together and run deno task deps:check:db, which resolves both a mixed SDK/Fresh consumer and a Fresh-only consumer without a workspace lock or warm cache and rejects multiple complete DB identities, including peer resolution suffixes. The guard also rejects unresolved modules.

After coordinated publication, qualify a fresh consumer of the fixed published SDK and Fresh versions with no application dependency overrides before removing downstream DB pins. Source qualification does not prove published resolution.

Pure route URL helpers

Route href, getLinkProps, and paired href/partialHref/getLinkProps use their typed inputs and schema defaults without invoking hooks. They are safe in memo factories, callbacks, SSR and conditional lists. To preserve current search, call page.hooks.useSearch() at the component's top level and pass those values explicitly. page.hooks.useRoute().getLinkProps and bound Link retain contextual preserveSearchParams behavior. Utility calls keep accepting preserve flags but never read current context; migrate implicit preservation to those explicit capabilities.

Desktop RPC keeps one native binding per window. With the matching SDK desktop adapter, a new browser document automatically retires its previous MessagePort before opening a new oRPC channel. Older document callbacks cannot close the replacement. Application shutdown still calls the binding's idempotent close() method. Custom two-argument invoke adapters keep their existing lifecycle; automatic document reconnection requires the coordinated SDK and Fresh release.

StreamDB recovery

The default createNetScriptStreamDB handle starts with await db.preload?.(). Its finite reconnect policy resumes the last consumed batch and retains the same reactive collections. db.status exposes liveness and terminal failure. Configure reconnect.maxRetries, initialDelayMs, and maxDelayMs to bound an outage; zero retries disables recovery. Authentication, malformed data, subscriber errors and invalid retained offsets fail without silently replaying the log. Stop or dispose cancels reads and backoff and is idempotent. Alternate factory adapters may omit these optional lifecycle hooks.

Built and signed on
GitHub Actions

Add Package

deno add jsr:@netscript/fresh

Import symbol

import * as fresh from "@netscript/fresh";
or

Import directly with a jsr specifier

import * as fresh from "jsr:@netscript/fresh@^0.0.10";