Skip to content

Commit

Permalink
feat: generic <Data> type for onBeforePrerenderStart()
Browse files Browse the repository at this point in the history
  • Loading branch information
lourot committed Dec 26, 2023
1 parent 19bc995 commit 29f265b
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 22 deletions.
5 changes: 4 additions & 1 deletion examples/react-full/pages/hello/+Page.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
export default Page

import React from 'react'
import { useData } from '../../renderer/useData'
import type { Data } from './+data'

function Page({ name }: { name: string }) {
function Page() {
const { name } = useData<Data>()
return (
<>
<h1>Hello</h1>
Expand Down
6 changes: 4 additions & 2 deletions examples/react-full/pages/star-wars/@id/+Page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export default Page

import React from 'react'
import type { MovieDetails } from '../types'
import { useData } from '../../../renderer/useData'
import type { Data } from './+data'

function Page({ movie }: { movie: MovieDetails }) {
function Page() {
const { movie } = useData<Data>()
return (
<>
<h1>{movie.title}</h1>
Expand Down
2 changes: 1 addition & 1 deletion examples/react-full/pages/star-wars/@id/+data.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// https://vike.dev/data
export { data }
export type Data = ReturnType<typeof data>
export type Data = Awaited<ReturnType<typeof data>>

import fetch from 'cross-fetch'
import { filterMovieData } from '../filterMovieData'
Expand Down
6 changes: 4 additions & 2 deletions examples/react-full/pages/star-wars/index/+Page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export default Page

import React from 'react'
import type { Movie } from '../types'
import { useData } from '../../../renderer/useData'
import type { Data } from './+data'

function Page({ movies }: { movies: Movie[] }) {
function Page() {
const { movies } = useData<Data>()
return (
<>
<h1>Star Wars Movies</h1>
Expand Down
1 change: 1 addition & 0 deletions examples/react-full/pages/star-wars/index/+data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// https://vike.dev/data
export { data }
export type Data = Awaited<ReturnType<typeof data>>

import { filterMoviesData, getStarWarsMovies, getTitle } from './getStarWarsMovies'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
export { onBeforePrerenderStart }

import type { OnBeforePrerenderStartAsync } from 'vike/types'
import type { Data as DataMovies } from './+data'
import type { Data as DataMovie } from '../@id/+data'
import { filterMovieData } from '../filterMovieData'
import { filterMoviesData, getStarWarsMovies, getTitle } from './getStarWarsMovies'

const onBeforePrerenderStart: OnBeforePrerenderStartAsync = async (): ReturnType<OnBeforePrerenderStartAsync> => {
const onBeforePrerenderStart: OnBeforePrerenderStartAsync<DataMovie | DataMovies> = async (): ReturnType<
OnBeforePrerenderStartAsync<DataMovie | DataMovies>
> => {
const movies = await getStarWarsMovies()

return [
Expand Down
4 changes: 2 additions & 2 deletions examples/react-full/renderer/+onRenderClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import type { OnRenderClientAsync } from 'vike/types'

let root: ReactDOM.Root
const onRenderClient: OnRenderClientAsync = async (pageContext): ReturnType<OnRenderClientAsync> => {
const { Page, data } = pageContext
const { Page } = pageContext
const page = (
<PageShell pageContext={pageContext}>
<Page {...data} />
<Page />
</PageShell>
)
const container = document.getElementById('page-view')!
Expand Down
4 changes: 2 additions & 2 deletions examples/react-full/renderer/+onRenderHtml.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import { getPageTitle } from './getPageTitle'
import type { OnRenderHtmlAsync } from 'vike/types'

const onRenderHtml: OnRenderHtmlAsync = async (pageContext): ReturnType<OnRenderHtmlAsync> => {
const { Page, data } = pageContext
const { Page } = pageContext

const stream = await renderToStream(
<PageShell pageContext={pageContext}>
<Page {...data} />
<Page />
</PageShell>,
// We don't need react-streaming for this app. (We use it merely to showcase that Vike can handle react-streaming with a pre-rendered app. Note that using react-streaming with pre-rendering can make sense if we want to be able to use React's latest <Suspsense> techniques.)
{ disable: true }
Expand Down
8 changes: 5 additions & 3 deletions examples/react-full/renderer/PageContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ declare global {
namespace Vike {
interface PageContext {
Page: Page
data?: Data
data?: {
// Needed by getPageTitle() and onBeforePrerenderStart()
title?: string
}
config: {
/** Title defined statically by /pages/some-page/+title.js (or by `export default { title }` in /pages/some-page/+config.js) */
title?: string
Expand All @@ -14,8 +17,7 @@ declare global {
}
}

type Page = (data: Data) => React.ReactElement
type Data = Record<string, unknown> & { title?: string }
type Page = () => React.ReactElement

// Tell TypeScript that this file isn't an ambient module
export {}
9 changes: 9 additions & 0 deletions examples/react-full/renderer/useData.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// https://vike.dev/useData
export { useData }

import { usePageContext } from './usePageContext'

function useData<Data>() {
const { data } = usePageContext()
return data as Data
}
12 changes: 4 additions & 8 deletions vike/shared/page-configs/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,24 @@ type GuardSync = (pageContext: PageContextServer) => void
*
* https://vike.dev/onBeforePrerenderStart
*/
type OnBeforePrerenderStartAsync = () => Promise<
type OnBeforePrerenderStartAsync<Data = unknown> = () => Promise<
(
| string
| {
url: string
pageContext: Partial<Vike.PageContext>
pageContext: Partial<Vike.PageContext & { data: Data }>
}
)[]
>
/** Global Hook called before the whole pre-rendering process starts.
*
* https://vike.dev/onBeforePrerenderStart
*/
type OnBeforePrerenderStartSync = () => (
type OnBeforePrerenderStartSync<Data = unknown> = () => (
| string
| {
url: string
pageContext: Partial<Vike.PageContext>
pageContext: Partial<Vike.PageContext & { data: Data }>
}
)[]
/** Hook called before the page is rendered.
Expand Down Expand Up @@ -259,10 +259,6 @@ type RouteAsync = (
type RouteSync = (
pageContext: PageContextServer | PageContextClient
) => { routeParams?: Record<string, string>; precedence?: number } | boolean
/** The page's URL(s).
*
* https://vike.dev/route
*/

// TODO: write docs of links below

Expand Down

0 comments on commit 29f265b

Please sign in to comment.