0
votes

Good morning/evening/night

I am coding a little website to be my homepage.
So far I managed to do what I wanted with express and handlebars templates but I am doing this website to test my boundaries, and I lack using a frontend framework and working with web components.

so for the sake of challenge and learning, I settled on svelte
wich is not exactly a framework, I read, but is a delight to write with and seems very promising on the performance side.

The problem is I want to keep the hand on my website, and sapper, the full framework that comes with svelte, is a bit too much of a black box for me.
(you put this file here and that file there,
compile with this complex configuration you shouldn't touch
and BOOM you got routes)

What I would like is to use express to manage the routes and then render the svelte app, either with a different page/app for a route, or the same app with different variables.

Could anybody point me in the right direction ? I consider using sessions or a socket but I have no idea of how to listen to this client-side and the documentation/articles about svelte are sparse and all talk about sapper.

Thank you !

here are a few lines of code I wrote in a test app. I know it's not the way you do it but it was for curiosity's sake

// --------------server.js
// ...
app.set('view engine', 'hbs')
app.set('views', path.join(__dirname, '../views'))
// Using a view engine on TOP of svelte 
// is certainly a bad choice

app.use(express.static('app/public'))
// to make the svelte compiled .js and .css available

app.get('/test', (req, res) => {
  res.render('home', {user:true}) 
  // I tried to send props like this
  // it did not get the props in svelte, 
  // but what I found is that svelte ADDS itself to the page
  // and does not remove the HBS code, event though it is in the target container
})


edit

I will finally use sapper, thank you for the advices... I really would like them to implement some sort of svelte middleware that allows to render/serve pages individually (once they are build of course)

1

1 Answers

1
votes

If you do it like that, splitting your web application into several independent svelte apps that are served from express, then you need to keep the (web-)application wide state on the backend or keep it in the local storage on the browser. You can't use the states provided by sveltes store (writeable, etc), because that's in-memory and destroyed whenever you navigate to a new page (via express).

If sapper is to much magic but you want to keep running a single-page-app, have a look at svelte-spa-router. That isn't configuration based.