feat: _app (#87)

Co-authored-by: Luca Casonato <hello@lcas.dev>
This commit is contained in:
Leo Kettmeir
2022-02-18 19:30:24 +01:00
committed by GitHub
co-authored by Luca Casonato
parent 2ec980ca45
commit 04a32f3498
8 changed files with 67 additions and 5 deletions
+7 -1
View File
@@ -1,5 +1,7 @@
import { ComponentType } from "./deps.ts";
export interface PageProps< export interface PageProps<
T extends Record<string, unknown> = Record<never, never>, T extends Record<string, unknown> = Record<string, unknown>,
> { > {
/** The URL of the request that resulted in this page being rendered. */ /** The URL of the request that resulted in this page being rendered. */
url: URL; url: URL;
@@ -25,6 +27,10 @@ export interface PageProps<
renderData?: T; renderData?: T;
} }
export interface AppProps {
Component: ComponentType<Record<never, never>>;
}
export interface UnknownPageProps { export interface UnknownPageProps {
/** The URL of the request that resulted in this page being rendered. */ /** The URL of the request that resulted in this page being rendered. */
url: URL; url: URL;
+17
View File
@@ -12,6 +12,7 @@ import { ALIVE_URL, INTERNAL_PREFIX, REFRESH_JS_URL } from "./constants.ts";
import { JS_PREFIX } from "./constants.ts"; import { JS_PREFIX } from "./constants.ts";
import { BUILD_ID } from "./constants.ts"; import { BUILD_ID } from "./constants.ts";
import { import {
AppModule,
ErrorPage, ErrorPage,
ErrorPageModule, ErrorPageModule,
Handler, Handler,
@@ -30,6 +31,7 @@ import {
ContentSecurityPolicyDirectives, ContentSecurityPolicyDirectives,
SELF, SELF,
} from "../runtime/csp.ts"; } from "../runtime/csp.ts";
import { h } from "../runtime/deps.ts";
interface StaticFile { interface StaticFile {
/** The URL to the static file on disk. */ /** The URL to the static file on disk. */
@@ -49,6 +51,7 @@ export class ServerContext {
#bundler: Bundler; #bundler: Bundler;
#renderer: Renderer; #renderer: Renderer;
#middleware: Middleware; #middleware: Middleware;
#app: AppModule;
#notFound: UnknownPage; #notFound: UnknownPage;
#error: ErrorPage; #error: ErrorPage;
@@ -57,6 +60,7 @@ export class ServerContext {
staticFiles: StaticFile[], staticFiles: StaticFile[],
renderer: Renderer, renderer: Renderer,
middleware: Middleware, middleware: Middleware,
app: AppModule,
notFound: UnknownPage, notFound: UnknownPage,
error: ErrorPage, error: ErrorPage,
) { ) {
@@ -64,6 +68,7 @@ export class ServerContext {
this.#staticFiles = staticFiles; this.#staticFiles = staticFiles;
this.#renderer = renderer; this.#renderer = renderer;
this.#middleware = middleware; this.#middleware = middleware;
this.#app = app;
this.#notFound = notFound; this.#notFound = notFound;
this.#error = error; this.#error = error;
this.#bundler = new Bundler(pages); this.#bundler = new Bundler(pages);
@@ -81,6 +86,7 @@ export class ServerContext {
const pages: Page[] = []; const pages: Page[] = [];
let renderer: Renderer = DEFAULT_RENDERER; let renderer: Renderer = DEFAULT_RENDERER;
let middleware: Middleware = DEFAULT_MIDDLEWARE; let middleware: Middleware = DEFAULT_MIDDLEWARE;
let app: AppModule = DEFAULT_APP;
let notFound: UnknownPage = DEFAULT_NOT_FOUND; let notFound: UnknownPage = DEFAULT_NOT_FOUND;
let error: ErrorPage = DEFAULT_ERROR; let error: ErrorPage = DEFAULT_ERROR;
for (const [self, module] of Object.entries(routes.pages)) { for (const [self, module] of Object.entries(routes.pages)) {
@@ -125,6 +131,11 @@ export class ServerContext {
path === "/_middleware.jsx" || path === "/_middleware.js" path === "/_middleware.jsx" || path === "/_middleware.js"
) { ) {
middleware = module as MiddlewareModule; middleware = module as MiddlewareModule;
} else if (
path === "/_app.tsx" || path === "/_app.ts" ||
path === "/_app.jsx" || path === "/_app.js"
) {
app = module as AppModule;
} else if ( } else if (
path === "/_404.tsx" || path === "/_404.ts" || path === "/_404.tsx" || path === "/_404.ts" ||
path === "/_404.jsx" || path === "/_404.js" path === "/_404.jsx" || path === "/_404.js"
@@ -207,6 +218,7 @@ export class ServerContext {
staticFiles, staticFiles,
renderer, renderer,
middleware, middleware,
app,
notFound, notFound,
error, error,
); );
@@ -268,6 +280,7 @@ export class ServerContext {
: []; : [];
const body = internalRender({ const body = internalRender({
page, page,
app: this.#app,
imports, imports,
preloads, preloads,
renderer: this.#renderer, renderer: this.#renderer,
@@ -465,6 +478,10 @@ const DEFAULT_MIDDLEWARE: Middleware = {
handler: (_, handle) => handle(), handler: (_, handle) => handle(),
}; };
const DEFAULT_APP: AppModule = {
default: ({ Component }) => h(Component, {}),
};
const DEFAULT_NOT_FOUND: UnknownPage = { const DEFAULT_NOT_FOUND: UnknownPage = {
route: "", route: "",
url: "", url: "",
+10 -2
View File
@@ -1,12 +1,20 @@
import { ServerContext } from "./context.ts"; import { ServerContext } from "./context.ts";
import { listenAndServe } from "./deps.ts"; import { listenAndServe } from "./deps.ts";
import { MiddlewareModule, PageModule, RendererModule } from "./types.ts"; import {
AppModule,
MiddlewareModule,
PageModule,
RendererModule,
} from "./types.ts";
export type { Handler, HandlerContext, Handlers } from "./types.ts"; export type { Handler, HandlerContext, Handlers } from "./types.ts";
export { RenderContext } from "./render.tsx"; export { RenderContext } from "./render.tsx";
export type { RenderFn } from "./render.tsx"; export type { RenderFn } from "./render.tsx";
export interface Routes { export interface Routes {
pages: Record<string, PageModule | RendererModule | MiddlewareModule>; pages: Record<
string,
PageModule | RendererModule | MiddlewareModule | AppModule
>;
baseUrl: string; baseUrl: string;
} }
+7 -2
View File
@@ -3,7 +3,7 @@
import { renderToString } from "./deps.ts"; import { renderToString } from "./deps.ts";
import { ComponentChildren, h } from "../runtime/deps.ts"; import { ComponentChildren, h } from "../runtime/deps.ts";
import { DATA_CONTEXT } from "../runtime/hooks.ts"; import { DATA_CONTEXT } from "../runtime/hooks.ts";
import { ErrorPage, Page, Renderer, UnknownPage } from "./types.ts"; import { AppModule, ErrorPage, Page, Renderer, UnknownPage } from "./types.ts";
import { import {
ErrorPageProps, ErrorPageProps,
PageProps, PageProps,
@@ -17,6 +17,7 @@ import { ContentSecurityPolicy } from "../runtime/csp.ts";
export interface RenderOptions { export interface RenderOptions {
page: Page | UnknownPage | ErrorPage; page: Page | UnknownPage | ErrorPage;
app: AppModule;
imports: string[]; imports: string[];
preloads: string[]; preloads: string[];
url: URL; url: URL;
@@ -145,7 +146,11 @@ export async function* render(
value: dataCache, value: dataCache,
children: h(SUSPENSE_CONTEXT.Provider, { children: h(SUSPENSE_CONTEXT.Provider, {
value: suspenseQueue, value: suspenseQueue,
children: h(opts.page.component!, props), children: h(opts.app.default, {
Component() {
return h(opts.page.component!, props);
},
}),
}), }),
}), }),
}), }),
+5
View File
@@ -1,6 +1,7 @@
import { ComponentType } from "../runtime/deps.ts"; import { ComponentType } from "../runtime/deps.ts";
import { router } from "./deps.ts"; import { router } from "./deps.ts";
import { import {
AppProps,
ErrorPageProps, ErrorPageProps,
PageConfig, PageConfig,
PageProps, PageProps,
@@ -106,3 +107,7 @@ export interface Middleware {
handle: () => Promise<Response>, handle: () => Promise<Response>,
): Response | Promise<Response>; ): Response | Promise<Response>;
} }
export interface AppModule {
default: ComponentType<AppProps>;
}
+15
View File
@@ -0,0 +1,15 @@
/** @jsx h */
/** @jsxFrag Fragment */
import { AppProps, Fragment, h, Head } from "../deps.ts";
export default function App(props: AppProps) {
return (
<>
<Head>
<meta name="description" content="Hello world!" />
</Head>
<props.Component />
</>
);
}
+2
View File
@@ -16,6 +16,7 @@ import * as $10 from "./pages/_500.tsx";
import * as $11 from "./pages/intercept.tsx"; import * as $11 from "./pages/intercept.tsx";
import * as $12 from "./pages/_404.tsx"; import * as $12 from "./pages/_404.tsx";
import * as $13 from "./pages/[name].tsx"; import * as $13 from "./pages/[name].tsx";
import * as $14 from "./pages/_app.tsx";
const routes = { const routes = {
pages: { pages: {
@@ -33,6 +34,7 @@ const routes = {
"./pages/intercept.tsx": $11, "./pages/intercept.tsx": $11,
"./pages/_404.tsx": $12, "./pages/_404.tsx": $12,
"./pages/[name].tsx": $13, "./pages/[name].tsx": $13,
"./pages/_app.tsx": $14,
}, },
baseUrl: import.meta.url, baseUrl: import.meta.url,
}; };
+4
View File
@@ -24,6 +24,10 @@ Deno.test("/ page prerender", async () => {
body, body,
`"data":[["home","Hello!"]]`, `"data":[["home","Hello!"]]`,
); );
assertStringIncludes(
body,
`<meta name="description" content="Hello world!" />`,
);
}); });
Deno.test("/props/123 page prerender", async () => { Deno.test("/props/123 page prerender", async () => {