question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Web Component custom attributes missing when using Preact

See original GitHub issue

Greetings, I’ve found a possible issue when using Preact with Web Components (Custom Elements) where custom HTML attributes are not being assigned to the element when rendered by Preact. Originally I had created this as a React demo to make sure the Components work with React so the code is almost identical except the Preact version loads Preact instead of React of course.

Alternatively perhaps the error is with the Web Component and React is just handling it but so far in my research everything with the Web Component seems good and it works well with regular HTML.

Reproduction

https://codepen.io/conrad-sollitt/pen/JjREjQg

Preact Version for the Demo

<script src="https://unpkg.com/preact@10.5.7/dist/preact.min.js"></script>

<!-- Page also includes this so it works with Preact DevTools -->
<script src="https://unpkg.com/preact@10.5.7/debug/dist/debug.umd.js"></script>
<script src="https://unpkg.com/preact@10.5.7/devtools/dist/devtools.umd.js"></script>

Original Demo (Preact) https://www.dataformsjs.com/examples/web-components-with-preact.htm

Based on React Version that Works Both files share the same JSX https://www.dataformsjs.com/examples/web-components-with-react.htm

Steps to reproduce

  • Use either the CodePen or original link I provided and click the Data Page from the top nav.
  • It should show the error below (Chrome Version 87) and in DevTools the attribute [url] is indeed missing.

image

Expected Behavior

  • Here is the React Version (link above), when you click on the same Data Page it works because the the url custom HTML attribute is included.

image

Additional Notes

HTML Source for the Demo https://github.com/dataformsjs/dataformsjs/blob/master/examples/web-components-with-preact.htm

JSX Source for the Demo https://github.com/dataformsjs/dataformsjs/blob/master/examples/web-components-with-react.jsx

Source for the <json-data> Web Component https://github.com/dataformsjs/dataformsjs/blob/master/js/web-components/json-data.js The minified version is simply minified using Terser and not compiled down to ES5 or earlier versions of JS

Source for working <markdown-content> Web Component https://github.com/dataformsjs/dataformsjs/blob/master/js/web-components/markdown-content.js Intresting the Markdown page is working and it too uses a url attribute Both files have a similar layout and:

class {name} extends HTMLElement ...
  static get observedAttributes() {
    return ['url', '..'];
  }

I’ll still do some more work to see if I can track down the issue myself.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
weaintplasticcommented, Dec 9, 2021

Preact’s behavior of not rendering attributes to custom elements creates issues in our isomorphic environment.

We pre-render web components by hydrating them on the server-side. In a nutshell, web components are rendered with light DOM and scoped CSS instead of Shadow DOM. This way we can guarantee that our web components are displayed on the client-side before the application is available.

The server-side hydration of web components operates on a DOM that was created using preact. Because of the missing attributes on custom elements, the hydration is missing meaningful instructions that otherwise would be available when added as attributes.

It seems that the React team stumbled upon this caveat while working on web component support. It would be great if this issue could be reopened to continue the discussion to set attributes rather than assigning property values for web components when rendered with Preact.

0reactions
havrancommented, Nov 12, 2021

@ConradSollitt Hi, thanks for answer.

I try recreate same bug, with simple web component and basic NextJS app, which I create, but without success. Simple component work.

Seems like problem is related with component which we use, because when I try use same component, then I get same problem.

When I found some more, I add this info here. Thanks again.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using web-components within Preact and typescript
With typescript 4.2.3 and preact 10.5.13, here is what works to define a custom tag name with attributes: declare module 'preact' ...
Read more >
Web Components | Preact
Custom Elements generally rely on custom properties in order to support setting complex values that can't be expressed as attributes. This works well...
Read more >
Handling data with Web Components | by Andreas Remdt
#1 — Using attributes. Attributes are the easiest way to pass data into a custom element: <custom-list filter="some filter"></ ...
Read more >
Using Custom Elements in Svelte | CSS-Tricks
This is because size is a property on the element, but class is not. Because Svelte detects a size property, it chooses to...
Read more >
All the Ways to Make a Web Component - Feb 2022 Update
We're also pleased to welcome the 60th variant with node-projects/base-custom-webcomponent . Update 02/18/2022: We forgot to include one ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found