sveltejs / sapper

The next small thing in web development, powered by Svelte
https://sapper.svelte.dev
MIT License
6.99k stars 434 forks source link

Add a render method like Next JS #1408

Open lamualfa opened 4 years ago

lamualfa commented 4 years ago

Is your feature request related to a problem? Please describe.

Small applications that use the concept of monoliths often use the API server, Frontend server and other logic in a single project. I have a little trouble to do the separation of the many routes if using the file routing concept.

Describe the solution you'd like

in Next JS, we can create a custom server using anything (like Fastify, Express, Koa, etc), then create a special handler that is used to render the React page. Next JS has its own render method which can be called from anywhere. Does Sapper have a plan to implement the same to render a Svelte page?

Example using render method in NextJS :

createServer((req, res) => {
    const parsedUrl = parse(req.url, true)
    const { pathname, query } = parsedUrl

    if (pathname === '/a') {
      // NextJS render method
      app.render(req, res, '/a', query)
    } else if (pathname === '/b') {
      // NextJS render method
      app.render(req, res, '/b', query)
    } else {
      // Other handler
      handle(req, res, parsedUrl)
    }
})

How important is this feature to you?

I am a Fastify user. I have a little trouble integrating the two. Fastify uses a quite different concept from Express to route them, so I need a render function that can be called from any route.

antony commented 4 years ago

It's not clear here what you're trying to achieve. Do you have a concrete example?

To create a server route in Sapper, you can just add a js file in the routes directory.

thgh commented 4 years ago

@antony You could see it as "page support for custom routers". Currently you can have "api routes" with .js but it's not possible to render a page component like that.

Usecases:

One challenge is that the naive solution would only allow SSR because this custom routing logic is not available client-side. (could be a feature)