Blog: Few things to know before launching a SvelteKit app in prod

As we migrated NNS-dapp to SvelteKit recently (without routing changes, yet), I compiled the top three to know before launching a SvelteKit dapp on mainnet. Hope it can be useful if you develop an application with the same frontend tech stack :smiley:.

3 Likes

Why did you move to SvelteKit? To my understanding most of the benefits SvelteKit adds arenā€™t really usable on the IC.

Are you using the static adapter for building?

We are moving to SvelteKit for various reason:

  • we want to make some changes in the routing (itā€™s in progress so let me validate first the idea before sharing some more info :wink:). until today we used a custom router I developed earlier this year which works fine but itsnā€™t that flexible and works only with a true single page application

  • we are indeed using the static adapter. long story short, I think that frontend app, when possible, should be optimized to use primarly HTML and less JS. e.g. not use JS to render static HTML. particularly on the IC with the service worker, for performance reason

  • we also think that using SvelteKit is more community friendly. itā€™s easier for devs to jump in NNS-dapp if it follows pattern the community could potentially already know as jumping in an app that uses a non-documented custom made routing :grin:

Does that make sense to you too?

2 Likes

Oh and beside SvelteKit, we also wanted to move to ViteJS. The dev experience is so much more enjoyable

1 Like

Makes a lot of sense! Iā€™ve been using Svelte with Vite for recent projects while taking a look at SvelteKit now and then but it never felt worth making the switch as I couldnā€™t really see any benefit for my usecases.

But Iā€™m interested in learning about your router solution, I just use svelte-spa-router and didnā€™t run into any troubles so far :smiley:.

Looking forward to one of your articles on the matter!

1 Like

@peterparker Great! I have spent many days trying to make sveltekit work on the IC and then gave up. Would be cool if there is some ā€œstarterā€ template for the others as there is for the standard svelte project.

Thatā€™s a good idea and none of the svelte examples in the example repo are SvelteKit currently. Maybe I can put something together, just gonna need a bit of timeā€¦

What would be cool @lukevoz just a frontend SvelteKit dapp or should it come with a small ā€œhello worldā€ backend (Rust or Motoko)? with II too?

1 Like

@peterparker simple motoko Hello world backend would be nice :slight_smile: ā€¦ Not sure if there are some caveats with II too. I am using the Connnect2IC toolkit.

1 Like

Thanks for the feedback. Then SvelteKit and a simplistic Hello Word backend in Motoko. I might need a bit of time to provide the example but itā€™s well noted.

2 Likes

@lukevoz last Friday I put together the SvelteKit sample repo we discussed and provided a PR to the examples repo

Im-doing-my-part GIFs - Get the best GIF on GIPHY

1 Like

That is so cool. Thank you. Gonna test it out today/tomorrow and will let you know how it went.:slightly_smiling_face:

Cool, any tests would be much appreciated!

Note: if you add auth-client to the sample you might need some polyfill that are not set in the template currently (ongoing discussion). Maybe something we can add afterwards.

Iā€™ve managed to get this working in local environment. All went smooth except when running the site via npm run dev command, I am getting a 500 error when communicating with the backend (calling the greet function):

6:23:41 PM [vite] http proxy error at /api/v2/canister/rrkah-fqaaa-aaaaa-aaaaq-cai/call:
Error: connect ECONNREFUSED ::1:8000
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1300:16)

This does not happen when I just access the front end canister via browser directly (without npm run dev). Not sure what the problem could be.

did you run dfx start and dfx deploy while trying - i.e. is the local replica still running when you run npm run dev?

donā€™t got that issue :man_shrugging:

Yes the local replica is still running, I am sending a screen too ā€“ on left side there is frontend after dfx deploy and on the right side npm run dev with error while the left one is working.

Not sure what could be wrong here.

Thanks for the screenshot!

Did you use dfx v0.12.0 ?


In your screenshot it says ā€œUnable to fetch root keyā€, thatā€™s the cause of the issue.

In dfx < v0.12.0 the declarations that were generated used process.env.NODE_ENV !== 'production' to detect if the root key should be loaded or not. They fixed / improved it in >= v0.12.0 using process.env.DFX_NETWORK !== 'ic'

The template used v0.12.0 to be up-to-date but also because it solves this other issue.

So I hope thatā€™s your issue :crossed_fingers:.

1 Like

Hmm, but I am using dfx 0.12.0 both dfx.json and dfx --version say that :neutral_face:

Can the node version be a problem? I am using v18.10.0 (npm v8.19.2).

Weā€™ve seen the least problems with node v16, but if you guys can figure out how to do it on v18 as well that would be great!

1 Like

Well spotted, itā€™s indeed NodeJS >= v17. Problably these ViteJS issues #5365 and #4794 - the ViteJS proxy does not work anymore.

I have fixed the template, can you sync and give it a try to confirm?

I have removed the proxy and upgraded the template to use the host param of agent-js instead which makes it compatible regardless of the NodeJS version.

1 Like

Oh yeah, now it works. :slight_smile: Thanks a lot for for your time spending on figuring out my errors here :slight_smile:

How about the II is it complicated to add? I was looking at the Connect2IC toolkit that Ive been with svelte before and it seems like it would need some rearrangement to work with svelte kit as well.