DEV Community

Cover image for How to apply SSR in Vue.js project without Nuxt?πŸ”₯
Anthony Max
Anthony Max Subscriber

Posted on

25 23 22 22 23

How to apply SSR in Vue.js project without Nuxt?πŸ”₯

Hello everyone! In this article we will consider one of the main problems that you can solve without using Nuxt.js as the main framework - applying the SSR (Server-side Rendering) method to your Vue.js application.

It is worth considering that this method uses SSR without indexing by robots, but retains all other benefits.

πŸ”— Connecting a third-party library

You can connect a library called HMPL-js. This is a compiler of the corresponding template language, which extends the capabilities of ordinary HTML. By passing only a string to the parameters of the compile function, you can get a ready-made DOM element that will fit into the interface of your application. Let's see how it looks:

<template>
  <div ref="hmplContainer">
    <!-- clicker here -->
  </div>
</template>

<script>
import { onMounted, ref } from "vue";
import hmpl from "hmpl-js";

export default {
  setup() {
    const hmplContainer = ref(null);

    onMounted(() => {
      const templateFn = hmpl.compile(
        `<div>
          <button data-action="increment" id="btn">Click!</button>
          <div>Clicks: {{ src: "/api/clicks", after: "click:#btn" }}</div>
        </div>`
      );

      const clicker = templateFn(({ request: { event } }) => ({
        body: JSON.stringify({ action: event.target.getAttribute("data-action") })
      })).response;

      hmplContainer.value.appendChild(clicker);
    });

    return { hmplContainer };
  }
};
</script>
Enter fullscreen mode Exit fullscreen mode

Here, when mounting the application, we insert a ready-made element that will be updated every time we click on the button. Moreover, the click count will occur on the server, and we will only send custom requests there.

πŸ’Ž Checkout HMPL-js β˜…

And this is another simple example, we can remove the after property and use it simply as a load on mount. This is how it will look:

<template>
  <div ref="hmplContainer">
    <!-- header here -->
  </div>
</template>

<script>
import { onMounted, ref } from "vue";
import hmpl from "hmpl-js";

export default {
  setup() {
    const hmplContainer = ref(null);

    onMounted(() => {
      const templateFn = hmpl.compile(
        `<div>
          {{ src: "/api/header" }}
        </div>`
      );

      const header = templateFn().response;
      hmplContainer.value.appendChild(header);
    });

    return { hmplContainer };
  }
};
</script>
Enter fullscreen mode Exit fullscreen mode

Thus, with a regular component we immediately receive our DOM element from the server without adding an additional event. From such blocks, you can create the entire page that will use the SSR method.

header

πŸ‘€ Integration with older versions of Vue

Moreover, this method works great for older versions of Vue. That is, you won’t have to update to version 3 or later (depending on when you read this), but using Nuxt.js, especially a newer version, you will have to do this. How can you do it in an older version:

<template>
  <div ref="hmplContainer">
    <!-- header here -->
  </div>
</template>

<script>
import Vue from "vue";
import hmpl from "hmpl-js";

export default Vue.extend({
  data() {
    return {
      hmplContainer: null
    };
  },
  mounted() {
    this.hmplContainer = this.$refs.hmplContainer;
    ...
  }
});
</script>
Enter fullscreen mode Exit fullscreen mode

Here, instead of the onMounted hook, we will use the regular mounted in the object, and also slightly change the ref. In general, nothing significant will change, but such functionality will be quite relevant.

βœ… Conclusion

This method will allow you to use SSR regardless of the version of your Vue application. Also, it does not force you to adhere to strict framework boundaries, you can combine this method with other libraries without losing interactivity and code readability, and in general without really redoing anything.

I hope this method will help you make your website smaller in size, thus loading faster for the client.


You can follow the author on Twitter!

Thank you all very much for reading the article ❀️!

Thanks you

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (7)

Collapse
 
butterfly_85 profile image
Butterfly β€’ β€’ Edited

Great article!

Collapse
 
anthonymax profile image
Anthony Max β€’

Thanks!

Collapse
 
khalil_benmeziane profile image
Khalil Benmeziane β€’

Great article keep going

Collapse
 
anthonymax profile image
Anthony Max β€’

Thanks!

Collapse
 
onemorethinginaicom profile image
One More Thing in AI β€’

Highly recommend!

Collapse
 
anthonymax profile image
Anthony Max β€’

Me too

Collapse
 
anthonymax profile image
Anthony Max β€’

This method is great for modern web applications, as it also supports modern standards like ES2025 and others.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.

πŸ‘‹ Kindness is contagious

If this post resonated with you, feel free to hit ❀️ or leave a quick comment to share your thoughts!

Okay