Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PoC]: SPA option for JSX Renderer #1946

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 38 additions & 2 deletions src/middleware/jsx-renderer/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
import type { Context, Renderer } from '../../context'
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { html, raw } from '../../helper/html'
import { jsx, createContext, useContext } from '../../jsx'
import type { FC, JSXNode } from '../../jsx'
import { renderToReadableStream } from '../../jsx/streaming'
import type { Env, Input, MiddlewareHandler } from '../../types'

const SPA_CONTENT_REQUEST_QUERY = '__spa_content'
const SPA_ROOT_ID = '__root'
const SPA_CLIENT_SCRIPT = `async function mountContent(pathname) {
const res = await fetch(pathname + '?${SPA_CONTENT_REQUEST_QUERY}')
const content = await res.text()
const root = document.querySelector('#${SPA_ROOT_ID}')
root.innerHTML = content
}
window.addEventListener(
'click',
(e) => {
if ((e.target).tagName !== 'A') {
return
}
if (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
return
}
const href = (e.target).getAttribute('href')
if (!href.startsWith('/')) {
return
}
e.preventDefault()
window.history.pushState(null, null, href)
mountContent(href)
},
true
)
`

export const RequestContext = createContext<Context | null>(null)

type PropsForRenderer = [...Required<Parameters<Renderer>>] extends [unknown, infer Props]
Expand All @@ -15,11 +43,19 @@
type RendererOptions = {
docType?: boolean | string
stream?: boolean | Record<string, string>
spa?: boolean
}

const createRenderer =
(c: Context, component?: FC<PropsForRenderer>, options?: RendererOptions) =>
(children: JSXNode, props: PropsForRenderer) => {
if (options?.spa) {
if (c.req.query(SPA_CONTENT_REQUEST_QUERY) !== undefined) {
return c.html(children)

Check failure on line 54 in src/middleware/jsx-renderer/index.ts

View workflow job for this annotation

GitHub Actions / Main

No overload matches this call.
}
children = jsx('hono-spa', { id: SPA_ROOT_ID }, children)

Check failure on line 56 in src/middleware/jsx-renderer/index.ts

View workflow job for this annotation

GitHub Actions / Main

Argument of type 'JSXNode' is not assignable to parameter of type 'string | HtmlEscapedString'.
}

const docType =
typeof options?.docType === 'string'
? options.docType
Expand All @@ -31,7 +67,7 @@
RequestContext.Provider,
{ value: c },
(component ? component({ children, ...(props || {}) }) : children) as any
)}`
)}${options?.spa ? raw(`<script>${SPA_CLIENT_SCRIPT}</script>`) : ''}`

if (options?.stream) {
return c.body(renderToReadableStream(body), {
Expand Down