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

feat: on boot draft #1679

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

feat: on boot draft #1679

wants to merge 3 commits into from

Conversation

OlaviSau
Copy link
Contributor

@OlaviSau OlaviSau commented Jun 8, 2024

No description provided.

const globalObject = getGlobalObject<{
onBootResult?: Record<string, unknown>,
Copy link
Member

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".

}

if (isObject(globalObject.onBootResult)) {
pageContext = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can use Object.assign() instead of re-creating a new object.

@@ -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 }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, but I don't think we need eager.

Actually, we need eager if we make onBoot() non-global and cumulative (see my sibling comment).

*
* https://vike.dev/onBoot
*/
type OnBoot = (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's name it OnBootAsync to keep it consistent with the other type names.

return { onBeforeRouteHook: hook, filesystemRoots: null }
const onBootHook = getHookFromPageConfigGlobal(pageConfigGlobal, 'onBoot')
const onBeforeRouteHook = getHookFromPageConfigGlobal(pageConfigGlobal, 'onBeforeRoute')
return { onBootHook, onBeforeRouteHook, filesystemRoots: null }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good question whether onBoot() should be global.

Maybe we can make onBoot() non-global and cumulative. In other words: the result of the various onBoot() hooks are cached and only the results of the relevant onBoot() hooks are applied each time Vike creates a new pageContext object.

But this means that onBoot() can be called later upon navigating to a new page and onBoot() is a misnomer then. Ah, no, actually if make onBoot() eager then we can call all onBoot() hooks at boot time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm actually leaning more towards making onBoot() global as you suggested.

The thing is that, if we don't make it global, then we have to resolve routing before calling onBoot(), which is somewhat contradictory as an idea of onBoot() is to call it as early as possible and before any request is made to the server.

If a user wants a per-page hook, then he can use onBeforeRender(). (So far I don't think we need to introduce a second hook onRequest().)

WDYT?

@@ -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>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should call onBoot() at request-time? My thinking was to call onBoot() exactly once and as soon as possible whenever its code is loaded and then cache its result.

@brillout
Copy link
Member

brillout commented Jun 9, 2024

Hi Olavi, thank you for opening this PR! It goes in the right direction.

I've left a couple of comments.

I'm a bit busy with two highest-priority tickets (pageContext prefetching and implementing the new head management design for vike-{react,vue,solid}), so I may be a bit slow with replying.

Can't wait to land onBoot() hooks. I've actually beeing thinking of bumping its priority to highest-priority.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants