sdk/router
RedwoodSDK’s router is a lightweight server-side router that’s designed to work with defineApp from rwsdk/worker.
route
The route function is used to define a route.
import { route } from "rwsdk/router";
route("/", () => new Response("Hello, World!"));prefix
The prefix function is used to modify the matched string of a group of routes, by adding a prefix to the matched string. This essentially allows you to group related functionality into a seperate file, import those routes and place it into your defineApp function.
import { route } from "rwsdk/router";
import { LoginPage } from "./LoginPage";
export const routes = [ route("/login", LoginPage), route("/logout", () => { /* handle logout*/ }),];import { prefix } from "rwsdk/router";
import { routes as userRoutes } from "@/app/pages/user/routes";
defineApp([prefix("/user", userRoutes)]);This will match /user/login and /user/logout
render
The render function is used to statically render the contents of a JSX element. It cannot contain any dynamic content. Use this to control the output of your HTML.
Options
The render function accepts an optional third parameter with the following options:
-
rscPayload(boolean, default:true) - Toggle the RSC payload that’s appended to the Document. Disabling this will mean that interactivity can no longer work. Your document should not include any client side initialization. -
ssr(boolean, default:true) - Enable or disable server-side rendering for these routes. When disabled, only client-side rendering is used, which requiresrscPayloadto be enabled. With SSR disabled, the server returns a minimal HTML shell with the RSC payload, allowing the client to hydrate the page.
import { render } from "rwsdk/router";
import { ReactDocument } from "@/app/Document";import { StaticDocument } from "@/app/Document";
import { routes as appRoutes } from "@/app/pages/app/routes";import { routes as docsRoutes } from "@/app/pages/docs/routes";import { routes as spaRoutes } from "@/app/pages/spa/routes";
export default defineApp([ // Default: SSR enabled with RSC payload render(ReactDocument, [prefix("/app", appRoutes)]),
// Static rendering: SSR enabled, RSC payload disabled render(StaticDocument, [prefix("/docs", docsRoutes)], { rscPayload: false }),
// Client-side only: SSR disabled, RSC payload enabled render(ReactDocument, [prefix("/spa", spaRoutes)], { ssr: false }),]);