Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How does Bun's templating engine support server-side rendering in Vue projects


How does Bun's templating engine support server-side rendering in Vue projects


Bun's templating engine supports server-side rendering (SSR) in Vue projects by allowing you to use Vue components directly as templates, without the need for a separate templating language. Here's how it works:

Using Vue Components as Templates

With Bun's templating engine, you can define your view templates using Vue components directly in your `.bun` files. For example:

html
<template>
  <div>
    <h1>{{ title }}</h1>
    <p>{{ content }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      title: 'My Page',
      content: 'This is some content.'
    }
  }
}
</script>

This Vue component can be used as a template for rendering the page on the server.

Server-Side Rendering

To render the Vue component on the server, you can use Bun's built-in `renderToString` function:

js
import { renderToString } from "bun:render";
import MyPage from "./views/MyPage.bun";

export default {
  async fetch(request) {
    const html = await renderToString(MyPage, {
      title: "My Dynamic Page",
      content: "This content is rendered on the server."
    });

    return new Response(html, {
      headers: {
        "Content-Type": "text/html"
      }
    });
  }
}

In this example, `renderToString` takes the Vue component and an object of props to pass to the component. It returns the rendered HTML string, which can be sent back as the response.

Hydration

To make the server-rendered HTML interactive on the client, Bun supports automatic hydration. When the client receives the initial HTML, Bun will automatically "hydrate" the Vue application, attaching event listeners and making the components interactive without the need for a full client-side render[1][2].

This allows you to enjoy the benefits of server-side rendering for improved initial load times and SEO, while still providing a fully interactive client-side experience.

Conclusion

Bun's templating engine makes it easy to use Vue components for server-side rendering in your projects. By allowing you to define templates directly in Vue syntax, it provides a seamless developer experience and eliminates the need for a separate templating language. The built-in support for rendering to string and automatic hydration ensures that your Vue applications can be rendered efficiently on both the server and client[3][4].

Citations:
[1] https://forum.cs-cart.com/t/vue-js-server-side-render-theme/55053
[2] https://www.syncfusion.com/blogs/post/a-step-by-step-guide-to-server-side-rendering-with-vuejs
[3] https://dev.to/spacerockmedia/react-vue-components-are-just-server-side-template-components-with-worse-performance-change-my-mind-59ge
[4] https://stackoverflow.com/questions/52517716/use-react-or-vue-over-server-side-template-engines
[5] https://vuejs.org/guide/scaling-up/ssr.html
[6] https://vuejs.org/guide/quick-start
[7] https://github.com/sailscastshq/boring-stack/discussions/67
[8] https://www.npmjs.com/search?page=11&perPage=20&q=server-side-rendering