11

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!

Axel Rauschmayer
  • 25,381
  • 5
  • 24
  • 17

2 Answers2

4

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.

Tran Chien
  • 614
  • 4
  • 10
0

The official template relies on rollup-plugin-svelte, where similar question was asked. Essentially compiling in SSR mode does not automatically generate any HTML, in fact some post processing is required. The Svelte Server-side component API can be used for that.

There are several solutions out there for SSR:

Hermann
  • 1,150
  • 8
  • 9
  • Thanks! The Server-side component API is all I need (see first line of my question). However, I can’t access this API when I’m using TypeScript. – Axel Rauschmayer Jan 18 '22 at 17:56