BREAKING: make render fn an option on start (#260)
This commit removes the `_render.{tj}s{x}?` file in favor of a `render` option on the `start` call in `main.ts`.
This commit is contained in:
+13
-18
@@ -17,6 +17,7 @@ import {
|
||||
AppModule,
|
||||
ErrorPage,
|
||||
ErrorPageModule,
|
||||
FreshOptions,
|
||||
Handler,
|
||||
Island,
|
||||
Middleware,
|
||||
@@ -24,8 +25,7 @@ import {
|
||||
MiddlewareRoute,
|
||||
Page,
|
||||
PageModule,
|
||||
Renderer,
|
||||
RendererModule,
|
||||
RenderFunction,
|
||||
RouterState,
|
||||
UnknownPage,
|
||||
UnknownPageModule,
|
||||
@@ -53,7 +53,7 @@ export class ServerContext {
|
||||
#islands: Island[];
|
||||
#staticFiles: StaticFile[];
|
||||
#bundler: Bundler;
|
||||
#renderer: Renderer;
|
||||
#renderFn: RenderFunction;
|
||||
#middlewares: MiddlewareRoute[];
|
||||
#app: AppModule;
|
||||
#notFound: UnknownPage;
|
||||
@@ -63,7 +63,7 @@ export class ServerContext {
|
||||
pages: Page[],
|
||||
islands: Island[],
|
||||
staticFiles: StaticFile[],
|
||||
renderer: Renderer,
|
||||
renderfn: RenderFunction,
|
||||
middlewares: MiddlewareRoute[],
|
||||
app: AppModule,
|
||||
notFound: UnknownPage,
|
||||
@@ -73,7 +73,7 @@ export class ServerContext {
|
||||
this.#pages = pages;
|
||||
this.#islands = islands;
|
||||
this.#staticFiles = staticFiles;
|
||||
this.#renderer = renderer;
|
||||
this.#renderFn = renderfn;
|
||||
this.#middlewares = middlewares;
|
||||
this.#app = app;
|
||||
this.#notFound = notFound;
|
||||
@@ -85,7 +85,10 @@ export class ServerContext {
|
||||
/**
|
||||
* Process the manifest into individual components and pages.
|
||||
*/
|
||||
static async fromManifest(manifest: Manifest): Promise<ServerContext> {
|
||||
static async fromManifest(
|
||||
manifest: Manifest,
|
||||
opts: FreshOptions,
|
||||
): Promise<ServerContext> {
|
||||
// Get the manifest' base URL.
|
||||
const baseUrl = new URL("./", manifest.baseUrl).href;
|
||||
const importMapURL = new URL("./import_map.json", manifest.baseUrl);
|
||||
@@ -93,7 +96,6 @@ export class ServerContext {
|
||||
// Extract all routes, and prepare them into the `Page` structure.
|
||||
const pages: Page[] = [];
|
||||
const islands: Island[] = [];
|
||||
let renderer: Renderer = DEFAULT_RENDERER;
|
||||
const middlewares: MiddlewareRoute[] = [];
|
||||
let app: AppModule = DEFAULT_APP;
|
||||
let notFound: UnknownPage = DEFAULT_NOT_FOUND;
|
||||
@@ -132,11 +134,6 @@ export class ServerContext {
|
||||
csp: Boolean(config?.csp ?? false),
|
||||
};
|
||||
pages.push(page);
|
||||
} else if (
|
||||
path === "/_render.tsx" || path === "/_render.ts" ||
|
||||
path === "/_render.jsx" || path === "/_render.js"
|
||||
) {
|
||||
renderer = module as RendererModule;
|
||||
} else if (isMiddleware) {
|
||||
middlewares.push({
|
||||
...middlewarePathToPattern(baseRoute),
|
||||
@@ -255,7 +252,7 @@ export class ServerContext {
|
||||
pages,
|
||||
islands,
|
||||
staticFiles,
|
||||
renderer,
|
||||
opts.render ?? DEFAULT_RENDER_FN,
|
||||
middlewares,
|
||||
app,
|
||||
notFound,
|
||||
@@ -407,7 +404,7 @@ export class ServerContext {
|
||||
app: this.#app,
|
||||
imports,
|
||||
preloads,
|
||||
renderer: this.#renderer,
|
||||
renderFn: this.#renderFn,
|
||||
url: new URL(req.url),
|
||||
params,
|
||||
data,
|
||||
@@ -567,10 +564,8 @@ export class ServerContext {
|
||||
};
|
||||
}
|
||||
|
||||
const DEFAULT_RENDERER: Renderer = {
|
||||
render(_ctx, render) {
|
||||
render();
|
||||
},
|
||||
const DEFAULT_RENDER_FN: RenderFunction = (_ctx, render) => {
|
||||
render();
|
||||
};
|
||||
|
||||
const DEFAULT_APP: AppModule = {
|
||||
|
||||
+5
-5
@@ -6,24 +6,24 @@ import {
|
||||
IslandModule,
|
||||
MiddlewareModule,
|
||||
PageModule,
|
||||
RendererModule,
|
||||
StartOptions,
|
||||
UnknownPageModule,
|
||||
} from "./types.ts";
|
||||
export type {
|
||||
FreshOptions,
|
||||
Handler,
|
||||
HandlerContext,
|
||||
Handlers,
|
||||
MiddlewareHandlerContext,
|
||||
RenderFunction,
|
||||
} from "./types.ts";
|
||||
export { RenderContext } from "./render.tsx";
|
||||
export type { RenderFn } from "./render.tsx";
|
||||
export type { InnerRenderFunction } from "./render.tsx";
|
||||
|
||||
export interface Manifest {
|
||||
routes: Record<
|
||||
string,
|
||||
| PageModule
|
||||
| RendererModule
|
||||
| MiddlewareModule
|
||||
| AppModule
|
||||
| ErrorPageModule
|
||||
@@ -37,9 +37,9 @@ export { ServerContext };
|
||||
|
||||
export async function start(
|
||||
routes: Manifest,
|
||||
opts?: StartOptions,
|
||||
opts: StartOptions = {},
|
||||
) {
|
||||
const ctx = await ServerContext.fromManifest(routes);
|
||||
const ctx = await ServerContext.fromManifest(routes, opts);
|
||||
console.log(
|
||||
`Server listening on http://${opts?.hostname ?? "localhost"}:${
|
||||
opts?.port ?? 8000
|
||||
|
||||
@@ -6,7 +6,7 @@ import {
|
||||
ErrorPage,
|
||||
Island,
|
||||
Page,
|
||||
Renderer,
|
||||
RenderFunction,
|
||||
UnknownPage,
|
||||
} from "./types.ts";
|
||||
import { HEAD_CONTEXT } from "../runtime/head.ts";
|
||||
@@ -23,12 +23,12 @@ export interface RenderOptions<Data> {
|
||||
preloads: string[];
|
||||
url: URL;
|
||||
params: Record<string, string | string[]>;
|
||||
renderer: Renderer;
|
||||
renderFn: RenderFunction;
|
||||
data?: Data;
|
||||
error?: unknown;
|
||||
}
|
||||
|
||||
export type RenderFn = () => string;
|
||||
export type InnerRenderFunction = () => string;
|
||||
|
||||
export class RenderContext {
|
||||
#id: string;
|
||||
@@ -174,10 +174,10 @@ export async function render<Data>(
|
||||
return bodyHtml;
|
||||
}
|
||||
|
||||
await opts.renderer.render(ctx, render as RenderFn);
|
||||
await opts.renderFn(ctx, render as InnerRenderFunction);
|
||||
|
||||
if (bodyHtml === null) {
|
||||
throw new Error("`render` function not called by renderer.");
|
||||
throw new Error("The `render` function was not called by the renderer.");
|
||||
}
|
||||
|
||||
const imports = opts.imports.map((url) => {
|
||||
|
||||
+11
-10
@@ -7,13 +7,22 @@ import {
|
||||
RouteConfig,
|
||||
UnknownPageProps,
|
||||
} from "../runtime/types.ts";
|
||||
import { RenderContext, RenderFn } from "./render.tsx";
|
||||
import { InnerRenderFunction, RenderContext } from "./render.tsx";
|
||||
|
||||
export interface RouterState {
|
||||
state: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export type StartOptions = ServeInit;
|
||||
export type StartOptions = ServeInit & FreshOptions;
|
||||
|
||||
export interface FreshOptions {
|
||||
render?: RenderFunction;
|
||||
}
|
||||
|
||||
export type RenderFunction = (
|
||||
ctx: RenderContext,
|
||||
render: InnerRenderFunction,
|
||||
) => void | Promise<void>;
|
||||
|
||||
export interface PageModule {
|
||||
default?: ComponentType<PageProps>;
|
||||
@@ -112,14 +121,6 @@ export interface ErrorPage {
|
||||
csp: boolean;
|
||||
}
|
||||
|
||||
export interface RendererModule {
|
||||
render(ctx: RenderContext, render: RenderFn): void | Promise<void>;
|
||||
}
|
||||
|
||||
export interface Renderer {
|
||||
render(ctx: RenderContext, render: RenderFn): void | Promise<void>;
|
||||
}
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
export interface MiddlewareModule<State = any> {
|
||||
handler(
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ import { REFRESH_JS_URL } from "../src/server/constants.ts";
|
||||
import { assert, assertEquals, assertStringIncludes } from "./deps.ts";
|
||||
import manifest from "./fixture_error/fresh.gen.ts";
|
||||
|
||||
const ctx = await ServerContext.fromManifest(manifest);
|
||||
const ctx = await ServerContext.fromManifest(manifest, {});
|
||||
const router = (req: Request) => {
|
||||
return ctx.handler()(req, {
|
||||
localAddr: {
|
||||
|
||||
+40
-42
@@ -7,27 +7,26 @@ import * as $1 from "./routes/_404.tsx";
|
||||
import * as $2 from "./routes/_500.tsx";
|
||||
import * as $3 from "./routes/_app.tsx";
|
||||
import * as $4 from "./routes/_middleware.ts";
|
||||
import * as $5 from "./routes/_render.ts";
|
||||
import * as $6 from "./routes/api/get_only.ts";
|
||||
import * as $7 from "./routes/assetsCaching/index.tsx";
|
||||
import * as $8 from "./routes/books/[id].tsx";
|
||||
import * as $9 from "./routes/connInfo.ts";
|
||||
import * as $10 from "./routes/failure.ts";
|
||||
import * as $11 from "./routes/index.tsx";
|
||||
import * as $12 from "./routes/intercept.tsx";
|
||||
import * as $13 from "./routes/intercept_args.tsx";
|
||||
import * as $14 from "./routes/islands/index.tsx";
|
||||
import * as $15 from "./routes/layeredMdw/_middleware.ts";
|
||||
import * as $16 from "./routes/layeredMdw/layer2-no-mw/without_mw.ts";
|
||||
import * as $17 from "./routes/layeredMdw/layer2/_middleware.ts";
|
||||
import * as $18 from "./routes/layeredMdw/layer2/abc.ts";
|
||||
import * as $19 from "./routes/layeredMdw/layer2/layer3/[id].ts";
|
||||
import * as $20 from "./routes/layeredMdw/layer2/layer3/_middleware.ts";
|
||||
import * as $21 from "./routes/middleware_root.ts";
|
||||
import * as $22 from "./routes/params.tsx";
|
||||
import * as $23 from "./routes/props/[id].tsx";
|
||||
import * as $24 from "./routes/static.tsx";
|
||||
import * as $25 from "./routes/wildcard.tsx";
|
||||
import * as $5 from "./routes/api/get_only.ts";
|
||||
import * as $6 from "./routes/assetsCaching/index.tsx";
|
||||
import * as $7 from "./routes/books/[id].tsx";
|
||||
import * as $8 from "./routes/connInfo.ts";
|
||||
import * as $9 from "./routes/failure.ts";
|
||||
import * as $10 from "./routes/index.tsx";
|
||||
import * as $11 from "./routes/intercept.tsx";
|
||||
import * as $12 from "./routes/intercept_args.tsx";
|
||||
import * as $13 from "./routes/islands/index.tsx";
|
||||
import * as $14 from "./routes/layeredMdw/_middleware.ts";
|
||||
import * as $15 from "./routes/layeredMdw/layer2-no-mw/without_mw.ts";
|
||||
import * as $16 from "./routes/layeredMdw/layer2/_middleware.ts";
|
||||
import * as $17 from "./routes/layeredMdw/layer2/abc.ts";
|
||||
import * as $18 from "./routes/layeredMdw/layer2/layer3/[id].ts";
|
||||
import * as $19 from "./routes/layeredMdw/layer2/layer3/_middleware.ts";
|
||||
import * as $20 from "./routes/middleware_root.ts";
|
||||
import * as $21 from "./routes/params.tsx";
|
||||
import * as $22 from "./routes/props/[id].tsx";
|
||||
import * as $23 from "./routes/static.tsx";
|
||||
import * as $24 from "./routes/wildcard.tsx";
|
||||
import * as $$0 from "./islands/Counter.tsx";
|
||||
import * as $$1 from "./islands/Test.tsx";
|
||||
|
||||
@@ -38,27 +37,26 @@ const manifest = {
|
||||
"./routes/_500.tsx": $2,
|
||||
"./routes/_app.tsx": $3,
|
||||
"./routes/_middleware.ts": $4,
|
||||
"./routes/_render.ts": $5,
|
||||
"./routes/api/get_only.ts": $6,
|
||||
"./routes/assetsCaching/index.tsx": $7,
|
||||
"./routes/books/[id].tsx": $8,
|
||||
"./routes/connInfo.ts": $9,
|
||||
"./routes/failure.ts": $10,
|
||||
"./routes/index.tsx": $11,
|
||||
"./routes/intercept.tsx": $12,
|
||||
"./routes/intercept_args.tsx": $13,
|
||||
"./routes/islands/index.tsx": $14,
|
||||
"./routes/layeredMdw/_middleware.ts": $15,
|
||||
"./routes/layeredMdw/layer2-no-mw/without_mw.ts": $16,
|
||||
"./routes/layeredMdw/layer2/_middleware.ts": $17,
|
||||
"./routes/layeredMdw/layer2/abc.ts": $18,
|
||||
"./routes/layeredMdw/layer2/layer3/[id].ts": $19,
|
||||
"./routes/layeredMdw/layer2/layer3/_middleware.ts": $20,
|
||||
"./routes/middleware_root.ts": $21,
|
||||
"./routes/params.tsx": $22,
|
||||
"./routes/props/[id].tsx": $23,
|
||||
"./routes/static.tsx": $24,
|
||||
"./routes/wildcard.tsx": $25,
|
||||
"./routes/api/get_only.ts": $5,
|
||||
"./routes/assetsCaching/index.tsx": $6,
|
||||
"./routes/books/[id].tsx": $7,
|
||||
"./routes/connInfo.ts": $8,
|
||||
"./routes/failure.ts": $9,
|
||||
"./routes/index.tsx": $10,
|
||||
"./routes/intercept.tsx": $11,
|
||||
"./routes/intercept_args.tsx": $12,
|
||||
"./routes/islands/index.tsx": $13,
|
||||
"./routes/layeredMdw/_middleware.ts": $14,
|
||||
"./routes/layeredMdw/layer2-no-mw/without_mw.ts": $15,
|
||||
"./routes/layeredMdw/layer2/_middleware.ts": $16,
|
||||
"./routes/layeredMdw/layer2/abc.ts": $17,
|
||||
"./routes/layeredMdw/layer2/layer3/[id].ts": $18,
|
||||
"./routes/layeredMdw/layer2/layer3/_middleware.ts": $19,
|
||||
"./routes/middleware_root.ts": $20,
|
||||
"./routes/params.tsx": $21,
|
||||
"./routes/props/[id].tsx": $22,
|
||||
"./routes/static.tsx": $23,
|
||||
"./routes/wildcard.tsx": $24,
|
||||
},
|
||||
islands: {
|
||||
"./islands/Counter.tsx": $$0,
|
||||
|
||||
@@ -6,5 +6,6 @@
|
||||
|
||||
import { start } from "$fresh/server.ts";
|
||||
import routes from "./fresh.gen.ts";
|
||||
import options from "./options.ts";
|
||||
|
||||
await start(routes);
|
||||
await start(routes, options);
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
import { FreshOptions } from "$fresh/server.ts";
|
||||
|
||||
export default {
|
||||
async render(_ctx, render) {
|
||||
await new Promise<void>((r) => r());
|
||||
const body = render();
|
||||
if (typeof body !== "string") {
|
||||
throw new Error("body is missing");
|
||||
}
|
||||
},
|
||||
} as FreshOptions;
|
||||
@@ -1,9 +0,0 @@
|
||||
import { RenderContext, RenderFn } from "../../../server.ts";
|
||||
|
||||
export async function render(_ctx: RenderContext, render: RenderFn) {
|
||||
await new Promise<void>((r) => r());
|
||||
const body = render();
|
||||
if (typeof body !== "string") {
|
||||
throw new Error("body is missing");
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -1,8 +1,9 @@
|
||||
import { ServerContext } from "../server.ts";
|
||||
import { assert, assertEquals, assertStringIncludes } from "./deps.ts";
|
||||
import manifest from "./fixture/fresh.gen.ts";
|
||||
import options from "./fixture/options.ts";
|
||||
|
||||
const ctx = await ServerContext.fromManifest(manifest);
|
||||
const ctx = await ServerContext.fromManifest(manifest, options);
|
||||
const router = (req: Request) => {
|
||||
return ctx.handler()(req, {
|
||||
localAddr: {
|
||||
|
||||
+6
-8
@@ -2,19 +2,17 @@
|
||||
// This file SHOULD be checked into source version control.
|
||||
// This file is automatically updated during development when running `dev.ts`.
|
||||
|
||||
import * as $0 from "./routes/_render.ts";
|
||||
import * as $1 from "./routes/docs/[...slug].tsx";
|
||||
import * as $2 from "./routes/gfm.css.ts";
|
||||
import * as $3 from "./routes/index.tsx";
|
||||
import * as $0 from "./routes/docs/[...slug].tsx";
|
||||
import * as $1 from "./routes/gfm.css.ts";
|
||||
import * as $2 from "./routes/index.tsx";
|
||||
import * as $$0 from "./islands/Counter.tsx";
|
||||
import * as $$1 from "./islands/LemonDrop.tsx";
|
||||
|
||||
const manifest = {
|
||||
routes: {
|
||||
"./routes/_render.ts": $0,
|
||||
"./routes/docs/[...slug].tsx": $1,
|
||||
"./routes/gfm.css.ts": $2,
|
||||
"./routes/index.tsx": $3,
|
||||
"./routes/docs/[...slug].tsx": $0,
|
||||
"./routes/gfm.css.ts": $1,
|
||||
"./routes/index.tsx": $2,
|
||||
},
|
||||
islands: {
|
||||
"./islands/Counter.tsx": $$0,
|
||||
|
||||
+17
-1
@@ -5,6 +5,22 @@
|
||||
/// <reference lib="deno.unstable" />
|
||||
|
||||
import { start } from "$fresh/server.ts";
|
||||
import { virtualSheet } from "$twind/sheets";
|
||||
import { setup, theme } from "./utils/twind.ts";
|
||||
|
||||
import routes from "./fresh.gen.ts";
|
||||
|
||||
await start(routes);
|
||||
const sheet = virtualSheet();
|
||||
sheet.reset();
|
||||
setup({ sheet, theme });
|
||||
|
||||
await start(routes, {
|
||||
render(ctx, render) {
|
||||
const snapshot = ctx.state.get("twindSnapshot") as unknown[] | null;
|
||||
sheet.reset(snapshot || undefined);
|
||||
render();
|
||||
ctx.styles.splice(0, ctx.styles.length, ...(sheet).target);
|
||||
const newSnapshot = sheet.reset();
|
||||
ctx.state.set("twindSnapshot", newSnapshot);
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
// This module adds twind support.
|
||||
|
||||
import { RenderContext, RenderFn } from "$fresh/server.ts";
|
||||
import { setup, theme } from "../utils/twind.ts";
|
||||
import { virtualSheet } from "$twind/sheets";
|
||||
|
||||
const sheet = virtualSheet();
|
||||
sheet.reset();
|
||||
setup({ sheet, theme });
|
||||
|
||||
export function render(ctx: RenderContext, render: RenderFn) {
|
||||
const snapshot = ctx.state.get("twindSnapshot") as unknown[] | null;
|
||||
sheet.reset(snapshot || undefined);
|
||||
render();
|
||||
ctx.styles.splice(0, ctx.styles.length, ...(sheet).target);
|
||||
const newSnapshot = sheet.reset();
|
||||
ctx.state.set("twindSnapshot", newSnapshot);
|
||||
}
|
||||
Reference in New Issue
Block a user