2

I'm new to SvelteKit. I have some familiarity having built an app that serves as a pure front end, but want to know if I can use SvelteKit for backend code, e.g. CPU intensive work or API development. I'm aware the convention is to separate out an API, but lower development overhead of doing as a monolith is attractive.

As I understand it, and mentioned in this question, code in endpoints and hooks will not be exposed to the client. This suggests to me that there is not reason so far as SvelteKit its self goes not to develop back end code in the same project.

Another consideration, is that the JAMstack ecosystem around frameworks like SvelteKit seem to place limitations on CPU usage. This I can work around by running the applications in containers (or by paying for less restrictive plans).

Is there any reason or recommendation, given the above mitigations that I shouldn't do this?

If I can do this:

  • What would the folder structure look like to ensure that server-side code stays server-side?
  • What techniques could I use other than find / grep on the build output to ensure that no sensitive data has been leaked?
gbro3n
  • 6,729
  • 9
  • 59
  • 100

1 Answers1

1

Updating and expanding this answer based on clarification in this comment:

What if I don't want to use Supabase though - what if I want to query Postgres directly, how do I ensure the connection string stays private? Will I run into platform constraints if using JAMstack orientated PaaS[?]

To ensure secrets stay private, the typical approach is to use environment variables in endpoints or hooks.

To quote Sillvva in the Svelte Discord (links added by me):

Endpoints run exclusively on the server. For page endpoints, the client calls them via fetch and the server calls them directly via function call during SSR.

We use environment variables to as part of a security strategy to make them “easy to change between deploys without changing any code” and to reduce the chance “of them being checked into the code repo accidentally.” Make sure you add all variations of whatever .env files you use to .gitignore.

Platforms like Netlify and Vercel allow one to set them in the UI or from their CLI.

From How do I use environment variables? in the SvelteKit docs FAQ:

To use environment variables at runtime, you would need to instantiate dotenv yourself in your server-side code so that they are exposed at process.env.YOUR_ENV_VAR. You may also use $session to pass them to the client if needed.

Load functions, which run on the server and the client, can be used to access data from endpoints. Note:

Code called inside load blocks […] should not directly reference any API keys or secrets, which will be exposed to the client, but instead call an endpoint that uses any required secrets

The Supabase links in the original answer were meant as an example of file structure, but the video link up top covers endpoints, hooks, and load fairly well, though I prefer to read rather than watch this kind of thing. This Reddit thread has a good summary of setting up a connection to Postgres in hooks handle function, making that available to endpoints via event.locals, and using load in svelte files. (Note some of the docs links have changed since that Reddit answer was posted but the ones in this answer are current.)

For PaaS much of the above holds, although Netlify, Vercel, and Cloudflare Pages have official adapters that one can use to take advantage of platform-specific features like (edge) functions and workers.

Original answer:

I am pressed for time, and can expand on this later, but for now…

Here are some resources to look into:

Better Protected Routes with endpoints, hooks, and load in SvelteKit

You can use load() to protect web pages in SvelteKit and enable your application to still work with and without JavaScript enabled. In this episode, I walk through converting from our other ProtectedLayout format using slots and switch to using > load.

Svelte Starter Kit

…is an opinionated boilerplate based off of SvelteKit, with all the bells and whistles you want ready, up and running when > starting any Full-stack Svelte/Javascript project. Out of the box you get all the essentials

  • Typescript as the language choice
  • Tailwind CSS for quick styling without getting out of your HTML
  • ESLint and Prettier for static code analysis and code formatting
  • SEO pre-configured

with Supabase as the 3rd Party Persistence Layer for

  • Authentication System with Supabase GoTrue
  • User Profiles available on /profile as an example for Supabase PostgREST (CRUD API)
  • User Avatar which is Supbase Storage(AWS S3 backed effortless uploads) supported

Quickstart: SvelteKit | Supabase

This example provides the steps to build a simple user management app (from scratch!) using Supabase and Svelte.

Supabase Auth With SvelteKit

Implementing a secure JWT token authentication system for server-rendered applications.

Please comment with any specific concerns not covered in these.

rdela
  • 467
  • 4
  • 12
  • 1
    Thanks for the links. All useful information. I guess the implicit answer is that yes I can use Svelte for back end code, given that the templates above are 'full stack' when Supabase is used. I don't feel I have a clear answer yet though. What if I don't want to use Supabase though - what if I want to query Postgres directly, how do I ensure the connection string stays private? Will I run into platform constraints if using JAMstack orientated PaaS. I appreciate these questions are probably dependant on various factors. – gbro3n Jul 06 '22 at 08:34
  • 1
    Thanks @gb2d I have updated the answer. Yes, exactly, the short answer is yes, you can write server-side code in SvelteKit, with the right code and proper adapter for your target environment. I gather the docs are intentionally shallow until SvelteKit hits 1.0, and that the maintainers are waiting til at least then to provide examples. Let me know if anything is still murky please. We're here to help each other! – rdela Jul 12 '22 at 01:32
  • Thank you so much for such a comprehensive answer. If I find I can add anything to this as I gain familiarity, I will. – gbro3n Jul 12 '22 at 09:02
  • 1
    Please do. Delighted to have someone as thoughtful as you in the Svelte community. – rdela Jul 12 '22 at 17:48
  • 1
    Added note to load functions section quoting the linked docs page: "Code called inside load blocks […] should not directly reference any API keys or secrets, which will be exposed to the client, but instead call an endpoint that uses any required secrets" https://stackoverflow.com/posts/72876917/revisions – rdela Jul 18 '22 at 17:16