How to do SSR (server-side rendering) in Svelte/TypeScript?

Viewed 3791

Svelte’s JavaScript server-side rendering API is described here: https://svelte.dev/docs#run-time-server-side-component-api

However, when I do this in TypeScript, there is no method App.render().

  • Do I need to change rollup.config.js (e.g. compilerOptions.generate)?
  • Do I need two versions of this file – one for the server and one for the client?

Can anyone help? Thanks!

2 Answers

Svelte Server-side component API is not directly accessible via import. Instead, you need to build the production with vite options --ssr. Otherwise, you're importing the component class extended SvelteComponent and that class has no render function.

You can check out this guide for Production SSR build: Vite Server-Side Rendering.

You don't need to set up the SSR Dev server or inject /@vite/client because svelte-hmr already does the magic under the hood.

The SSR Bundle options ssr.noExternal doesn't seem to work for me. So that I need to convert all Svelte components import into static import for a production build.

Related