feat: add config option to disable runtime js (#36)

The config option can be toggled per page:

```js
export const config: PageConfig = { runtimeJS: false };
```
This commit is contained in:
Luca Casonato
2021-06-13 00:01:59 +02:00
committed by GitHub
parent ae4603c731
commit 65d164d073
9 changed files with 66 additions and 20 deletions
+13
View File
@@ -1,3 +1,16 @@
export interface PageProps {
params: Record<string, string | string[]>;
}
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;
}
+12 -11
View File
@@ -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<PageProps>,
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, {
+4
View File
@@ -53,6 +53,10 @@ export async function render(opts: RenderOptions): Promise<string> {
templateProps = undefined;
}
if (opts.imports.length === 0) {
templateProps = undefined;
}
const html = template({
bodyHtml,
imports: opts.imports,
+3 -1
View File
@@ -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<PageProps>;
config?: PageConfig;
}
export interface Page {
@@ -11,6 +12,7 @@ export interface Page {
url: string;
name: string;
component: ComponentType<PageProps>;
runtimeJS: boolean;
}
export type ApiRouteModule = {