-
-
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
New hook onBoot()
#962
Comments
that is great I was customizing the express server and fetch some data before it renders the page. this will be very helpful thanks! |
@gdev219 👍 Let me know if not having this hook is a blocker. (Since you have an Express.js server, you should be able to easily workaround it.) |
This feature would be very helpful! I'd like to fetch navigation menu entries and default site metadata (title, description, etc.) from a CMS, before any page loads. Alternatively, if the nested layout design is powerful enough, the |
@brillout That feature would be very useful. Tbh I'd be fine with the workaround of having a global import { enumObjects } from '../renderer/types';
export default onBeforeRender;
async function onBeforeRender() {
return {
pageContext: {
enums: enumObjects,
},
};
} In the Page I'm accessing the ...
const { enums } = usePageContext();
console.log(enums); // retrieves undefined
... Do you have an idea what I'm doing wrong here? |
Try to move
I hope this help you |
@phonzammi Thanks for the tip. I tried that, but when debugging I noticed, that the |
Do you have any Take a look at my example here If you can give a reproduction may help too |
@s3ththompson FYI the workaround should also work for your use case. |
@phonzammi what if I still want to retrieve specific data for one page, but have global data available for all pages? Isn't that the whole point of this Issue? |
@stampaaaron. In my opinion, that case should refer to state management or cache, you don't want/need to fetching then refetching global data again and again right? or if you need too then you can add async state management library such as tanstack-query or swr, etc. Please read again
First thing first, it's about fetching (or refetching) global/init data. Then it's up to you how to handle/persist/preserve that data. Actually, I still have some answers or opinions (some still in my mind & I haven't implement yet). What do you think @brillout ? |
@phonzammi Yes, custom hooks seems to be what @stampaaaron is looking for. |
@stampaaaron maybe helpful: we have now invented a new |
New name: |
@brillout If you wanted to set a i18n values for onBeforeRoute, which runs both in the client and in the server - you would have 2 different sources for these values. Ideally there would be some clean way to persist something permanently in the pageContext because that would be enough for this use case as the user can create the initial pageContext for the renderPage. Maybe something like staticContext / globalContext / appContext. |
@OlaviSau Indeed. I just updated the Feature Request. |
Do you want me to implement it as you specified in the update? |
@OlaviSau Do you want to implement it because you need it or because you want to contribute? If it's only because you need it it's probably faster I implement it. But if you want to contribute on a recurring basis, then yes we can give a collaboration a shot! (Btw. I like that one blog post of yours.) |
I do "need" it in the sense that it would make our code simpler and I really don't mind implementing it either. I suspect that this won't be the last contribution, especially after that kind of flattery 😁 😁 Though there are certain design questions that pop up for sure that you might be able to answer more easily when implementing it yourself. We use express yet it would still be very nice to have this, even though there are workarounds. It wouldn't be fair to say that it's a blocker though. |
Ok let's give it a try then! One of these contribution-welcome 💕 would be easier, but we can try and if we realize this feature request is a little too ambitious then I can take over the implementation. As for the design questions, we'll see as we go. Note https://vike.dev/file-env which is a fairly recent feature that makes a lot of sense in this context. And, yes, it should be as soon as Vike's runtime loads, i.e. regardless of whether the user uses Express.js.
I see 😁 - no but seriously: you figured out a fundament of good SWE which makes me think you have taste. |
I'll give it a try 😁 Thanks for the file-env reference, would have been huge problem if I would have missed that ❤️ |
This is a great idea, I would be very happy if this hook was implemented |
@codefln Feel free to elaborate your use case. |
In general, the more use cases for |
My use case is almost identical to the author of this issue. |
Very similar to the original use case, but with with services. We currently use a number of services that have identical interfaces (but distinct implementations). We were |
So, depending on the page, you want to use a different Note With @OlaviSau, author of the |
For my specific use case, the services to instantiate are determined at build time (or dev launch time), so a single global |
Description
A common use case is to fetch global initialization data that is needed by all pages. (For example i18n data.)
The
onBoot()
hook is executed as soon as the server is started and its result is awaited before any page starts rendering.It's executed when the production/development/preview server starts as well as when the pre-rendering process starts. So it's guaranteed to have been executed before any page starts (pre-)rendering.
Workaround until this is implemented:
renderPage()
, see API >pageContext
> Custom.onBeforeRender()
hook that applies to all your pages, and fetch your global/init data in there.onDataLoad()
. CallonDataLoad()
from your globalonBeforeRender()
hook.Additional consideration:
+onBoot.client.js
.+onBoot.js
=> server-side only.+onBoot.client.js
=> client-side only.+onBoot.shared.js
=> server- and client-side. (I can't think of a use case, but supporting.shared
wouldn't induce any additional implementation cost.)+onBoot.js
and+onBoot.client.js
.See also:
pageContext
).If not having
onBoot()
is a blocker for you then write a comment down below.The text was updated successfully, but these errors were encountered: