-
-
Notifications
You must be signed in to change notification settings - Fork 349
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
feat: on boot draft #1679
base: main
Are you sure you want to change the base?
feat: on boot draft #1679
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,9 @@ import { getPageContextUrlComputed } from '../../shared/getPageContextUrlCompute | |
import { getPageFilesAll } from '../../shared/getPageFiles.js' | ||
import { loadPageRoutes } from '../../shared/route/loadPageRoutes.js' | ||
import { getBaseServer } from './getBaseServer.js' | ||
import { assert, isBaseServer, PromiseType, getGlobalObject, objectAssign } from './utils.js' | ||
import { assert, isBaseServer, PromiseType, getGlobalObject, objectAssign, isObject } from './utils.js' | ||
const globalObject = getGlobalObject<{ | ||
onBootResult?: Record<string, unknown>, | ||
pageFilesData?: PromiseType<ReturnType<typeof getPageFilesAll>> | ||
}>('createPageContext.ts', {}) | ||
|
||
|
@@ -14,15 +15,16 @@ async function createPageContext(urlOriginal: string) { | |
globalObject.pageFilesData = await getPageFilesAll(true) | ||
} | ||
const { pageFilesAll, allPageIds, pageConfigs, pageConfigGlobal } = globalObject.pageFilesData | ||
const { pageRoutes, onBeforeRouteHook } = await loadPageRoutes( | ||
const { pageRoutes, onBeforeRouteHook, onBootHook } = await loadPageRoutes( | ||
pageFilesAll, | ||
pageConfigs, | ||
pageConfigGlobal, | ||
allPageIds | ||
) | ||
const baseServer = getBaseServer() | ||
assert(isBaseServer(baseServer)) | ||
const pageContext = { | ||
|
||
let pageContext = { | ||
urlOriginal, | ||
_objectCreatedByVike: true, | ||
_urlHandler: null, | ||
|
@@ -35,6 +37,18 @@ async function createPageContext(urlOriginal: string) { | |
_pageRoutes: pageRoutes, | ||
_onBeforeRouteHook: onBeforeRouteHook | ||
} | ||
|
||
if (!globalObject.onBootResult && onBootHook) { | ||
globalObject.onBootResult = await onBootHook.hookFn(pageContext) as Record<string, unknown> | ||
} | ||
|
||
if (isObject(globalObject.onBootResult)) { | ||
pageContext = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can use |
||
...pageContext, | ||
...globalObject.onBootResult | ||
} | ||
} | ||
|
||
const pageContextUrlComputed = getPageContextUrlComputed(pageContext) | ||
objectAssign(pageContext, pageContextUrlComputed) | ||
return pageContext | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -189,6 +189,7 @@ const configDefinitionsBuiltIn: ConfigDefinitionsBuiltIn = { | |
} | ||
|
||
type ConfigNameGlobal = | ||
| 'onBoot' | ||
| 'onPrerenderStart' | ||
| 'onBeforeRoute' | ||
| 'prerender' | ||
|
@@ -206,6 +207,9 @@ const configDefinitionsBuiltInGlobal: Record<ConfigNameGlobal, ConfigDefinitionI | |
onBeforeRoute: { | ||
env: { server: true, client: 'if-client-routing', eager: true } | ||
}, | ||
onBoot: { | ||
env: { server: true, client: true, eager: true } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Actually, we need |
||
}, | ||
prerender: { | ||
env: { config: true } | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -149,6 +149,14 @@ async function renderPageAndPrepare( | |
// From now on, renderContext.pageConfigs contains all the configuration data; getVikeConfig() isn't called anymore for this request | ||
} | ||
|
||
if (renderContext.onBootHook) { | ||
const onBootResult = await renderContext.onBootHook?.hookFn(pageContextInit) as Record<string, unknown> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should call |
||
pageContextInit = { | ||
...pageContextInit, | ||
...onBootResult | ||
}; | ||
} | ||
|
||
// Check Base URL | ||
{ | ||
const pageContextHttpResponse = checkBaseUrl(pageContextInit, httpRequestId) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ export type { OnBeforePrerenderStartAsync } | |
export type { OnBeforePrerenderStartSync } | ||
export type { OnBeforeRenderAsync } | ||
export type { OnBeforeRenderSync } | ||
export type { OnBoot } | ||
export type { OnBootSync } | ||
export type { OnBeforeRouteAsync } | ||
export type { OnBeforeRouteSync } | ||
export type { OnHydrationEndAsync } | ||
|
@@ -51,12 +53,12 @@ type HookNamePage = | |
| 'onRenderClient' | ||
| 'guard' | ||
| 'data' | ||
type HookNameGlobal = 'onBeforePrerender' | 'onBeforeRoute' | 'onPrerenderStart' | ||
type HookNameGlobal = 'onBeforePrerender' | 'onBeforeRoute' | 'onPrerenderStart' | 'onBoot' | ||
// v0.4 design TODO/v1-release: remove | ||
type HookNameOldDesign = 'render' | 'prerender' | ||
|
||
type ConfigNameBuiltIn = | ||
| Exclude<keyof Config, keyof ConfigVikeUserProvided | 'onBeforeRoute' | 'onPrerenderStart'> | ||
| Exclude<keyof Config, keyof ConfigVikeUserProvided | 'onBeforeRoute' | 'onPrerenderStart' | 'onBoot'> | ||
| 'prerender' | ||
| 'clientEntryLoaded' | ||
| 'onBeforeRenderEnv' | ||
|
@@ -131,6 +133,18 @@ type OnBeforeRenderAsync = ( | |
* https://vike.dev/onBeforeRender | ||
*/ | ||
type OnBeforeRenderSync = (pageContext: PageContextServer) => { pageContext: Partial<Vike.PageContext> } | void | ||
/** Hook called when Vike is first initialized. | ||
* | ||
* https://vike.dev/onBoot | ||
*/ | ||
type OnBoot = ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's name it |
||
pageContext: PageContextServer | ||
) => Promise<{ pageContext: Partial<Vike.PageContext> } | void> | ||
/** Hook called when Vike is first initialized. | ||
* | ||
* https://vike.dev/onBoot | ||
*/ | ||
type OnBootSync = (pageContext: PageContextServer) => { pageContext: Partial<Vike.PageContext> } | void | ||
/** Hook called before the URL is routed to a page. | ||
* | ||
* https://vike.dev/onBeforeRoute | ||
|
@@ -349,6 +363,12 @@ type ConfigBuiltIn = { | |
*/ | ||
onBeforePrerenderStart?: OnBeforePrerenderStartAsync | OnBeforePrerenderStartSync | ImportString | ||
|
||
/** Hook called when Vike is first initialized. | ||
* | ||
* https://vike.dev/onBoot | ||
*/ | ||
onBoot?: OnBoot | OnBootSync | ImportString | ||
|
||
/** Hook called before the URL is routed to a page. | ||
* | ||
* https://vike.dev/onBeforeRoute | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,11 +30,11 @@ async function loadPageRoutes( | |
pageConfigs: PageConfigRuntime[], | ||
pageConfigGlobal: PageConfigGlobalRuntime, | ||
allPageIds: string[] | ||
): Promise<{ pageRoutes: PageRoutes; onBeforeRouteHook: null | Hook }> { | ||
): Promise<{ pageRoutes: PageRoutes; onBeforeRouteHook: null | Hook; onBootHook: null | Hook }> { | ||
await Promise.all(pageFilesAll.filter((p) => p.fileType === '.page.route').map((p) => p.loadFile?.())) | ||
const { onBeforeRouteHook, filesystemRoots } = getGlobalHooks(pageFilesAll, pageConfigs, pageConfigGlobal) | ||
const { onBootHook, onBeforeRouteHook, filesystemRoots } = getGlobalHooks(pageFilesAll, pageConfigs, pageConfigGlobal) | ||
const pageRoutes = getPageRoutes(filesystemRoots, pageFilesAll, pageConfigs, allPageIds) | ||
return { pageRoutes, onBeforeRouteHook } | ||
return { pageRoutes, onBeforeRouteHook, onBootHook } | ||
} | ||
|
||
function getPageRoutes( | ||
|
@@ -175,12 +175,14 @@ function getGlobalHooks( | |
pageConfigGlobal: PageConfigGlobalRuntime | ||
): { | ||
onBeforeRouteHook: null | Hook | ||
onBootHook: null | Hook | ||
filesystemRoots: null | FilesystemRoot[] | ||
} { | ||
// V1 Design | ||
if (pageConfigs.length > 0) { | ||
const hook = getHookFromPageConfigGlobal(pageConfigGlobal, 'onBeforeRoute') | ||
return { onBeforeRouteHook: hook, filesystemRoots: null } | ||
const onBootHook = getHookFromPageConfigGlobal(pageConfigGlobal, 'onBoot') | ||
const onBeforeRouteHook = getHookFromPageConfigGlobal(pageConfigGlobal, 'onBeforeRoute') | ||
return { onBootHook, onBeforeRouteHook, filesystemRoots: null } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a good question whether Maybe we can make
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm actually leaning more towards making The thing is that, if we don't make it global, then we have to resolve routing before calling If a user wants a per-page hook, then he can use WDYT? |
||
} | ||
|
||
// Old design | ||
|
@@ -221,7 +223,7 @@ function getGlobalHooks( | |
} | ||
}) | ||
|
||
return { onBeforeRouteHook, filesystemRoots } | ||
return { onBootHook: null, onBeforeRouteHook, filesystemRoots } | ||
} | ||
|
||
function dirname(filePath: string): string { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this goes in the right direction: we want to call
onBoot()
only once and cache its result "forever".