T
TanStack3mo ago
conscious-sapphire

How to execute code on start's startup ?

Hi ! I need to execute migrations and stuff on tanstack start launch, where can I do that? I tried on ssr.ts but it get executed (once, so cool) but it gets ran only when I first visit a page. I tried to search inside the Discord, some mentionned to do that through a Nitro module/plugin, I did try, but I can't get what I need to run inside (working with cloudflare and can't get cloudflare bindings working inside it)
4 Replies
conscious-sapphire
conscious-sapphire3mo ago
conscious-sapphire
conscious-sapphireOP3mo ago
Thank you, well the cloudflare bindings are working fine inside the app itself, I'm still not using the devinxified start version, so now I get the bindings like this:
export async function getCloudflareBindings() {
if (import.meta.env.DEV) {
const { getPlatformProxy } = await import("wrangler");
const { env } = await getPlatformProxy();
return env as unknown as CloudflareBindings;
}

return process.env as unknown as CloudflareBindings;
}
export async function getCloudflareBindings() {
if (import.meta.env.DEV) {
const { getPlatformProxy } = await import("wrangler");
const { env } = await getPlatformProxy();
return env as unknown as CloudflareBindings;
}

return process.env as unknown as CloudflareBindings;
}
It works for now (curious about the lagging that you had tho). But what I wanted to do is to run the migrations as soon as the app starts, like is there an entrypoint of the app itself somewhere ?
conscious-sapphire
conscious-sapphire3mo ago
the lag was that i had a page that grabbed a value from kv and it would take more time to grab that value if i was also regrabbing the proxy on every refresh i don't think there's an accesible entry point unforuntately
conscious-sapphire
conscious-sapphireOP3mo ago
Ok, I understand know, good to know then. Well I just migrated to the devixified version because I noticed that the documentation was also updated, now server.tsx (old ssr.tsx) actually runs once as soon as when I start dev server, soooo so far so good, I didn't check when built, but it's better than before well finally, I was wrong, I had a window open in the background and the window was reconnecting to vite so it triggered the server.tsx

Did you find this page helpful?