Use Vue with Jeasx

Vue is a reactive UI framework with single-file components. Jeasx renders its components on the server, and the browser hydrates them as islands, so the rest of the page stays plain HTML.

How do I integrate Vue with Jeasx? Install Vue and the esbuild plugin for single-file components:

npm install vue unplugin-vue

Then register the plugin for the server and the browser build in jeasx.config.js:

export default {
  // The server imports this config at startup too, so import build-only plugins lazily.
  ESBUILD_SERVER_OPTIONS: async () => {
    const { default: vuePlugin } = await import("unplugin-vue/esbuild");
    return { plugins: [vuePlugin({ ssr: true, sourceMap: false })] };
  },

  ESBUILD_BROWSER_OPTIONS: async () => {
    const { default: vuePlugin } = await import("unplugin-vue/esbuild");
    return {
      // Vue's feature flags: avoid a console warning and drop unused code.
      define: {
        __VUE_OPTIONS_API__: "false",
        __VUE_PROD_DEVTOOLS__: "false",
        __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: "false",
      },
      plugins: [vuePlugin({ sourceMap: false })],
    };
  },
};

The styles of all components imported by a browser bundle end up in a CSS file next to it (e.g. index.css next to index.js). Link it in your page, so the components are styled before hydration and without JavaScript.

On the server, renderToString from vue/server-renderer turns a component into HTML. In the browser, createSSRApp(...).mount() takes over this markup without rendering it again.

Have a look at the source code of this page to see how the components are rendered on the server and hydrated in the browser.

Temperature converter

Type into either field. v-model binds both inputs, and a writable computed converts in both directions. The starting value comes from the page as a prop.


Kanban board

Drag tasks between columns, or use the arrow buttons. The counts and the progress bar update on their own, because they are computed from the same reactive state.

40% done

To do 2

  • Hydrate in the browser
  • Ship it

Doing 1

  • Render on the server

Done 2

  • Install Vue
  • Register unplugin-vue