Here only the rendering part would be ported in each web component file, while they would all use the same Brisa wrapper defined in the importmap, which is responsible for creating the Web Component with the signals and the shadow DOM.

SSR of the Web Component

SSR of a Web Component can now be done thanks to Declarative Shadow DOM. The counter-wc.server.js file has already been compiled with this behavior, so you only need to import it on your server and render it in the HTML and adapt it to your server framework.

Here is an example with Bun.js or Node.js without using JSX:

ssr.js

import { renderToString } from \\\"brisa/server\\\";import { jsx } from \\\"brisa/jsx-runtime\\\";import CustomCounter from \\\"counter-wc/server\\\";const html = `                Brisa Web Component Example                        ${await renderToString(jsx(CustomCounter, { start: 10 }))}    `;console.log(html);

Then run bun run ssr.js and you will see the HTML with the rendered web component using the Declarative Shadow DOM.

Tell me more about Brisa... Please...

The integration of these Web Component libraries with Brisa is done through a configuration file:

import type { WebComponentIntegrations } from \\\"brisa\\\";export default {  \\\"custom-counter\\\": {    client: \\\"./path/to/web-component.client.js\\\",    server: \\\"./path/to/web-component.server.js\\\",    types: \\\"./path/to/web-component.types.d.ts\\\",  },} satisfies WebComponentIntegrations;

In this way, SSR and TypeScript types are automatically integrated into your project. And you can use the Web Component in any Server Component or within another Web Component.

\\\"Build

If you are interested in knowing more, I invite you to subscribe to the Brisa newsletter to receive the latest news and updates on Brisa. We estimate that by the end of September it will be ready for launch.

Note for Web Component library creators

We encourage you to try Brisa to create your own Web Component libraries. If you put the \\\"made with Brisa\\\" badge, we will put a link to your library on the Brisa page.


\\\"Build

  \\\"Made

Example

If you want to see the GitHub repository of the example of the counter that we have explained in this article, you can take a look and use it as a reference for your own creations:

Conclusion

In this post, we have seen how to build reactive Web Components that work with SSR and with any JavaScript framework or Vanilla JS. We have used Brisa to build the Web Component and we have seen how to load it in a Vanilla JS project and how to do SSR with it.

I hope you have enjoyed this post and that you have learned something new. If you have any questions, do not hesitate to ask me in the comments below. I will be happy to help you.

Happy coding and enjoy the rest of the summer! ??


\\\"Build

Enjoy the rest of the summer!
","image":"http://www.luping.net/uploads/20240902/172528237066d5b842be406.png","datePublished":"2024-09-02T21:06:10+08:00","dateModified":"2024-09-02T21:06:10+08:00","author":{"@type":"Person","name":"luping.net","url":"https://www.luping.net/articlelist/0_1.html"}}
"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Build Reactive Web Components with SSR

Build Reactive Web Components with SSR

Published on 2024-09-02
Browse:467

Traditional way of writing Web Components is not very SSR (Server Side Rendering) friendly. In this post, I show you how you can build reactive Web Components that work with SSR and with any JavaScript framework (Vue, React, Svelte, Solid, Brisa) or Vanilla JS.

  • Introduction
  • Writting a Web Component with Brisa
  • Building the Web Component
  • Loading the Web Component in a Vanilla JS project
  • SSR of the Web Component
  • Tell me more about Brisa... Please...
  • Note for Web Component library creators
  • Example
  • Conclusion

Introduction

We are going to use Brisa Web Component Compiler. Brisa is a web framework that, besides being similar to other frameworks like Next.js or Nuxt.js, also allows you to build reactive Web Components that work with signals for reactivity, with JSX and with SSR.

Build Reactive Web Components with SSR


Brisa logo

In order to do this, you only need to know the syntax of Brisa when writing Web Components. Brisa is not yet public as it is currently at 95.48% of the v0.1 routemap, but we estimate that in 1 month it will be ready for launch and everyone will be able to access it. However, even if it is not public at all, you can already use it to create your own Web Components libraries.

Writting a Web Component with Brisa

As an example, we are going to write a Web Component of a counter, as always, the classic example.

counter-wc.tsx

import type { WebContext } from "brisa";

export default function CounterWC(
  { start = 0, color = "#2cebcf" }: { start?: number; color?: string },
  { state, css }: WebContext,
) {
  const count = state(start);

  css`
    button {
      background-color: ${color};
      color: white;
      border: none;
      border-radius: 5px;
      padding: 10px;
      margin: 5px;
      cursor: pointer;
    }
    div {
      display: flex;
      justify-content: center;
      align-items: center;
    }
  `;

  return (
    
{count.value}
); }

Brisa uses the name of the files to know the selector, here the selector would be counter-wc.

TIP: Although Brisa is not public yet, you can use TypeScript types to guide you on how to write Web Components.

In the example above, state is used to create a signal and then using the .value you make it reactive inside the JSX. The props are also special signals, since as they are read-only, the .value is not used to make it easier to use and to define default values more easily, this is done through build-time optimizations, similar to React to act as if they were using signals but the other way around.

The css template literal allows it to react to reactive changes in this case of the color property. This css template literal outside of this example is very useful for making reactive animations easily. It is important to remember that Web Components work with Shadow DOM, so the CSS does not affect the rest of the page.

Building the Web Component

To build the Web Component, you need to run the following command:

brisa build -w counter-wc.tsx

This command will generate 2 files:

[ wait ]  ? building your standalone components...
[ info ]
[ info ]   Standalone components:
[ info ]   - build/counter-wc.client.js (670.00 B)
[ info ]   - build/counter-wc.server.js (842.00 B)
[ info ]
[ info ]   ✨  Done in 42.20ms.

These files are not the Web Component, it is only the rendering function of the Web Component optimized at build-time to be as light as possible (the bytes that come out are without gzip).

So, how do we load the Web Component?

Loading the Web Component in a Vanilla JS project

To do this, you need to add the importmap in the HTML with brisa/client and then import the counter-wc.client.js file:


  
    Brisa Web Component Example

Here only the rendering part would be ported in each web component file, while they would all use the same Brisa wrapper defined in the importmap, which is responsible for creating the Web Component with the signals and the shadow DOM.

SSR of the Web Component

SSR of a Web Component can now be done thanks to Declarative Shadow DOM. The counter-wc.server.js file has already been compiled with this behavior, so you only need to import it on your server and render it in the HTML and adapt it to your server framework.

Here is an example with Bun.js or Node.js without using JSX:

ssr.js

import { renderToString } from "brisa/server";
import { jsx } from "brisa/jsx-runtime";
import CustomCounter from "counter-wc/server";

const html = `


    
    Brisa Web Component Example
        ${await renderToString(jsx(CustomCounter, { start: 10 }))}
    

`;

console.log(html);

Then run bun run ssr.js and you will see the HTML with the rendered web component using the Declarative Shadow DOM.

Tell me more about Brisa... Please...

The integration of these Web Component libraries with Brisa is done through a configuration file:

import type { WebComponentIntegrations } from "brisa";

export default {
  "custom-counter": {
    client: "./path/to/web-component.client.js",
    server: "./path/to/web-component.server.js",
    types: "./path/to/web-component.types.d.ts",
  },
} satisfies WebComponentIntegrations;

In this way, SSR and TypeScript types are automatically integrated into your project. And you can use the Web Component in any Server Component or within another Web Component.

Build Reactive Web Components with SSR

If you are interested in knowing more, I invite you to subscribe to the Brisa newsletter to receive the latest news and updates on Brisa. We estimate that by the end of September it will be ready for launch.

Note for Web Component library creators

We encourage you to try Brisa to create your own Web Component libraries. If you put the "made with Brisa" badge, we will put a link to your library on the Brisa page.


Build Reactive Web Components with SSR


  Made with Brisa

Example

If you want to see the GitHub repository of the example of the counter that we have explained in this article, you can take a look and use it as a reference for your own creations:

  • https://github.com/aralroca/counter-wc

Conclusion

In this post, we have seen how to build reactive Web Components that work with SSR and with any JavaScript framework or Vanilla JS. We have used Brisa to build the Web Component and we have seen how to load it in a Vanilla JS project and how to do SSR with it.

I hope you have enjoyed this post and that you have learned something new. If you have any questions, do not hesitate to ask me in the comments below. I will be happy to help you.

Happy coding and enjoy the rest of the summer! ??


Build Reactive Web Components with SSR

Enjoy the rest of the summer!
Release Statement This article is reproduced at: https://dev.to/aralroca/build-reactive-web-components-with-ssr-3pb9?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3