Documentation menu

Routes and TSX

Build public destinations with native SpacePress route source. Keep stable URLs part of the content contract rather than a decorative detail.

File routes

The official native routing model uses page files, shared layouts and dynamic segments. A root page.tsx serves the root route; a child route directory supplies a nested destination; [slug]/page.tsx represents a dynamic segment. Follow the actual source root and conventions in your starter.

A route should state what it reads, what it renders and what happens when content is missing. Use a genuine not-found response for an unknown record. Returning a successful page that says “not found” creates ambiguous navigation and discovery behavior.

Layout and components

A shared layout supplies the document framing, site navigation and shared styles. Keep route-specific composition in the route or its components. Do not repeat the entire shell in each page unless there is a deliberate reason.

Start with semantic TSX: one meaningful page heading, section headings in order, real links for navigation, buttons for actions and labels for inputs. A visually unusual composition still needs a useful reading order.

Load and render content

Read records through the installed SpacePress database API rather than copying them into a second array. Share a loader when multiple views use the same visibility and normalization rules. Keep private queries and public queries distinct.

For rich content, use the native content rendering component appropriate to the value rather than printing markup as plain text or injecting unsanitized HTML. SpacePress documents <Content> for WordPress block content and <Html> for sanitized HTML. Check their exact signatures and available imports in the installed version.

Dynamic editorial routes may receive a native typed item context that supports authorized preview behavior. Do not replace that with an anonymous-only custom lookup without understanding the effect on drafts and access rules.

Metadata and addresses

Provide useful titles and descriptions. Keep canonical URLs aligned with the route that actually responds after normalization. Use stable absolute public URLs for social assets where required. Verify the emitted initial HTML rather than assuming a source export became metadata.

Verify routes

Test root, nested, dynamic, empty and unknown cases. Check links from both wide and narrow layouts. Inspect anonymous HTML for the intended body, headings and privacy. Confirm trailing-slash/canonical behavior on the host. Query-string controls need validation and meaningful empty results.

The official routing reference describes additional routing and component capabilities; compilation against the starter's exact version is the next check, not the final proof.

A public query from the starter

The root route validates its page query, selects published records and counts the same public set. For posts with the same publication time, the post ID breaks the tie so the order remains consistent across pages of the same dataset.

import { integer } from "spacepress";
import { posts } from "spacepress:db";
import { home_url } from "php/wordpress";
import { Pagination, type PageProps } from "spacepress/app";

export const search = { journalPage: integer().default(1) };

export default async function Index({ search }: PageProps) {
  const page = search.journalPage > 0 ? search.journalPage : 1;
  const total = await posts.count({ where: { status: "published" } });
  const pages = Math.ceil(total / 6);
  if (page > Math.max(1, pages)) {
    return <section><h1>Page unavailable</h1><a href={home_url("/")}>Back to the first page</a></section>;
  }
  const entries = await posts.list({
    where: { status: "published" },
    orderBy: { publishedAt: "desc", id: "desc" },
    limit: 6,
    offset: (page - 1) * 6,
  });
  return <section>
    <h1>Latest posts</h1>
    {entries.map(post => <article><h2>{post.title}</h2></article>)}
    <Pagination page={page} pages={pages} param="journalPage" />
  </section>;
}

This illustrates the starter's APIs; compile any modified example against the bundled version. Its archive uses the same records with a compact component and 20 records per page. Presentation and density change; identity and publication state do not.

Use a route-specific query parameter such as journalPage on the homepage. WordPress can reserve page for its own content pagination and redirect a root URL using that key. Match the key in the typed search declaration and Pagination props.

Typed detail rendering

The supplied dynamic route uses the native item context:

import { Content, type PageProps } from "spacepress/app";

export default function Post({ item }: PageProps<"journal/[slug]">) {
  return <article>
    <h1>{item.title}</h1>
    {item.description ? <p>{item.description}</p> : null}
    <Content value={item.content} />
  </article>;
}

The shared layout imports ./style.css, chooses template = "blank" and renders its children inside the native shell. Links in the actual starter use WordPress home_url() so they respect the application's mounted base. Keep that behavior when adapting routes.