Build Reactive Web Components with SSR

WBOY
Release: 2024-08-24 11:22:06
Original
183 people have browsed it

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 at95.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}
); }
Copy after login

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
Copy after login

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.
Copy after login

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   
    
Copy after login

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);
Copy after login

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;
Copy after login

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 
Copy after login

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!

The above is the detailed content of Build Reactive Web Components with SSR. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!