diff --git a/example/pages/index.tsx b/example/pages/index.tsx
index 9d12f807..65089ec6 100644
--- a/example/pages/index.tsx
+++ b/example/pages/index.tsx
@@ -19,23 +19,25 @@ export default function Home() {
function Counter() {
const [count, setCount] = useState(0);
- return
-
Current count: {count}
-
-
setCount(count - 1)}
- disabled={!IS_BROWSER}
- >
-
-
-
setCount(count + 1)}
- disabled={!IS_BROWSER}
- >
-
-
+ return (
+
+
Current count: {count}
+
+ setCount(count - 1)}
+ disabled={!IS_BROWSER}
+ >
+
+
+ setCount(count + 1)}
+ disabled={!IS_BROWSER}
+ >
+
+
+
-
;
+ );
}
diff --git a/example/pages/x/[module].tsx b/example/pages/x/[module].tsx
index 1322657b..05a2faac 100644
--- a/example/pages/x/[module].tsx
+++ b/example/pages/x/[module].tsx
@@ -15,11 +15,13 @@ export default function ModuleInfoPage({ params }: PageProps) {
}
const { latest, versions } = info;
- return
- The module `{params.module}` has {versions.length} versions, with {latest}
- {" "}
- being the most recent.
-
;
+ return (
+
+ The module `{params.module}` has {versions.length} versions, with {latest}
+ {" "}
+ being the most recent.
+
+ );
}
interface ModuleInfo {
diff --git a/src/cli/init.ts b/src/cli/init.ts
index 0a348745..ee7ddfff 100644
--- a/src/cli/init.ts
+++ b/src/cli/init.ts
@@ -80,15 +80,17 @@ export default function Home() {
function Counter() {
const [count, setCount] = useState(0);
- return
-
{count}
-
-
-
;
+ return (
+
+
{count}
+
+
+
+ );
}
`;
await Deno.writeTextFile(
diff --git a/src/runtime/types.ts b/src/runtime/types.ts
index 55977a83..0e36c51f 100644
--- a/src/runtime/types.ts
+++ b/src/runtime/types.ts
@@ -1,4 +1,19 @@
export interface PageProps {
+ /** The URL of the request that resulted in this page being rendered. */
+ url: URL;
+
+ /** The route matcher (e.g. /blog/:id) that the request matched for this page
+ * to be rendered. */
+ route: string;
+
+ /**
+ * The paramteres that were matched from the route.
+ *
+ * For the `/foo/:bar` route with url `/foo/123`, `params` would be
+ * `{ bar: '123' }`. For a route with no matchers, `params` would be `{}`. For
+ * a wildcard route, like `/foo/:path*` with url `/foo/bar/baz`, `params` would
+ * be `{ path: [ 'bar', 'baz' ] }`.
+ */
params: Record
;
}
diff --git a/src/server/context.ts b/src/server/context.ts
index e303b355..0fef084b 100644
--- a/src/server/context.ts
+++ b/src/server/context.ts
@@ -94,7 +94,7 @@ export class ServerContext {
for (const page of this.#pages) {
const bundlePath = `/${page.name}.js`;
const imports = [bundleAssetUrl(bundlePath)];
- routes[page.route] = async (_, match) => {
+ routes[page.route] = async (req, match) => {
const preloads = this.#bundler.getPreloads(bundlePath).map(
bundleAssetUrl,
);
@@ -103,6 +103,7 @@ export class ServerContext {
imports: page.runtimeJS ? imports : [],
preloads: page.runtimeJS ? preloads : [],
renderer: this.#renderer,
+ url: new URL(req.url),
params: match.params,
});
return new Response(body, {
diff --git a/src/server/render.tsx b/src/server/render.tsx
index 3fbf0c35..2866660c 100644
--- a/src/server/render.tsx
+++ b/src/server/render.tsx
@@ -2,12 +2,14 @@ import { renderToString } from "./deps.ts";
import { ComponentChild, h } from "../runtime/deps.ts";
import { DATA_CONTEXT } from "../runtime/hooks.ts";
import { Page, Renderer } from "./types.ts";
+import { PageProps } from "../runtime/types.ts";
export interface RenderOptions {
page: Page;
imports: string[];
preloads: string[];
- params: Record;
+ url: URL;
+ params: Record;
renderer: Renderer;
}
@@ -17,9 +19,13 @@ export class RenderContext {
#id: string;
#state: Map = new Map();
#head: ComponentChild[] = [];
+ #url: URL;
+ #route: string;
- constructor(id: string) {
+ constructor(id: string, url: URL, route: string) {
this.#id = id;
+ this.#url = url;
+ this.#route = route;
}
/** A unique ID for this logical JIT render. */
@@ -42,12 +48,23 @@ export class RenderContext {
get head(): ComponentChild[] {
return this.#head;
}
+
+ /** The URL of the page being rendered. */
+ get url(): URL {
+ return this.#url;
+ }
+
+ /** The route matcher (e.g. /blog/:id) that the request matched for this page
+ * to be rendered. */
+ get route(): string {
+ return this.#route;
+ }
}
const MAX_SUSPENSE_DEPTH = 10;
export async function render(opts: RenderOptions): Promise {
- const props = { params: opts.params };
+ const props = { params: opts.params, url: opts.url, route: opts.page.route };
const dataCache = new Map();
@@ -56,7 +73,7 @@ export async function render(opts: RenderOptions): Promise {
children: h(opts.page.component, props),
});
- const ctx = new RenderContext(crypto.randomUUID());
+ const ctx = new RenderContext(crypto.randomUUID(), opts.url, opts.page.route);
let suspended = 0;
const renderWithRenderer = (): string | Promise => {
@@ -97,19 +114,15 @@ export async function render(opts: RenderOptions): Promise {
opts.renderer.postRender(ctx, bodyHtml);
let templateProps: {
- params?: Record;
+ props: PageProps;
data?: [string, unknown][];
- } | undefined = { params: opts.params, data: [...dataCache.entries()] };
- if (Object.entries(templateProps.params!).length === 0) {
- delete templateProps.params;
- }
+ } | undefined = { props, data: [...dataCache.entries()] };
if (templateProps.data!.length === 0) {
delete templateProps.data;
}
- if (Object.entries(templateProps).length === 0) {
- templateProps = undefined;
- }
+ // If this is a static render (runtimeJS is false), then we don't need to
+ // render the props into the template.
if (opts.imports.length === 0) {
templateProps = undefined;
}
@@ -144,11 +157,13 @@ export function template(opts: TemplateOptions): string {
{opts.props !== undefined
- ?
+ ? (
+
+ )
: null}