Skip to content

Commit

Permalink
fix: use Vike.PageContext
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Sep 22, 2023
1 parent 812d3f1 commit ed86389
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 91 deletions.
93 changes: 29 additions & 64 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions vike-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"react": "18.x.x",
"react-dom": "18.x.x",
"vite": "^4.3.8",
"vite-plugin-ssr": "^0.4.141"
"vite-plugin-ssr": "^0.4.142"
},
"devDependencies": {
"@brillout/release-me": "^0.1.7",
Expand All @@ -30,7 +30,8 @@
"@types/react-dom": "^18.0.3",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"typescript": "^5.0.2"
"typescript": "^5.0.2",
"vite-plugin-ssr": "^0.4.142"
},
"typesVersions": {
"*": {
Expand Down
4 changes: 3 additions & 1 deletion vike-react/renderer/+config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ export default {
import type { Component } from './types'
declare global {
namespace VikePackages {
export interface ConfigVikeReact {
interface ConfigVikeReact {
/** The page's root React component */
Page?: Component
/** React element rendered and appended into <head></head> */
Head?: Component
/** A component, usually common to several pages, that wraps the root component `Page` */
Expand Down
2 changes: 1 addition & 1 deletion vike-react/renderer/PageContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ export { PageContextProvider }
export { usePageContext }

import React, { useContext } from 'react'
import { PageContext } from './types'
import { getGlobalObject } from './utils/getGlobalObject.js'
import type { PageContext } from 'vite-plugin-ssr/types'

const { Context } = getGlobalObject('PageContextProvider.ts', {
Context: React.createContext<PageContext>(undefined as never)
Expand Down
2 changes: 1 addition & 1 deletion vike-react/renderer/getPageElement.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export { getPageElement }

import type { PageContext } from './types'
import React from 'react'
import { PageContextProvider } from './PageContextProvider.js'
import type { PageContext } from 'vite-plugin-ssr/types'

function getPageElement(pageContext: PageContext): JSX.Element {
const Layout = pageContext.config.Layout ?? PassThrough
Expand Down
2 changes: 1 addition & 1 deletion vike-react/renderer/onRenderClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export default onRenderClient

import ReactDOM from 'react-dom/client'
import { getTitle } from './getTitle.js'
import type { PageContextClient } from './types'
import type { PageContextClient } from 'vite-plugin-ssr/types'
import { getPageElement } from './getPageElement.js'

let root: ReactDOM.Root
Expand Down
4 changes: 2 additions & 2 deletions vike-react/renderer/onRenderHtml.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ export default onRenderHtml

import { renderToString } from 'react-dom/server'
import { escapeInject, dangerouslySkipEscape } from 'vite-plugin-ssr/server'
import type { PageContext } from 'vite-plugin-ssr/types'
import { getTitle } from './getTitle.js'
import { getPageElement } from './getPageElement.js'
import type { PageContextServer } from './types'
import { PageContextProvider } from './PageContextProvider.js'
import React from 'react'

async function onRenderHtml(pageContext: PageContextServer) {
async function onRenderHtml(pageContext: PageContext) {
let pageHtml = ''
if (!!pageContext.Page) {
const page = getPageElement(pageContext)
Expand Down
26 changes: 7 additions & 19 deletions vike-react/renderer/types.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
export type { PageContextServer }
export type { PageContextClient }
export type { PageContext }
export type { PageProps }
export type { Page }
export type { Component }

import type {
PageContextBuiltInServer,
PageContextBuiltInClientWithClientRouting as PageContextBuiltInClient
} from 'vite-plugin-ssr/types'
import type { ReactElement } from 'react'

// type Component = (props: Record<string, unknown>) => ReactElement
type Component = (props: any) => ReactElement

type Page = (pageProps: PageProps) => ReactElement
type PageProps = Record<string, unknown>

export type PageContextCommon = {
Page: Page
pageProps?: PageProps
declare global {
namespace Vike {
interface PageContext {
Page: Component
pageProps: Record<string, unknown>
}
}
}

type PageContextServer = PageContextBuiltInServer<Page> & PageContextCommon
type PageContextClient = PageContextBuiltInClient<Page> & PageContextCommon
type PageContext = PageContextClient | PageContextServer

0 comments on commit ed86389

Please sign in to comment.