NOTICE: 2021/04/09 no longer hosts typescript files. Use

Host JSX file as a web page

Like serving static files, JSX files can also be served as a web page.

serveJsx is built-in middleware to serve JSX files. It is similar to serveStatic, but It uses dynamic import to build a response based on JSX file.

import { createApp, serveJsx } from "[email protected]/mod.ts";
const app = createApp();
// .jsx/.tsx files in ./pages directory will be dynamically imported
// and rendered component served as html
app.use(serveJsx("./pages", (f) => import(f)));
app.listen({ port: 8899 });

Here is a typical directory structure for using serveJsx along with serveStatic.

├── main.ts
├── pages
│   ├── about.tsx
│   └── index.tsx
└── public
    └── index.css

JSX files that served as a page must export React component as default. If component needs asynchronous initialization for rendering in server side, you should define getInitialProps to the component.

It is async function that returns Promise of property type of the component. This methodology is similar to Next.js but totally different. Component will be rendered only on the server side and won't be hydrated on the client side.

// @deno-types="[email protected]/types/react/index.d.ts"
import React from "";
import { DFC } from "[email protected]/mod.ts";

const Index: DFC<{ title: string; text: string }> = ({ title, text }) => {
  return (
        <meta charSet={"UTF-8"} />

// getInitialProps is an asynchronous data fetcher
// for rendering components in server side.
// This is identical methodology to Next.js
// It will be called exactly once for each request.
Index.getInitialProps = async () => {
  const resp = await fetch("");
  const text = await resp.text();
  return { title: "Index Page", text };

// default export are used for Server Side Rendering.
export default Index;