Skip to content

Commit

Permalink
fix stylesheet from styled components
Browse files Browse the repository at this point in the history
  • Loading branch information
mdlucas committed Jan 3, 2024
1 parent e5258cd commit 512ed9f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 54 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
"useData": [
"./dist/components/useData.d.ts"
],
"ClientOnly": [
"./dist/components/ClientOnly.d.js"
],
"renderer/onRenderHtml": [
"./dist/renderer/onRenderHtml.d.ts"
],
Expand Down
47 changes: 11 additions & 36 deletions src/components/ClientOnly.tsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,17 @@
export { ClientOnly }
import React from 'react'
import { FC, ReactElement, ReactNode, useEffect, useState } from 'react'

import React, { lazy, useEffect, useState, startTransition } from 'react'
import type { ComponentType, ReactNode } from 'react'

function ClientOnly<T>({
load,
children,
fallback,
deps = []
}: {
load: () => Promise<{ default: React.ComponentType<T> } | React.ComponentType<T>>
children: (Component: React.ComponentType<T>) => ReactNode
fallback: ReactNode
deps?: Parameters<typeof useEffect>[1]
}) {
const [Component, setComponent] = useState<ComponentType<unknown> | null>(null)
type Props = {
children: ReactNode
}
export const ClientOnly: FC<Props> = ({ children }): ReactElement<any, any> | null => {
const [hasMounted, setHasMounted] = useState(false)

useEffect(() => {
const loadComponent = () => {
const Component = lazy(() =>
load()
.then((LoadedComponent) => {
return {
default: () => children('default' in LoadedComponent ? LoadedComponent.default : LoadedComponent)
}
})
.catch((error) => {
console.error('Component loading failed:', error)
return { default: () => <p>Error loading component.</p> }
})
)
setComponent(Component)
}
setHasMounted(true)
}, [])

startTransition(() => {
loadComponent()
})
}, deps)
if (!hasMounted) return null

return Component ? <Component /> : fallback
return <>{children}</>
}
9 changes: 2 additions & 7 deletions src/renderer/+config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const toggleSsrRelatedConfig: ConfigEffect = ({ configDefinedAt, configValue })
}

export default {
onRenderHtml: 'import:vike-builders/renderer/onRenderHtml:onRenderHtml',
onRenderClient: 'import:vike-builders/renderer/onRenderClient:onRenderClient',
onRenderHtml: 'import:@platformbuilders/vike-builders/renderer/onRenderHtml:onRenderHtml',
onRenderClient: 'import:@platformbuilders/vike-builders/renderer/onRenderClient:onRenderClient',
passToClient: ['pageProps', 'title'],
clientRouting: false,
hydrationCanBeAborted: true,
Expand All @@ -38,9 +38,6 @@ export default {
description: {
env: { server: true }
},
theme: {
env: { server: true , client: true}
},
favicon: {
env: { server: true }
},
Expand Down Expand Up @@ -83,8 +80,6 @@ declare global {
*
*/
lang?: string

theme?: any
/**
* If true, render mode is SSR or pre-rendering (aka SSG). In other words, the
* page's HTML will be rendered at build-time or request-time.
Expand Down
5 changes: 1 addition & 4 deletions src/renderer/getPageElement.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import React from 'react'
import { ThemeProvider } from 'styled-components'
import type { PageContext } from 'vike/types'
import { themeFormatter } from '@platformbuilders/theme-toolkit'
import { PageContextProvider } from './PageContextProvider.js'

function PassThrough({ children }: any) {
return <>{children}</>
}
export function PageShell({ children, pageContext }: { children: React.ReactNode; pageContext: PageContext }) {
const Layout = pageContext.config.Layout || PassThrough
const themeJson = themeFormatter(pageContext.config.theme) ?? {}
return (
<PageContextProvider pageContext={pageContext}>
<Layout theme={themeJson}>{children}</Layout>
<Layout>{children}</Layout>
</PageContextProvider>
)
}
29 changes: 22 additions & 7 deletions src/renderer/onRenderHtml.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,35 @@ import { ServerStyleSheet } from 'styled-components'
import { dangerouslySkipEscape, escapeInject } from 'vike/server'
import type { OnRenderHtmlAsync } from 'vike/types'
import { PageShell } from './getPageElement.js'
import { getTitle } from './getTitle.js'
import { PageContextProvider } from './PageContextProvider.js'
import React from 'react'

const onRenderHtml: OnRenderHtmlAsync = async (pageContext): ReturnType<OnRenderHtmlAsync> => {
const { description, favicon } = pageContext.config
const faviconTag = !favicon ? '' : escapeInject`<link rel="icon" href="${favicon}" />`
const descriptionTag = !description ? '' : escapeInject`<meta name="description" content="${description}" />`

const title = getTitle(pageContext)
const titleTag = !title ? '' : escapeInject`<title>${title}</title>`

const Head = pageContext.config.Head || (() => <></>)
const head = (
<PageContextProvider pageContext={pageContext}>
<Head />
</PageContextProvider>
)

const headHtml = dangerouslySkipEscape(ReactDOMServer.renderToString(head))

const sheet = new ServerStyleSheet()
let pageHtml
let test = 't'
if (!pageContext.Page) {
// SPA
pageHtml = ''
} else {
// SSR / HTML-only
const { Page, data } = pageContext
test = 'passou aqui'
pageHtml = ReactDOMServer.renderToString(
sheet.collectStyles(
<PageShell pageContext={pageContext}>
Expand All @@ -25,22 +41,21 @@ const onRenderHtml: OnRenderHtmlAsync = async (pageContext): ReturnType<OnRender
)
}
// See https://vike.dev/head
const title = String(pageContext.exports.title) || 'Vite SSR app'
const desc = String(pageContext.exports.description) || 'App using Vite + Vike'

const documentHtml = escapeInject`<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8" />
${faviconTag}
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="${desc}" />
<title>${title}</title>
${titleTag}
${descriptionTag}
${headHtml}
${dangerouslySkipEscape(sheet.getStyleTags())}
</head>
<body>
<div>${test}</div>
<div id="page-view">${dangerouslySkipEscape(pageHtml)}</div>
</body>
</html>`
Expand Down

0 comments on commit 512ed9f

Please sign in to comment.