From 0fbc9e816aa934e65484f00463b5a0dffd6fb584 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Sat, 25 Sep 2021 11:05:49 +0200 Subject: [PATCH] BREAKING: disable runtime JS by default (#55) --- example/pages/index.tsx | 4 +++- src/runtime/types.ts | 16 ++++++++++------ src/server/context.ts | 2 +- tests/fixture/pages/[name].tsx | 4 +++- tests/fixture/pages/index.tsx | 4 +++- tests/fixture/pages/props/[id].tsx | 4 +++- tests/fixture/pages/static.tsx | 4 +--- 7 files changed, 24 insertions(+), 14 deletions(-) diff --git a/example/pages/index.tsx b/example/pages/index.tsx index 6300bffc..8b6521e2 100644 --- a/example/pages/index.tsx +++ b/example/pages/index.tsx @@ -2,7 +2,7 @@ import { RoundedButton } from "../components/Button.tsx"; import { IconMinus, IconPlus } from "../components/Icons.tsx"; -import { h, IS_BROWSER, tw, useState } from "../deps.ts"; +import { h, IS_BROWSER, PageConfig, tw, useState } from "../deps.ts"; export default function Home() { return ( @@ -43,3 +43,5 @@ function Counter() { ); } + +export const config: PageConfig = { runtimeJS: true }; diff --git a/src/runtime/types.ts b/src/runtime/types.ts index 0e36c51f..42c31d01 100644 --- a/src/runtime/types.ts +++ b/src/runtime/types.ts @@ -19,13 +19,17 @@ export interface PageProps { 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. + * By default, runtime JS is disabled. This means that interactivity on the + * client that depends on Preact or other JS code will not function. * - * It is recommended to disable runtime JavaScript for static pages that do - * not require interactivity, like marketing or blog pages. + * Runtime JS can be enabled by setting `runtimeJS` to `true`. + * + * It is recommended to keep runtime JavaScript disabled for static pages that + * do not require interactivity, like marketing or blog pages. + * + * Note that the runtime JS feature will likely be overhauled in the future to + * provide more granular control over which components need to be hydrated on + * the client. */ runtimeJS?: boolean; } diff --git a/src/server/context.ts b/src/server/context.ts index 5b34c9c2..4b7b6cc6 100644 --- a/src/server/context.ts +++ b/src/server/context.ts @@ -59,7 +59,7 @@ export class ServerContext { name, component, handler, - runtimeJS: config?.runtimeJS ?? true, + runtimeJS: config?.runtimeJS ?? false, }; pages.push(page); } else if ( diff --git a/tests/fixture/pages/[name].tsx b/tests/fixture/pages/[name].tsx index 505b0895..17cf0d30 100644 --- a/tests/fixture/pages/[name].tsx +++ b/tests/fixture/pages/[name].tsx @@ -1,6 +1,6 @@ /** @jsx h */ -import { h } from "../deps.ts"; +import { h, PageConfig } from "../deps.ts"; interface Props { params: Record; @@ -9,3 +9,5 @@ interface Props { export default function Greet(props: Props) { return
Hello {props.params.name}
; } + +export const config: PageConfig = { runtimeJS: true }; diff --git a/tests/fixture/pages/index.tsx b/tests/fixture/pages/index.tsx index 5b7c5277..d108365d 100644 --- a/tests/fixture/pages/index.tsx +++ b/tests/fixture/pages/index.tsx @@ -1,6 +1,6 @@ /** @jsx h */ -import { h, IS_BROWSER, useData } from "../deps.ts"; +import { h, IS_BROWSER, PageConfig, useData } from "../deps.ts"; export default function Home() { const message = useData("home", (key) => { @@ -15,3 +15,5 @@ export default function Home() { ); } + +export const config: PageConfig = { runtimeJS: true }; diff --git a/tests/fixture/pages/props/[id].tsx b/tests/fixture/pages/props/[id].tsx index b0a439bc..a3e1059b 100644 --- a/tests/fixture/pages/props/[id].tsx +++ b/tests/fixture/pages/props/[id].tsx @@ -1,7 +1,9 @@ /** @jsx h */ -import { h, PageProps } from "../../deps.ts"; +import { h, PageConfig, PageProps } from "../../deps.ts"; export default function Home(props: PageProps) { return
{JSON.stringify(props)}
; } + +export const config: PageConfig = { runtimeJS: true }; diff --git a/tests/fixture/pages/static.tsx b/tests/fixture/pages/static.tsx index 02bc8be4..950341d5 100644 --- a/tests/fixture/pages/static.tsx +++ b/tests/fixture/pages/static.tsx @@ -1,9 +1,7 @@ /** @jsx h */ -import { h, PageConfig } from "../deps.ts"; +import { h } from "../deps.ts"; export default function StaticPage() { return

This is a static page.

; } - -export const config: PageConfig = { runtimeJS: false };