Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/trendyminds/include-fragment

A zero-dependency, micro utility to make an XHR request for HTML content directly in your HTML!
https://github.com/trendyminds/include-fragment

Last synced: 6 days ago
JSON representation

A zero-dependency, micro utility to make an XHR request for HTML content directly in your HTML!

Awesome Lists containing this project

README

        

# 📦 Include Fragment

## 🤔 What is this?

This package is a fork/reconception of [GitHub's _fantastic_ Include Fragment Element package](https://github.com/github/include-fragment-element). **You should nearly _always_ prefer it to this package**.

## 🤷‍♂️ Why this package then?

While Include Fragment Element is much more robust this:

- 0️⃣  Has **zero dependencies**
- 🚀  Is <1kB (**443 bytes gzipped**)
- 🌍  Does not require a custom elements polyfill
- 🧙‍♂️  Still allows you to dynamically insert and defer fragments

## ⚡️ Setup and use
1. Install using `npm i @trendyminds/include-fragment`
2. Add the appropriate HTML and a valid `data-src` for your XHR request:

### Before
```html



Loading tip...


```

### After
```html


You look nice today



```

## 💡 Tips

- You can defer the loading of a fragment by leaving out the `data-src`. As soon as it's added the MutationObserver will run the XHR request.
- You can provide a custom fallback/error message if the request fails:

### Before
```html



Loading tip...


```

### After
```html


Uh oh!

```

## 🤝 Browser Support
- Chrome
- Firefox
- Safari
- IE 11+