Developing an oEmbed component with WebComponents and what I learned
I developed and released an oEmbed component with WebComponents.
- https://www.webcomponents.org/element/Silver-birder/o-embed
- https://www.npmjs.com/package/@silverbirder/o-embed
I would like to list what I learned during the development.
Starter Kit
When developing Web Components, it seems good to use one of the following starter kits.
Even without these, you can develop Web Components, but to write in Typescript or to test, you need a certain amount of preparation. Therefore, if you want to increase the initial speed of development, use a starter kit.
Or, you might appreciate the benefits of using a starter kit after first creating Web Components with just plain Javascript.
Personally, I recommend open-wc. Because it has the following tools.
- Testing
- Demoing
- Building
- Linting
Of course, it also supports Typescript.
Catch Up
To catch up on what Web Components are, the MDN site is a good reference.
There is also a Japanese specification for WebComponents (Custom Elements).
The implementation code for the rendering engine blink in Chromium, which is in Chrome, is also available.
Best Practices
Google has published best practices for Custom Elements.
Custom Element Best Practices | Web Fundamentals | Google Developers
For example
Always accept primitive data (strings, numbers, booleans) as either attributes or properties.
As stated above, only pass primitive data to HTML attributes. Rich data like objects and arrays need to be serialized and lose object references.
Testing
To write tests for Web Components, you need to support Shadow DOM. You can use a library that wraps the browser API like JSDOM, but it is more appropriate to use a headless browser. Therefore, @web/test-runner is useful. This test library is one of the same Modern Web as open-wc. @web/test-runner supports Puppeteer, Playwright, and Selenium.
Publish
If you want to publish the Web Components you've created, it's a good idea to read the following article.
Developing Components: Publishing: Open Web Components
In particular, reading what you should not do will make you go, "I see."
❌ Do not optimize
❌ Do not bundle
❌ Do not minify
❌ Do not use .mjs file extensions
❌ Do not import polyfills
For more details, please read the above article.
In conclusion
I hope to be able to use Web Components at a production level.
Share
Related tags
- Defining Fragments Composed in Micro Frontends as Web Components and Sharing them with Module Federation
- Created OEmbed and OGP WebComponents for use on my blog site
- The Goodness of Web Components
- If you're writing in Markdown, Rocket, an SSG that uses WebComponents, is recommended!
- Debuting "Introduction to Web Components for Beginners" at Techbook Fest 7!
- My First Mobile App Development Attempt and Why I Gave Up
- Things I learned when I started using React at work
- Thoughts on Using Ruby on Rails in Business
- Knowing the History Before Learning React
- Writing about what I learned from being infected with the Omicron variant
- Building a TikTok Scraping Infrastructure on GCP and the Challenges Faced
- Learning How Browsers Work
- It's become harder to "ask casually" since remote work started
- What I, an engineer in my late 20s, need to learn from now on
- Everything you need to know about Micro Frontends
- Introducing a Tool for Bulk Updating Account Images and What I Learned
- Everything I Learned About Micro Frontends
- Cotlin is a Tool for Collecting Links on Twitter, Discover Presentations from Around the World
- Why the Combination of FetchAll and RedirectURL in Google Apps Script is Bad
- I tried creating rMinc, a service that registers GMail to GCalendar
- I Tried Making a One-Frame Comic Search Service Tiqav2 (Algolia + Cloudinary + Google Cloud Vision API)
- Sharing All Experiences of First-time Writing at Techbook Fest 7