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<
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. */
url: URL;
@@ -25,6 +27,10 @@ export interface PageProps<
renderData?: T;
}
export interface AppProps {
Component: ComponentType<Record<never, never>>;
}
export interface UnknownPageProps {
/** The URL of the request that resulted in this page being rendered. */
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 { BUILD_ID } from "./constants.ts";
import {
AppModule,
ErrorPage,
ErrorPageModule,
Handler,
@@ -30,6 +31,7 @@ import {
ContentSecurityPolicyDirectives,
SELF,
} from "../runtime/csp.ts";
import { h } from "../runtime/deps.ts";
interface StaticFile {
/** The URL to the static file on disk. */
@@ -49,6 +51,7 @@ export class ServerContext {
#bundler: Bundler;
#renderer: Renderer;
#middleware: Middleware;
#app: AppModule;
#notFound: UnknownPage;
#error: ErrorPage;
@@ -57,6 +60,7 @@ export class ServerContext {
staticFiles: StaticFile[],
renderer: Renderer,
middleware: Middleware,
app: AppModule,
notFound: UnknownPage,
error: ErrorPage,
) {
@@ -64,6 +68,7 @@ export class ServerContext {
this.#staticFiles = staticFiles;
this.#renderer = renderer;
this.#middleware = middleware;
this.#app = app;
this.#notFound = notFound;
this.#error = error;
this.#bundler = new Bundler(pages);
@@ -81,6 +86,7 @@ export class ServerContext {
const pages: Page[] = [];
let renderer: Renderer = DEFAULT_RENDERER;
let middleware: Middleware = DEFAULT_MIDDLEWARE;
let app: AppModule = DEFAULT_APP;
let notFound: UnknownPage = DEFAULT_NOT_FOUND;
let error: ErrorPage = DEFAULT_ERROR;
for (const [self, module] of Object.entries(routes.pages)) {
@@ -125,6 +131,11 @@ export class ServerContext {
path === "/_middleware.jsx" || path === "/_middleware.js"
) {
middleware = module as MiddlewareModule;
} else if (
path === "/_app.tsx" || path === "/_app.ts" ||
path === "/_app.jsx" || path === "/_app.js"
) {
app = module as AppModule;
} else if (
path === "/_404.tsx" || path === "/_404.ts" ||
path === "/_404.jsx" || path === "/_404.js"
@@ -207,6 +218,7 @@ export class ServerContext {
staticFiles,
renderer,
middleware,
app,
notFound,
error,
);
@@ -268,6 +280,7 @@ export class ServerContext {
: [];
const body = internalRender({
page,
app: this.#app,
imports,
preloads,
renderer: this.#renderer,
@@ -465,6 +478,10 @@ const DEFAULT_MIDDLEWARE: Middleware = {
handler: (_, handle) => handle(),
};
const DEFAULT_APP: AppModule = {
default: ({ Component }) => h(Component, {}),
};
const DEFAULT_NOT_FOUND: UnknownPage = {
route: "",
url: "",
+10 -2
View File
@@ -1,12 +1,20 @@
import { ServerContext } from "./context.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 { RenderContext } from "./render.tsx";
export type { RenderFn } from "./render.tsx";
export interface Routes {
pages: Record<string, PageModule | RendererModule | MiddlewareModule>;
pages: Record<
string,
PageModule | RendererModule | MiddlewareModule | AppModule
>;
baseUrl: string;
}
+7 -2
View File
@@ -3,7 +3,7 @@
import { renderToString } from "./deps.ts";
import { ComponentChildren, h } from "../runtime/deps.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 {
ErrorPageProps,
PageProps,
@@ -17,6 +17,7 @@ import { ContentSecurityPolicy } from "../runtime/csp.ts";
export interface RenderOptions {
page: Page | UnknownPage | ErrorPage;
app: AppModule;
imports: string[];
preloads: string[];
url: URL;
@@ -145,7 +146,11 @@ export async function* render(
value: dataCache,
children: h(SUSPENSE_CONTEXT.Provider, {
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 { router } from "./deps.ts";
import {
AppProps,
ErrorPageProps,
PageConfig,
PageProps,
@@ -106,3 +107,7 @@ export interface Middleware {
handle: () => 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 $12 from "./pages/_404.tsx";
import * as $13 from "./pages/[name].tsx";
import * as $14 from "./pages/_app.tsx";
const routes = {
pages: {
@@ -33,6 +34,7 @@ const routes = {
"./pages/intercept.tsx": $11,
"./pages/_404.tsx": $12,
"./pages/[name].tsx": $13,
"./pages/_app.tsx": $14,
},
baseUrl: import.meta.url,
};
+4
View File
@@ -24,6 +24,10 @@ Deno.test("/ page prerender", async () => {
body,
`"data":[["home","Hello!"]]`,
);
assertStringIncludes(
body,
`<meta name="description" content="Hello world!" />`,
);
});
Deno.test("/props/123 page prerender", async () => {