Custom functions in +page.server.ts #9489
-
I understand the server load function to load data on page load, but what I am trying to understand is if there is a way to create functions that can be referenced from +page.svelte after the page has been loaded. I know this can be done with API endpoints, but I am interested to understand if there is a way to create server functions specific to just that page. The use case would be, calling a function function on +page.server.ts from +page.svelte, when a button in +page.svelte is pressed for example. This function will run in +page.server.ts and return result to +page.svelte. I tried creating a function and adding it to load data, but cannot stringify the function. I tried importing from +page.server.ts, but cannot do that either. +page.server.ts
+page.svelte
Is there any recommendation around this?
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You can do this with a universal load function in |
Beta Was this translation helpful? Give feedback.
-
It is not currently possible in SvelteKit to do what you're describing. The way to execute code on the server in response to a client-side action in SvelteKit is either creating an API route with +server.js files or form actions. |
Beta Was this translation helpful? Give feedback.
It is not currently possible in SvelteKit to do what you're describing. The way to execute code on the server in response to a client-side action in SvelteKit is either creating an API route with +server.js files or form actions.