SvelteKit
Using Telefunc with SvelteKit.
Example
Add to existing app
See example of adding Telefunc to an existing app: d31fc02.
npm install telefunc(orpnpm/yarn)- Add Telefunc's Vite plugin (example
vite.config.ts) - Add Telefunc's server middleware (example
src/routes/_telefunc/+server.ts) - Define your first
.telefunc.jsfile (examplesrc/routes/Counter.telefunc.ts).telefunc.jsfiles are always server-only, regardless of where they are located. You don't need to place them in$lib/servernor add.server. - Optionally: globally define the type your
contextobject (examplesrc/telefunc.d.ts)
Initial Data
For fetching the initial data of pages (SSR data) we use SvelteKit's built-in data fetching mechanism instead of Telefunc.
For example, we can use SvelteKit's load function in a +page.server.js file to fetch initial data directly from a database, see SvelteKit Docs > Loading data > Universal vs server.
We still use Telefunc for fetching data but only after the initial rendering of the page, for example for pagination or infinite scroll.
In case you're curious: we cannot use Telefunc for server-side rendered (SSR) data because only the framework can pass SSR data from the server to the client-side (which is needed for hydration). This is common to all SSR frameworks like SvelteKit.