diff --git a/README.md b/README.md
index 16fbe2b3..0f75f7f6 100644
--- a/README.md
+++ b/README.md
@@ -15,13 +15,16 @@ Fresh does not have a build step - you write your code, deploy it to
the framework.
- No build step
-- Zero config
+- Zero config necessary
- JIT rendering on the edge
-- Tiny (example is 3KB of JS)
-- Client side hydration
+- Tiny (example is 0-3KB of runtime JS)1
+- Optional client side hydration
- TypeScript out of the box
- File-system routing à la Next.js
+_1 Client side hydration can be disabled per page. This results in
+**no JS** being shipped to the client._
+
## Install
To install, run the following command. This will add `fresh` CLI to your PATH.
diff --git a/example/pages/x/[module].tsx b/example/pages/x/[module].tsx
index 5c754771..1322657b 100644
--- a/example/pages/x/[module].tsx
+++ b/example/pages/x/[module].tsx
@@ -1,4 +1,4 @@
-import { h, PageProps, useData } from "../../deps.ts";
+import { h, PageConfig, PageProps, useData } from "../../deps.ts";
export default function ModuleInfoPage({ params }: PageProps) {
const module = params.module;
@@ -32,3 +32,5 @@ async function fetcher(url: string): Promise {
if (resp.status === 200) return resp.json();
return null;
}
+
+export const config: PageConfig = { runtimeJS: false };
diff --git a/src/runtime/types.ts b/src/runtime/types.ts
index 1e384fc9..55977a83 100644
--- a/src/runtime/types.ts
+++ b/src/runtime/types.ts
@@ -1,3 +1,16 @@
export interface PageProps {
params: Record;
}
+
+export interface PageConfig {
+ /**
+ * Setting this flag to `false` disables all client side runtime JS. This
+ * means that all interactivity on the client that depends on Preact or other
+ * JS code will not function. This option is set to `true` (runtime javascript
+ * is enabled) by default.
+ *
+ * It is recommended to disable runtime JavaScript for static pages that do
+ * not require interactivity, like marketing or blog pages.
+ */
+ runtimeJS?: boolean;
+}
diff --git a/src/server/context.ts b/src/server/context.ts
index 51420980..736477a4 100644
--- a/src/server/context.ts
+++ b/src/server/context.ts
@@ -1,12 +1,10 @@
import { extname, mediaTypeLookup, router } from "./deps.ts";
-import * as rt from "../runtime/deps.ts";
-import { PageProps } from "../runtime/types.ts";
import { Routes } from "./mod.ts";
import { Bundler } from "./bundle.ts";
import { INTERNAL_PREFIX } from "./constants.ts";
import { JS_PREFIX } from "./constants.ts";
import { BUILD_ID } from "./constants.ts";
-import { ApiRoute, Page } from "./types.ts";
+import { ApiRoute, ApiRouteModule, Page, PageModule } from "./types.ts";
import { render } from "./render.tsx";
export class ServerContext {
@@ -40,23 +38,26 @@ export class ServerContext {
const route = pathToRoute(baseRoute);
const name = baseRoute.replace("/", "-");
if (path.startsWith("/api/")) {
+ const handlers = Object.fromEntries(
+ Object.entries(module as ApiRouteModule).filter(([method]) =>
+ method === "default" || router.METHODS.includes(method)
+ ),
+ );
const apiRoute: ApiRoute = {
route,
url,
name,
- handlers: Object.fromEntries(
- Object.entries(module).filter(([method]) =>
- router.METHODS.includes(method) || method === "default"
- ),
- ),
+ handlers,
};
apiRoutes.push(apiRoute);
} else if (!path.startsWith("/_")) {
+ const { default: component, config } = (module as PageModule);
const page: Page = {
route,
url,
name,
- component: module.default as rt.ComponentType,
+ component,
+ runtimeJS: config?.runtimeJS ?? true,
};
pages.push(page);
}
@@ -83,8 +84,8 @@ export class ServerContext {
);
const body = await render({
page,
- imports,
- preloads,
+ imports: page.runtimeJS ? imports : [],
+ preloads: page.runtimeJS ? preloads : [],
params: match.params,
});
return new Response(body, {
diff --git a/src/server/render.tsx b/src/server/render.tsx
index 0bea900c..7e622494 100644
--- a/src/server/render.tsx
+++ b/src/server/render.tsx
@@ -53,6 +53,10 @@ export async function render(opts: RenderOptions): Promise {
templateProps = undefined;
}
+ if (opts.imports.length === 0) {
+ templateProps = undefined;
+ }
+
const html = template({
bodyHtml,
imports: opts.imports,
diff --git a/src/server/types.ts b/src/server/types.ts
index fb6362ec..20072784 100644
--- a/src/server/types.ts
+++ b/src/server/types.ts
@@ -1,9 +1,10 @@
import { ComponentType } from "../runtime/deps.ts";
import { router } from "./deps.ts";
-import { PageProps } from "../runtime/types.ts";
+import { PageConfig, PageProps } from "../runtime/types.ts";
export interface PageModule {
default: ComponentType;
+ config?: PageConfig;
}
export interface Page {
@@ -11,6 +12,7 @@ export interface Page {
url: string;
name: string;
component: ComponentType;
+ runtimeJS: boolean;
}
export type ApiRouteModule = {
diff --git a/tests/fixture/pages/static.tsx b/tests/fixture/pages/static.tsx
new file mode 100644
index 00000000..1d7ab74f
--- /dev/null
+++ b/tests/fixture/pages/static.tsx
@@ -0,0 +1,7 @@
+import { h, PageConfig } from "../deps.ts";
+
+export default function StaticPage() {
+ return This is a static page.
;
+}
+
+export const config: PageConfig = { runtimeJS: false };
diff --git a/tests/fixture/routes.gen.ts b/tests/fixture/routes.gen.ts
index 5c37d9d3..cdf30f00 100644
--- a/tests/fixture/routes.gen.ts
+++ b/tests/fixture/routes.gen.ts
@@ -2,17 +2,19 @@
// This file SHOULD be checked into source version control.
// To update this file, run `fresh routes`.
-import * as $0 from "./pages/index.tsx";
+import * as $0 from "./pages/api/name.ts";
import * as $1 from "./pages/api/get_only.ts";
-import * as $2 from "./pages/api/name.ts";
+import * as $2 from "./pages/static.tsx";
import * as $3 from "./pages/[name].tsx";
+import * as $4 from "./pages/index.tsx";
const routes = {
pages: {
- "./pages/index.tsx": $0,
+ "./pages/api/name.ts": $0,
"./pages/api/get_only.ts": $1,
- "./pages/api/name.ts": $2,
+ "./pages/static.tsx": $2,
"./pages/[name].tsx": $3,
+ "./pages/index.tsx": $4,
},
baseUrl: import.meta.url,
};
diff --git a/tests/main_test.ts b/tests/main_test.ts
index 361fb5ce..7418b6cb 100644
--- a/tests/main_test.ts
+++ b/tests/main_test.ts
@@ -85,3 +85,15 @@ Deno.test("/api/xyz not found", async () => {
assert(resp);
assertEquals(resp.status, 404);
});
+
+Deno.test("/static page prerender", async () => {
+ const resp = await router(new Request("https://fresh.deno.dev/static"));
+ assert(resp);
+ assertEquals(resp.status, 200);
+ assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
+ const body = await resp.text();
+ assert(!body.includes(`static.js`));
+ assert(!body.includes(``));
+ assertStringIncludes(body, "This is a static page.
");
+ assert(!body.includes("__FRSH_PROPS"));
+});