Skip to content

Commit

Permalink
minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Aug 30, 2024
1 parent d0ba07e commit 38f594a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
18 changes: 8 additions & 10 deletions packages/vike-vue/src/integration/onRenderHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@ import type { PageContextInternal } from '../types/PageContext.js'
const onRenderHtml: OnRenderHtmlAsync = async (
pageContext: PageContextServer & PageContextInternal,
): ReturnType<OnRenderHtmlAsync> => {
const { pageHtml, fromHtmlRenderer, ssrContext } = await getPageHtml(pageContext)
const { pageHtml, fromHtmlRenderer } = await getPageHtml(pageContext)

pageContext.isRenderingHead = true
const headHtml = await getHeadHtml(pageContext)
pageContext.isRenderingHead = false

const { bodyHtmlBegin, bodyHtmlEnd } = await getBodyHtmlBeginEnd(pageContext, ssrContext)
const { bodyHtmlBegin, bodyHtmlEnd } = await getBodyHtmlBoundary(pageContext)

const { htmlAttributesString, bodyAttributesString } = getTagAttributes(pageContext)

// Not needed on the client-side, thus we remove it to save KBs sent to the client
delete pageContext._configFromHook

const documentHtml = escapeInject`<!DOCTYPE html>
<html${dangerouslySkipEscape(htmlAttributesString)}>
<head>
Expand All @@ -36,12 +39,8 @@ const onRenderHtml: OnRenderHtmlAsync = async (
<div id="app">${pageHtml}</div>
${bodyHtmlEnd}
</body>
<!-- built with https://github.com/vikejs/vike-vue -->
</html>`

// Not needed on the client-side, thus we remove it to save KBs sent to the client
delete pageContext._configFromHook

return {
documentHtml,
pageContext: {
Expand Down Expand Up @@ -95,7 +94,7 @@ async function getPageHtml(pageContext: PageContextServer) {
pageContext.ssrContext = ssrContext
Object.assign(fromHtmlRenderer, ...afterRenderResults)
}
return { pageHtml, fromHtmlRenderer, ssrContext }
return { pageHtml, fromHtmlRenderer }
}

async function getHeadHtml(pageContext: PageContextServer & PageContextInternal) {
Expand Down Expand Up @@ -131,14 +130,13 @@ async function getHeadHtml(pageContext: PageContextServer & PageContextInternal)
return headHtml
}

async function getBodyHtmlBeginEnd(pageContext: PageContextServer, ssrContext: SSRContext) {
async function getBodyHtmlBoundary(pageContext: PageContextServer) {
const bodyHtmlBegin = dangerouslySkipEscape(
(await callCumulativeHooks(pageContext.config.bodyHtmlBegin, pageContext)).join(''),
)

// we define this hook here so that it doesn't need to be exported by vike-vue
const defaultTeleport = `<div id="teleported">${ssrContext.teleports?.['#teleported'] ?? ''}</div>`

const defaultTeleport = `<div id="teleported">${pageContext.ssrContext!.teleports?.['#teleported'] ?? ''}</div>`
const bodyHtmlEndHooks = [defaultTeleport, ...(pageContext.config.bodyHtmlEnd ?? [])]
const bodyHtmlEnd = dangerouslySkipEscape((await callCumulativeHooks(bodyHtmlEndHooks, pageContext)).join(''))

Expand Down
10 changes: 5 additions & 5 deletions packages/vike-vue/src/types/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ declare global {
*
* https://vike.dev/bodyHtmlBegin
*/
bodyHtmlBegin?: BodyInjectHtml
bodyHtmlBegin?: BodyHtmlBoundary

/**
* Raw HTML injected at the end of `<body>`.
Expand All @@ -167,7 +167,7 @@ declare global {
*
* https://vike.dev/bodyHtmlEnd
*/
bodyHtmlEnd?: BodyInjectHtml
bodyHtmlEnd?: BodyHtmlBoundary

/**
* Hook called right after creating Vue's `app` instance.
Expand Down Expand Up @@ -215,8 +215,8 @@ declare global {
onAfterRenderHtml?: Array<OnAfterRenderHtmlSync | OnAfterRenderHtmlAsync>
onBeforeRenderClient?: Array<OnBeforeRenderClientSync | OnBeforeRenderClientAsync>
onAfterRenderClient?: Function[]
bodyHtmlBegin?: BodyInjectHtml[]
bodyHtmlEnd?: BodyInjectHtml[]
bodyHtmlBegin?: BodyHtmlBoundary[]
bodyHtmlEnd?: BodyHtmlBoundary[]
Layout?: Component[]
Head?: Component[]
bodyAttributes?: TagAttributes[]
Expand All @@ -232,7 +232,7 @@ export type __FakeExport_Config = never
// - https://github.com/Microsoft/TypeScript/issues/983
type PageContext_ = PageContext

type BodyInjectHtml = string | ((pageContext: PageContext) => string)
type BodyHtmlBoundary = string | ((pageContext: PageContext) => string)

// JSDocs are preserved
type PickWithoutGetter<T, K extends keyof T> = {
Expand Down

0 comments on commit 38f594a

Please sign in to comment.