logoalt Hacker News

miragecraft05/03/20252 repliesview on HN

I too lamented the loss of HTML imports and ended up coming up with my own JavaScript library for it.

https://miragecraft.com/blog/replacing-html-imports

At the end of the day it’s not something trivial to implement at the HTML spec/parser level.

For relative links, how should the page doing the import handle them?

Do nothing and let it break, convert to absolute links, or remap it as a new relative link?

Should the include be done synchronously or asynchronously?

The big benefit of traditional server side includes is that its synchronous, thus simplifying logic for in-page JavaScript, but all browsers are trying to eliminate synchronous calls for speed, it’s hard to see them agreeing to add a new synchronous bottleneck.

Should it be CORS restricted? If it is then it blocks offline use (file:// protocol) which really kills its utility.

There are a lot of hurdles to it and it’s hard to get people to agree on the exact implementation, it might be best to leave it to JavaScript libraries.


Replies

DemocracyFTW205/04/2025

My guess is that some or maybe all of your concerns should have been solved by CSS @import (https://developer.mozilla.org/en-US/docs/Web/CSS/@import) although, as I'm reading the first few lines of the linked article, those must appear near the top of a CSS file, so are significantly more restricted than an import that can appear in the middle of a document.

paul_h05/04/2025

Someone else made the same - https://github.com/Paul-Browne/HTMLInclude - but it's not been updated in 7 years, leaving questions. I'll try yours and theirs in due course. Err, and the fragment @HumanOstrich said elsewhere in comments.