include-file

Load remote HTML fragments with progressive enhancement. Fallback content shows until fetch completes.

Overview

The <include-file> component fetches HTML from a URL and injects it. Any existing content serves as a fallback shown until the fetch completes (or if it fails).

Attributes

AttributeValuesDefaultDescription
srcstringURL to fetch HTML from
mode"replace", "append", "prepend"replaceHow fetched content is inserted
lazybooleanDefer loading until element enters viewport
allow-scriptsbooleanRe-execute inline scripts in loaded content (trusted sources only)

Lazy Loading

Add lazy to defer the fetch until the element scrolls into view (with a 200px root margin).

Insert Modes

Control how the fetched HTML is inserted with mode.

Events

EventDetailDescription
include-file:loaded{ src, html }Fired after successful content load
include-file:error{ src, error }Fired if fetch fails

JavaScript API

Method Description
reload() Re-fetch content from the current src.

Progressive Enhancement

Without JavaScript, the fallback content (whatever HTML is inside the element) remains visible. The src URL is never fetched, so users see the fallback. This makes it safe to use for non-critical content.

Accessibility

  • Fallback content ensures content is always available.
  • Error state sets data-error on the element. The original fallback content is restored on first-load failure. Style [data-error] in CSS to show a visible error state.
  • Changing the src attribute triggers a re-fetch, enabling dynamic content updates.

Trust Boundary

This component injects HTML via innerHTML. It is designed for loading trusted first-party fragments (partials, includes from your own server). Do not use it to load untrusted or user-generated HTML from third-party sources.

The allow-scripts attribute is an additional opt-in that re-executes inline scripts in loaded content. Only use it when you control the source URL.

Related

  • <iframe> — Embeds an entire document (heavier, sandboxed)