Skip to content

Commit

Permalink
allow layouts to access context (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
yukukotani committed Nov 29, 2023
1 parent 0c8a688 commit 45ae97c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export const createApp = <E extends Env>(options: ServerOptions<E>): Hono<E> =>
: options.renderToString

const renderContent = async (
c: Context,
innerContent: string | Promise<string> | Node | Promise<Node>,
{ layouts, head, filename, nestedLayouts }: ToWebOptions
) => {
Expand All @@ -116,7 +117,7 @@ export const createApp = <E extends Env>(options: ServerOptions<E>): Hono<E> =>
const layout = NESTED_LAYOUTS[path]
if (layout) {
try {
innerContent = await layout.default({ children: innerContent, head, filename })
innerContent = await layout.default({ children: innerContent, head, filename, c })
} catch (e) {
console.trace(e)
}
Expand All @@ -136,7 +137,7 @@ export const createApp = <E extends Env>(options: ServerOptions<E>): Hono<E> =>

if (defaultLayout) {
try {
innerContent = await defaultLayout.default({ children: innerContent, head, filename })
innerContent = await defaultLayout.default({ children: innerContent, head, filename, c })
} catch (e) {
console.trace(e)
}
Expand Down Expand Up @@ -239,7 +240,7 @@ export const createApp = <E extends Env>(options: ServerOptions<E>): Hono<E> =>
// @ts-ignore
c.setRenderer(async (node, headProps) => {
if (headProps) head.set(headProps)
const content = await renderContent(node, renderOptions)
const content = await renderContent(c, node, renderOptions)
return createResponse(c, content)
})
await next()
Expand Down Expand Up @@ -270,7 +271,7 @@ export const createApp = <E extends Env>(options: ServerOptions<E>): Hono<E> =>
const notFoundHandler = notFound.default as NotFoundHandler

subApp.get('*', async (c) => {
const content = await renderContent(notFoundHandler(c, { head }), renderOptions)
const content = await renderContent(c, notFoundHandler(c, { head }), renderOptions)
c.status(404)
return createResponse(c, content)
})
Expand All @@ -281,7 +282,11 @@ export const createApp = <E extends Env>(options: ServerOptions<E>): Hono<E> =>
subApp.onError((error, c) => {
c.status(500)
return (async () => {
const content = await renderContent(errorHandler(c, { error, head }), renderOptions)
const content = await renderContent(
c,
errorHandler(c, { error, head }),
renderOptions
)
c.status(500)
return createResponse(c, content)
})()
Expand All @@ -308,7 +313,7 @@ export const createApp = <E extends Env>(options: ServerOptions<E>): Hono<E> =>
if (defaultNotFound) {
const notFoundHandler = defaultNotFound.default as unknown as NotFoundHandler<E>
app.notFound(async (c) => {
const content = await renderContent(notFoundHandler(c, { head }), {
const content = await renderContent(c, notFoundHandler(c, { head }), {
head,
filename: NOTFOUND_FILENAME,
})
Expand All @@ -322,7 +327,7 @@ export const createApp = <E extends Env>(options: ServerOptions<E>): Hono<E> =>
const errorHandler = defaultError.default as unknown as ErrorHandler<E>
app.onError((error, c) => {
return (async () => {
const content = await renderContent(errorHandler(c, { error, head }), {
const content = await renderContent(c, errorHandler(c, { error, head }), {
head,
filename: ERROR_FILENAME,
})
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export type LayoutHandler<N = Node> = (props: {
children: N | string
head: Head<N>
filename: string
c: LayoutContext
}) => N | string | Promise<N | string>

/** Function Handler */
Expand All @@ -68,3 +69,5 @@ export type Route<E extends Env = Env, N = Node> = {
}

export type AppRoute<E extends Env = Env> = (app: Hono<E>) => void

export type LayoutContext = Omit<Context | 'render', 'setRenderer'>

0 comments on commit 45ae97c

Please sign in to comment.