23

I recently started working with Svelte via SvelteKit and I have a few questions about this framework to which I haven't been able to find any direct answers in the source/documentation:

  1. SvelteKit has SSR and in the documentation it says:

All server-side code, including endpoints, has access to fetch in case you need to request data from external APIs.

  • What code is server-side rendered besides endpoints and how it decides this? All the code from scripts from svelte pages runs on the client or some of it runs on the server?
  • In order to make use of SSR locally you need an adapter for it or does svelte start a server on its own?
  • How does SSR work in a production environment like Netlify for example. Is the netlify adapter is used for SSR (running the endpoints in a netlify function)? If a netlify adapter is not provided, how/where would the endpoints run?
  1. If I want to use custom netlify functions in a sveltekit project what configurations are needed (besides netlify.toml and netlify adapter) in order for netlify to recognize the functions from inside the functions directory?
  2. What is the difference here between SSR and prerendering? SSR is used only for endpoints and other js code and prerendering is used for generating the Html to send it to the client which will then be hydrated, with the compiled js code, also sent to the browser?

Thanks!

Connor Low
  • 5,900
  • 3
  • 31
  • 52
Claudia
  • 241
  • 1
  • 2
  • 6

2 Answers2

18

By default, pages are also server-side rendered when you first visit a site. When you navigate to a subsequent pages they will be client-side rendered.

Adapters are only used in production. If you run npm run dev for local development you still get SSR. In production, how exactly SSR is run depends on the adapter you choose. An adapter is required for production. adapter-node runs SSR on a Node server, adapter-netlify runs SSR in Netlify functions, etc.

See here for discussion of custom Netlify functions: https://github.com/sveltejs/kit/issues/1249

SSR is used for pages as well, but prerendering means that rendering happens at build time instead of when a visitor visits the page. See this proposed documentation for more info: https://github.com/sveltejs/kit/pull/1525

Ben McCann
  • 18,548
  • 25
  • 83
  • 101
  • Hey Ben "subsequent pages they will be client-side rendered" will be opt-in, right? – Pier Jan 19 '22 at 15:46
  • 2
    No, it's opt-out. The default is when you navigate to a new page it will update just the changed sections of the UI in the client without refreshing the whole page. However, you can disable the client-side router if you'd like. – Ben McCann Jan 20 '22 at 03:34
6

Pages are SSR when you first visit the site, including all the code in the script tag of your svelte page. However, as you navigate to other pages, the code will be run on the client and the page will be dynamically rendered as Sveltekit makes a single page web app look like it has different pages with the history API.

You can decide which code runs on the server and which runs on the client. If you don't do anything special, Sveltekit and your deployment environment will decide that for you. If you want some code to run only in browser (perhaps it needs to use the window object or need authentication), you can use the browser variable.

import { browser } from '$app/environment';

if (browser) {
  // Code that runs only in browser
} 

You can also put the code in the onMount function, which will be run when the component first mounts, which only happens in browser.

import { onMount } from 'svelte';

onMount(() => {
  // Do stuff
})

If you want SSR, you can put the function in the load function in route/+page.js. One typical use case is for a blog entry that grabs the content from the database and populates and formats the content. If you get to the page from a URL, or refresh page, the code in the load function will be executed on the server. If you navigate to the page from elsewhere in your web app, the code will be run on the client, but it will look like SSR as the UI will refresh only after the load function returns (you won't see loading screen or a blank page). See the official docs for more for more.

import { error } from '@sveltejs/kit';
 
/** @type {import('./$types').PageLoad} */
export function load({ params }) {
  if (params.slug === 'hello-world') {
    return {
      title: 'Hello world!',
      content: 'Welcome to our blog. Lorem ipsum dolor sit amet...'
    };
  }
 
  throw error(404, 'Not found');
}

I am not very sure about how to use Netlify function, as Ben mentions, you can see the discussion on https://github.com/sveltejs/kit/issues/1249. Although I think that you might be able to implement the same functionality with +page.server.js, and the "Actions" to invoke them.

Juan Pablo Rinaldi
  • 3,394
  • 1
  • 20
  • 20
DE0CH
  • 113
  • 2
  • 4