Replies: 1 comment
-
You could also call |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
So here is a use-case
Say I have a cricket match scoreboard website. I am loading data from a db for score and polling for it every 5 seconds from frontend. However I also need the page to be instantly loaded with score without initial client side data loading. (This could also be use case for stock market indicators app or things like that where one wants to use polling)
Here are some patterns I could think of. (Didn't implement all of them but just ideas, let me know if I am wrong)
Create a common method for data fetching in something like /lib/controller/xyz.ts and then call this method in /routes/api/xyz/server.ts and /routes/xyz/+page.server.ts along with having onmount polling function call it in /routes/xyz/+page.svelte or in /routes/xyz/+page.ts
Create an api endpoint in /routes/api/xyz/server.ts and calling the api route from boths page.server.ts and page.ts for ssr and polling
Create a /routes/xyz/server.ts file with get request giving data and then have /routes/xyz/+page.server.ts and rputes/xyz/+page.ts files call the endpoint as "./" and with some way to specify app/json request rather than app/html request to let sveltekit's router do it's magic of determining whether it is page request or api request.
I would prefer if 3rd one works since it doesn't require you to have separate api route location to maintain. But would like to know implications on caching etc.
Beta Was this translation helpful? Give feedback.
All reactions