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

qual: make config hook types more precise #1137

Merged
merged 46 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
aaecd95
qual: make config hook types more precise
lourot Sep 27, 2023
7fbda5b
qual: fix type
lourot Sep 28, 2023
ed6e4d5
qual: fix CI failure
lourot Sep 28, 2023
22968d2
qual: use more precise types for onBeforeRender
lourot Sep 28, 2023
8bde0ef
qual: support `server-and-client` in onBeforeRender's type
lourot Sep 29, 2023
3591539
qual: make config hook types more precise
lourot Sep 29, 2023
9ddafd1
qual: fix types
lourot Sep 29, 2023
fb693ef
qual: use interface merging
lourot Sep 30, 2023
50ffcff
qual: improve onBeforeRender's and onRenderHtml's types
lourot Sep 30, 2023
fd80d7e
qual: improve onRenderClient's type
lourot Sep 30, 2023
fe791d4
qual: add type to onPrerenderStart()
lourot Oct 2, 2023
7c6f0ce
qual: add type to onBeforePrerenderStart()
lourot Oct 2, 2023
ced4d81
qual: add type to onBeforeRoute()
lourot Oct 2, 2023
7d9739b
qual: add types to client-routing-related hooks
lourot Oct 2, 2023
664fdba
qual: add type to route() hook
lourot Oct 2, 2023
17c3c06
qual: add type to guard() hook
lourot Oct 2, 2023
c527972
Merge branch 'main' into quality/config-hook-types
lourot Oct 2, 2023
98cf3f5
experiment: make defining hook types less tedious
brillout Oct 4, 2023
c822d92
qual: add hook-related type utilities
lourot Oct 4, 2023
260b1c7
qual: some hooks run on client too
lourot Oct 4, 2023
9541993
qual: rename hook-related type utilities
lourot Oct 5, 2023
ffb5061
Merge branch 'main' into quality/config-hook-types
lourot Oct 5, 2023
2213c79
experiment: start replacing hook-related utilities
lourot Oct 5, 2023
907d6cd
qual: improve `OnBeforeRender{Param,Return}`
brillout Oct 6, 2023
070c6dc
minor fix
brillout Oct 6, 2023
bfc5454
typo
brillout Oct 6, 2023
4a8e8c2
workaround expanded `OnBeforeRenderParam` quickinfo
brillout Oct 6, 2023
b4aad1c
polish `OnBeforeRenderParam` quickinfo
brillout Oct 6, 2023
400754a
qual: remove hook-related type utilities
lourot Oct 9, 2023
f2ec222
Merge branch 'main' into quality/config-hook-types
lourot Oct 9, 2023
7bc32ed
simplify hook type
brillout Oct 10, 2023
70eb05f
Merge remote-tracking branch 'origin/main' into quality/config-hook-t…
brillout Oct 11, 2023
b577406
use hoisted named export
brillout Oct 11, 2023
7055f5b
fix hook imports
brillout Oct 11, 2023
6912ebb
Merge branch 'main' into quality/config-hook-types
brillout Oct 12, 2023
b04435d
qual: simplify hook types
lourot Oct 12, 2023
74e7cd0
qual: remove now-useless imports
lourot Oct 12, 2023
5b4e15c
experiment: stop supporting sync variant for void-returning hooks
lourot Oct 12, 2023
35efab5
Revert "experiment: stop supporting sync variant for void-returning h…
lourot Oct 12, 2023
fc31669
qual: harmonize hook types
lourot Oct 12, 2023
25b9ce1
qual: remove redundant param types
lourot Oct 12, 2023
3b2971f
suggestion: add link to docs instead of comment. That's how I've docu…
brillout Oct 12, 2023
91b3653
suggestion: add @depreated to RouteAsync and provide Route type alias
brillout Oct 12, 2023
da76690
add argument to `OnHydrationEnd{Async,Sync}`
brillout Oct 12, 2023
9b7302b
suggestion: assume user is using Client Routing (fairly safe assumpti…
brillout Oct 12, 2023
fb51afa
Merge branch 'main' into quality/config-hook-types
brillout Oct 12, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"typescript": "^5.0.2",
"vike-react": "0.2.0-commit-af9b3c5",
"vike-react": "0.3.0",
"vite": "^4.0.3",
"vike": "0.4.142"
},
Expand Down
7 changes: 4 additions & 3 deletions examples/path-aliases-v1/renderer/onRenderHtml.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import ReactDOMServer from 'react-dom/server'
import React from 'react'
import { escapeInject, dangerouslySkipEscape } from 'vike/server'
import { PageLayout } from './PageLayout'
import type { PageContext } from '#root/types'
import { ReactComponent } from '#root/types'
import type { PageContextServer } from 'vike/types'

function onRenderHtml(pageContext: PageContext) {
const { Page } = pageContext
function onRenderHtml(pageContext: PageContextServer) {
const Page = pageContext.Page as ReactComponent
lourot marked this conversation as resolved.
Show resolved Hide resolved
const pageHtml = ReactDOMServer.renderToString(
<PageLayout>
<Page />
Expand Down
9 changes: 6 additions & 3 deletions examples/react-full-v1/pages/hello/+onBeforeRender.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
export default onBeforeRender
// https://vike.dev/onBeforeRender

import type { PageContextBuiltInServer } from 'vike/types'
import type { Config, PageContextServer } from 'vike/types'
import { render } from 'vike/abort'

import { names } from './names'

async function onBeforeRender(pageContext: PageContextBuiltInServer) {
const onBeforeRender: Config['onBeforeRender'] = async (
pageContext: PageContextServer
): Promise<{ pageContext: Partial<Vike.PageContext> }> => {
const { name } = pageContext.routeParams
if (name !== 'anonymous' && !names.includes(name)) {
throw render(404, `Unknown name: ${name}.`)
Expand All @@ -17,3 +19,4 @@ async function onBeforeRender(pageContext: PageContextBuiltInServer) {
}
}
}
export default onBeforeRender
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// https://vike.dev/onBeforeRender
export default onBeforeRender

import fetch from 'cross-fetch'
import { filterMovieData } from '../filterMovieData'
import type { PageContextBuiltInServer } from 'vike/types'
import type { Config, PageContextServer } from 'vike/types'
import type { MovieDetails } from '../types'
import { render } from 'vike/abort'
import React from 'react'

async function onBeforeRender(pageContext: PageContextBuiltInServer) {
const onBeforeRender: Config['onBeforeRender'] = async (
pageContext: PageContextServer
): Promise<{ pageContext: Partial<Vike.PageContext> }> => {
const dataUrl = `https://star-wars.brillout.com/api/films/${pageContext.routeParams.id}.json`
let movie: MovieDetails
try {
Expand Down Expand Up @@ -44,3 +45,4 @@ async function onBeforeRender(pageContext: PageContextBuiltInServer) {
}
}
}
export default onBeforeRender
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// https://vike.dev/onBeforeRender
export default onBeforeRender

import type { Config, PageContextServer } from 'vike/types'
import { filterMoviesData, getStarWarsMovies, getTitle } from './getStarWarsMovies'

async function onBeforeRender() {
const onBeforeRender: Config['onBeforeRender'] = async (
pageContext: PageContextServer
): Promise<{ pageContext: Partial<Vike.PageContext> }> => {
await sleep(700) // Simulate slow network
const movies = await getStarWarsMovies()
return {
Expand All @@ -18,6 +20,7 @@ async function onBeforeRender() {
}
}
}
export default onBeforeRender

function sleep(milliseconds: number): Promise<void> {
return new Promise((r) => setTimeout(r, milliseconds))
Expand Down
11 changes: 7 additions & 4 deletions examples/react-full-v1/renderer/+onRenderHtml.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// https://vike.dev/onRenderHtml
export default onRenderHtml

import { renderToStream } from 'react-streaming/server'
import React from 'react'
import { escapeInject } from 'vike/server'
import { PageShell } from './PageShell'
import { getPageTitle } from './getPageTitle'
import type { PageContextServer } from 'vike/types'
import type { Config, DocumentHtml, PageContextServer } from 'vike/types'

async function onRenderHtml(pageContext: PageContextServer) {
const onRenderHtml: Config['onRenderHtml'] = async (
pageContext: PageContextServer
): Promise<{ documentHtml: DocumentHtml; pageContext: Partial<Vike.PageContext> | Function }> => {
const { Page, pageProps } = pageContext

const stream = await renderToStream(
Expand All @@ -34,10 +34,13 @@ async function onRenderHtml(pageContext: PageContextServer) {
return {
documentHtml,
// See https://vike.dev/stream#initial-data-after-stream-end
// temp(aurelien): interesting that this can be an async function. Can this be the case in
// onBeforeRender() as well? Or is this specific to onRenderHtml()?
lourot marked this conversation as resolved.
Show resolved Hide resolved
pageContext: async () => {
return {
someAsyncProps: 42
}
}
}
}
export default onRenderHtml
51 changes: 12 additions & 39 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 17 additions & 2 deletions vike/shared/page-configs/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ export type { HookName }

import type { PrefetchStaticAssets } from '../../client/client-routing-runtime/prefetch/getPrefetchSettings.js'
import type { ConfigDefinition } from '../../node/plugin/plugins/importUserCode/v1-design/getVikeConfig/configDefinitionsBuiltIn.js'
import type { DocumentHtml } from '../../node/runtime/html/renderHtml.js'
import type { ConfigVikeUserProvided } from '../ConfigVike.js'
import type { Vike, VikePackages } from '../VikeNamespace.js'
import type { PageContext, PageContextClient, PageContextServer } from '../types.js'

type HookName =
| 'onHydrationEnd'
Expand Down Expand Up @@ -39,6 +41,8 @@ type Config = ConfigBuiltIn &
| VikePackages.ConfigVikeSvelte
)

type OptionalPromise<T> = T | Promise<T>

// TODO: write docs of links below

/** Page configuration.
Expand Down Expand Up @@ -78,7 +82,12 @@ type ConfigBuiltIn = {
*
* https://vike.dev/onBeforeRender
*/
onBeforeRender?: Function | ImportString | null
onBeforeRender?:
| ((pageContext: PageContextServer) => OptionalPromise<{ pageContext: Partial<Vike.PageContext> }>)
| ((pageContext: PageContextClient) => OptionalPromise<{ pageContext: Partial<Vike.PageContext> }>)
| ((pageContext: PageContext) => OptionalPromise<{ pageContext: Partial<Vike.PageContext> }>)
| ImportString
| null
lourot marked this conversation as resolved.
Show resolved Hide resolved

/** Determines what pageContext properties are sent to the client-side.
*
Expand All @@ -95,7 +104,13 @@ type ConfigBuiltIn = {
*
* https://vike.dev/onRenderHtml
*/
onRenderHtml?: Function | ImportString
onRenderHtml?:
| ((
pageContext: PageContextServer
) => OptionalPromise<
DocumentHtml | { documentHtml: DocumentHtml; pageContext?: Partial<Vike.PageContext> | Function }
>)
| ImportString

/** Enable async Route Functions.
*
Expand Down
1 change: 1 addition & 0 deletions vike/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type {
export type { ConfigEntries } from '../shared/getPageFiles/getExports.js'

export type { InjectFilterEntry } from '../node/runtime/html/injectAssets/getHtmlTags.js'
export type { DocumentHtml } from '../node/runtime/html/renderHtml.js'

// TODO/v1-release: remove
import type { ConfigEnv } from '../shared/page-configs/PageConfig.js'
Expand Down