Route Exports

View as Markdown

Route files in src/routes can export the following named exports.

default (Component)

Required for pages. The React/Preact component to render for this route. The one exception is a resource route: a module that exports HTTP method handlers and no default component is served as an endpoint, not a page.

export default function Page() {
  return <h1>Hello</h1>;
}

loader

Optional. Async function to load data on the server.

export async function loader(args: LoaderArgs): Promise<Data>;

action

Optional. Async function to handle non-GET requests.

export async function action(args: ActionArgs): Promise<Data>;

HTTP method handlers (GET, POST, …)

Optional, resource routes only. A route module with method-handler exports and no default component is served as an HTTP endpoint — see Resource Routes for the full contract (return values, 405/Allow, middleware, client-build stripping). A module that exports both a component and method handlers fails the build.

// src/routes/api/health.ts → GET /api/health
export async function GET() {
  return { ok: true };
}

config

Optional. Configuration object for the route.

export const config = {
  mode: "static" | "app" // default: "static"
};

ErrorBoundary

Optional. Component to render when an error occurs in this route or its children.

export function ErrorBoundary() {
  return <h1>Something went wrong</h1>;
}

headers

Optional. Function to set HTTP headers for the response.

export function headers({ loaderHeaders, parentHeaders }) {
  return {
    "Cache-Control": "max-age=3600",
  };
}

Optional. Function to set the document <head> — title, meta tags, and <link> tags. It receives the route's data (plus params, pathname, request, context) and returns an SEO object.

export function head({ data }) {
  return {
    title: data.post.title,
    description: data.post.excerpt,
    canonical: `https://example.com/blog/${data.post.slug}`,
    openGraph: { image: data.post.cover },
    // Arbitrary <link> tags: favicon, preconnect, manifest, alternate…
    link: { rel: "icon", type: "image/svg+xml", href: "/favicon.svg" },
  };
}

Supported fields: title, titleTemplate (e.g. "%s — My Site", usually set on a layout so child routes inherit it), defaultTitle, description, canonical (sent exactly as given), keywords, noindex, robots, viewport, themeColor, authors, creator, verification, openGraph, twitter, jsonLd, link, meta, headScripts, htmlAttrs, bodyAttrs. A layout's head() and a route's head() merge — the route wins per field, while link, headScripts, and jsonLd are concatenated and meta entries replace the layout's with the same key. Every value is escaped.

The fields Next's Metadata and Viewport exports have keep their shape, so they map one to one:

// src/routes/_layout.tsx
export function head() {
  return {
    titleTemplate: "%s | Fylun",
    viewport: { width: "device-width", initialScale: 1, interactiveWidget: "resizes-content" },
    themeColor: [
      { media: "(prefers-color-scheme: light)", color: "white" },
      { media: "(prefers-color-scheme: dark)", color: "#0a0a0a" },
    ],
    authors: [{ name: "Fylun" }],
    creator: "Fylun",
    robots: { index: true, follow: true }, // or a string, sent as written
    verification: { google: process.env.GOOGLE_SITE_VERIFICATION },
    openGraph: {
      locale: "en_US",
      siteName: "Fylun",
      images: [{ url: "https://fylun.ai/og.png", width: 1200, height: 630, type: "image/png", alt: "Fylun" }],
    },
    // Anything else: one entry per <meta>, by name, property or httpEquiv.
    meta: [{ name: "format-detection", content: "telephone=no" }],
  };
}

noindex: true writes <meta name="robots" content="noindex">; a route's noindex or robots replaces its layout's. viewport replaces the default width=device-width, initial-scale=1.0.

index.html's head

Server-rendered documents — start, dev, prerendered pages and the adapter runtimes — carry the <title>, <meta> and <link> tags of the app's index.html <head>, as Vite does for a client-rendered app. They are the layer under the route chain: a head() tag with the same key replaces them — the same name, property or http-equiv (a page's themeColor replaces every theme-color in index.html), the title, a canonical or manifest link, or a link with the same rel and href. The framework's charset always wins; stylesheets, modulepreload links, <script> and <style> are not carried over (Neutron links each route's CSS and its client entry; put inline scripts in headScripts, which get the CSP nonce). neutron-ts build records the head of the built dist/index.html, after Vite replaced %ENV% placeholders and asset URLs, in dist/.neutron-document-head.json; dev reads index.html itself. Client navigation applies each page's whole head: tags the new page no longer has are removed.