I am creating a web component using native implementation, which in it's html template has links to images. However, those links only work if they are absolute, or relative to the main document, which means, that that component is not reusable, or portable. Also, it is very counterintuitive.
Currently, I add a data-url_prefix attribute to all elements that need to use images. Then, when creating a shadow root for my custom element, I replace a {{URL_PREFIX}} with the value of that argument.
My solution seems very bad. I would be very glad if you advised something better, thanks.
I found an interesting quote on the http://webcomponents.org/polyfills/html-imports/ page:
POLYFILL NOTES
In imported documents, href and src attributes in HTML, and url properties in CSS files, are relative to the location of the imported document, not the main document.
why would a polifill use different logic that the native implementation?
Web Components Ideally should encapsulate all their dependencies, but if a web component requires an image, it should know the absolute URL to that image, which does not allow the component to be simply moved around in file structure.
Say, for example I have the following structure:
- index.html
- css
- main.css
- js
- main.js
- web_components
- cool_web_component
- cool_web_component.html
- icon.png
- cool_web_component
If I change it to the following:
- index.html
- css
- main.css
- js
- main.js
- cool_web_component
- cool_web_component.html
- icon.png
I would be required to change the pointer to icon.png somewhere in those files My question is how to avoid it, or solve it in elegant way. Also, why the actual native implementation is in conflict with the polyfills?