You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Document that vercel functions should be created as SvelteKit endpoints聽#781
Hello team 馃憢, really enjoying the svelte-kit experience so far!
One thing I noticed is when using vercel's serverless functions alongside a svelte-kit app there are a few nuances and unintended side effects, namely with client-side code not executing in local dev when svelte-kit is wrapped with vercel dev
I've since found a way around this behavior and I am fully aware svelte-kit is in beta so this will likely change, however I wanted to bring it up and reach out in case I am going against the grain with this setup.
For what it's worth this is with the static adapter. The way I was able to mitigate the behavior was to write the functions in CommonJS, add a package.json file inside the /api directory with all the necessary dependencies for the serverless functions (like node-fetch or querystring), and require() the routes in a Vite plugin to then be added to the vite dev server. By doing so I can simply run svelte-kit dev rather than vercel dev and still have access to the serverless functions in dev.
Just in order to manage expectations here, I didn't consider vercel API functions at all when writing this adapter. It's not that we can't support them, they just aren't something we expect to work right now. If they do, that's pure chance.
After a chat with Leo from Vercel, the recommendation is to use Svelte Kit's endpoints to write functions in, and not use the Vercel provided functionality - Vercel functions are really intended as helpers if your chosen framework doesn't include a way to write serverless functions (or it is the blessed way to write serverless functions as it is in nextjs).
changed the title [-]Issues with `vercel dev` locally with client-side code not executing[/-][+]Document that vercel functions should be created as SvelteKit endpoints[/+]on Mar 31, 2021
For using the static adapter would it still be recommended to use Vercel's serverless functions? Or ideally I would use the vercel-adapter and use the endpoints? My original thought was that it was odd that the client-side code wasn't being executed when wrapping svelte-kit dev with vercel dev.
Moving forward I'll try out the vercel-adapter and migrate my serverless functions to svelte-kit endpoints.
Update: migrating the functions into svelte-kit endpoints and the vercel-adapter work great! I'm able to use the .json.js files for api routes just fine. However now the pages that are generated from dynamic routes are failing to render. These are mostly blog posts, so is there a way to pull that blog post data in statically while still rehydrating the layout component? I have a "now playing" widget powered by spotify in my layout component but the post data won't change since it's pulled in from markdown files.
If I'm understanding correctly I should add
<scriptcontext="module">exportconsthydrate=falseexportconstprerender=trueexportasyncfunctionload({ page, fetch, context }){constres=awaitfetch(`${page.path}.json`)const{ post }=awaitres.json()return{props: { post }}}</script>
Hello, jumping in here to discuss a bit about serverless functions with sveltekit and vercel (and possibly others?).
I think there are 2 downsides of the current output of sveltekit endpoints functions with sveltekit. Currently endpoints hide behind the functions/render/index.js file, and from what I understand (see here), this creates only 1 serverless function, this issues are:
Scalability: If you have 10 serverless functions with sveltekit, all 10 will hide behind the same serverless function endpoint in vercel (functions/render/index.js). If, say sveltekit function 1 of 10 has a high number of requests, the vercel serverless functions would need to copy and spin un another render/index.js. Which means it needs to clone all 10 sveltekit functions to run just that 1 endpoint again. This can slow down cold boot times and increase cost per function due to increased memory per vercel (AWS under the hood) function, see here.
Memory/Size Limits: Functions could reach the memory or size limits if all functions are bundled together. See here.
Edit: There's also the problem of cold boot time. I assume it would be slower to boot a 10 sveltekit function bundle, than just 1.
Let me know if I'm making the right assumptions, or if there is some code that I'm not seing that splits the render/index.js file into multiple vercel (AWS) functions.
Reacted by Anton Kravchenko, Marcus, Brawl, Stef, Iltimas Doha, Maxime Quandalle, Thilo Maier, josef, Zander Martineau, Bradley Griffiths and 2 more
Hi, I also think it would make sense to deploy the Sveltekit endpoints as separate Vercel serverless functions for the following additional reason.
I currently have an issue that I am unable to include a static html email template file in a sveltekit endpoint, as the file is opt out in the vercel build. The endpoint code throws an exception that the file cannot be found when running on Vercel, locally it works fine. I already tried to configure the functions includeFiles option in vercel.json, but this is not working as the sveltekit endpoint serverless function is not located under api which is a requirement.
Error: The pattern "functions/node/render/index.js" defined in functionsdoesn't match any Serverless Functions inside theapi directory. Learn More: https://vercel.link/unmatched-function-pattern
Would it be hard to implement independant Vercel functions for each Svelte kit endpoint given the current adapter logic?
@antony, is there a roadmap for adapter-vercel or some other discussion that we can follow with regards to how the adapter goes about combining endpoints in one serverless function vs. splitting them up into more than one function?
Would be curious what the pros and cons are and in what direction the adapter will move.
Actually I moved out the logic into an own Vercel serverless function in the api folder, and wrapped the html content into a json file which I than imported directly in the js code. That works fine.
Activity
wiesson commentedon Mar 31, 2021
I'm wondering that it works on vercel at all, because the functions may not be included in the
.vercel_build_output.josefaidt commentedon Mar 31, 2021
For what it's worth this is with the static adapter. The way I was able to mitigate the behavior was to write the functions in CommonJS, add a
package.jsonfile inside the/apidirectory with all the necessary dependencies for the serverless functions (likenode-fetchorquerystring), andrequire()the routes in a Vite plugin to then be added to the vite dev server. By doing so I can simply runsvelte-kit devrather thanvercel devand still have access to the serverless functions in dev.MirrorBytes commentedon Mar 31, 2021
This also occurs with the vercel-adapter.
antony commentedon Mar 31, 2021
Just in order to manage expectations here, I didn't consider vercel API functions at all when writing this adapter. It's not that we can't support them, they just aren't something we expect to work right now. If they do, that's pure chance.
After a chat with Leo from Vercel, the recommendation is to use Svelte Kit's endpoints to write functions in, and not use the Vercel provided functionality - Vercel functions are really intended as helpers if your chosen framework doesn't include a way to write serverless functions (or it is the blessed way to write serverless functions as it is in nextjs).
[-]Issues with `vercel dev` locally with client-side code not executing[/-][+]Document that vercel functions should be created as SvelteKit endpoints[/+]josefaidt commentedon Mar 31, 2021
For using the static adapter would it still be recommended to use Vercel's serverless functions? Or ideally I would use the vercel-adapter and use the endpoints? My original thought was that it was odd that the client-side code wasn't being executed when wrapping
svelte-kit devwithvercel dev.Moving forward I'll try out the vercel-adapter and migrate my serverless functions to svelte-kit endpoints.
josefaidt commentedon Mar 31, 2021
Update: migrating the functions into svelte-kit endpoints and the vercel-adapter work great! I'm able to use the
.json.jsfiles for api routes just fine. However now the pages that are generated from dynamic routes are failing to render. These are mostly blog posts, so is there a way to pull that blog post data in statically while still rehydrating the layout component? I have a "now playing" widget powered by spotify in my layout component but the post data won't change since it's pulled in from markdown files.If I'm understanding correctly I should add
however it still appears to fetch the json data for these static pages. example: https://www-josefaidt.vercel.app/posts/new-express-fs-router-features
and when using the static adapter https://josef.dev/posts/new-express-fs-router-features
leerob commentedon Apr 29, 2021
I believe this can be closed now - agree with the recommendation to use
adapter-verceland the function inside SvelteKit!dsegovia90 commentedon May 6, 2021
Hello, jumping in here to discuss a bit about serverless functions with sveltekit and vercel (and possibly others?).
I think there are 2 downsides of the current output of sveltekit endpoints functions with sveltekit. Currently endpoints hide behind the functions/render/index.js file, and from what I understand (see here), this creates only 1 serverless function, this issues are:
Edit: There's also the problem of cold boot time. I assume it would be slower to boot a 10 sveltekit function bundle, than just 1.
Let me know if I'm making the right assumptions, or if there is some code that I'm not seing that splits the render/index.js file into multiple vercel (AWS) functions.
tonprince commentedon Jun 22, 2021
Hi, I also think it would make sense to deploy the Sveltekit endpoints as separate Vercel serverless functions for the following additional reason.
I currently have an issue that I am unable to include a static html email template file in a sveltekit endpoint, as the file is opt out in the vercel build. The endpoint code throws an exception that the file cannot be found when running on Vercel, locally it works fine. I already tried to configure the functions
includeFilesoption in vercel.json, but this is not working as the sveltekit endpoint serverless function is not located under api which is a requirement.Error: The pattern "functions/node/render/index.js" defined infunctionsdoesn't match any Serverless Functions inside theapidirectory. Learn More: https://vercel.link/unmatched-function-patternmquandalle commentedon Sep 25, 2021
Would it be hard to implement independant Vercel functions for each Svelte kit endpoint given the current adapter logic?
amr3k commentedon Mar 9, 2022
@tonprince Did you solve it?
maiertech commentedon Mar 10, 2022
@antony, is there a roadmap for adapter-vercel or some other discussion that we can follow with regards to how the adapter goes about combining endpoints in one serverless function vs. splitting them up into more than one function?
Would be curious what the pros and cons are and in what direction the adapter will move.
tonprince commentedon Mar 13, 2022
Actually I moved out the logic into an own Vercel serverless function in the api folder, and wrapped the html content into a json file which I than imported directly in the js code. That works fine.