Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Svelte SSR #1349

Merged
merged 2 commits into from
Dec 6, 2022
Merged

Svelte SSR #1349

merged 2 commits into from
Dec 6, 2022

Conversation

buhrmi
Copy link
Contributor

@buhrmi buhrmi commented Nov 25, 2022

This adds Svelte SSR support.

To enable SSR for a Svelte app:

1. Create an ssr.js entrypoint. Here is an example for Vite:

// ssr.js
import { createInertiaApp } from '@inertiajs/svelte'
import createServer from '@inertiajs/svelte/server'
import layout from '../pages/_layout.svelte'

const pages = import.meta.glob('../pages/**/*.svelte')

createServer((page) => createInertiaApp({
  page,
  resolve: async name => {
    const page = await pages[`../pages/${name}.svelte`]()    
    return Object.assign({layout}, page)
  }
}))

Build it with vite build --outDir dist/server --ssr src/ssr.js or a similar command appropriate for your environment.

2. Enable the hydratable compiler option

// vite.config.js
// ...
  plugins: [
    svelte({
      compilerOptions: {
        hydratable: true
      }
    })
  ]
// ...

3. Add the hydrate option when instantiating your app:

// application.js
createInertiaApp({ 
  // .....
  setup({ el, App}) {
    new App({ target: el, hydrate: true })
  },
})

@reinink
Copy link
Member

reinink commented Nov 26, 2022

Weww, this is great @buhrmi! I just pulled in @pedroborges's Svelte PR #1344, would you mind updating this PR to resolve conflicts?

I'm excited! 🥳

@buhrmi
Copy link
Contributor Author

buhrmi commented Nov 26, 2022

☝️ rebased

@patricknelson
Copy link

Hmm... oops! I think I'm going to just delete my previous comment out of shame. 😅

However, I think you could register this as general interest on the topic of sort of parametric (or API-driven) server-side rendering for Svelte components, even if it's not properly placed here. My use case (an API for independent SSR of Svelte components in mixed/hybrid non-Node.js environments) doesn't quite fit Inertia.js generally, but I do find work on this project inspiring!

@reinink reinink merged commit 1857104 into inertiajs:master Dec 6, 2022
@reinink
Copy link
Member

reinink commented Dec 6, 2022

@buhrmi Thanks again for this! I think I'll probably end up moving things around a little, but this is a great starting point. Thanks so much! 🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants