npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@aaashur/eleventy-plugin-add-remote-data

v1.0.0

Published

An [Eleventy](https://11ty.dev/) plugin for fetching remote JSON data from one or more endpoints and exposing each response as a [global data](https://www.11ty.dev/docs/data-global-custom/) variable.

Downloads

21

Readme

eleventy-plugin-add-remote-data

An Eleventy plugin for fetching remote JSON data from one or more endpoints and exposing each response as a global data variable.

Setup

Run the following command at the root of your Eleventy project:

npm install @aaashur/eleventy-plugin-add-remote-data

Next, include the plugin in your Eleventy config file:

const addRemoteData = require("@aaashur/eleventy-plugin-add-remote-data");

module.exports = (eleventyConfig) => {
    eleventyConfig.addPlugin(addRemoteData, {
        data: {
            // See "Usage" below
        },
    });
};

Usage

Use the data property of the plugin options object to define the name of one or more global data variables and the remote URL of its source data.

For example, the following configuration:

eleventyConfig.addPlugin(addRemoteData, {
    data: {
        robots: "https://api.ashur.cab/robots/v2.json"
    },
});

would create a global data variable named robots that you might use in a template like this:

---
permalink: /robots.txt
---
{%- for robot in robots.disallow -%}
User-agent: {{ robot }}
Disallow: /

{% endfor -%}

Adding a second data property coinToss would create a global data variable named coinToss:

    data: {
        coinToss: "https://coin-toss.netlify.app/api/v1.json",
        robots: "https://api.ashur.cab/robots/v2.json"
    },

etc.

Configuration

This plugin uses @11ty/eleventy-fetch under the hood, and accepts all the same options.

In addition to the top-level data property, you can set an options property to adjust default behaviors.

eleventyConfig.addPlugin(addRemoteData, {
    data: {
        // ...
    },
    options: {
        // ...
    },
});

Cache

By default, eleventy-fetch caches results for 1 day and stores them in a directory called .cache.

To use a different directory or duration, use the options object to set one or both for all endpoints:

eleventyConfig.addPlugin(addRemoteData, {
    data: {
        // ...
    },
    options: {
        directory: "different-cache-directory",
        duration: "30d",
    },
});

If you're working with global data variables that have different requirements, you can define options on an individual basis:

eleventyConfig.addPlugin(addRemoteData, {
    data: {
        coinToss: {
            options: {
                duration: "0d",

                // Because we haven't defined `directory`, this endpoint will
                // inherit the "different-cache-directory" value from default
                // options defined below
            },

            url: "https://coin-toss.netlify.app/api/v1.json",
        },

        robots: "https://api.ashur.cab/robots/v2.json",
    },

    options: {
    	// Default options
        directory: "different-cache-directory",
        duration: "30d",
    },
});

Options for individual endpoints will be merged with default options, allowing you to fine-tune just the properties you need.

Type

For convenience, this plugin assumes a valid JSON response by default — if it encounters an invalid payload, an exception will be thrown.

To switch to another type supported by eleventy-fetch, you can set type on both the top-level options object:

eleventyConfig.addPlugin(addRemoteData, {
    data: {
        // ...
    },
    options: {
        type: "text",
    },
}

and on a per-endpoint basis:

eleventyConfig.addPlugin(addRemoteData, {
    data: {
        coinToss: {
            type: "json",
            url: "https://coin-toss.netlify.app/api/v1.json",
        },
    },
    options: {
        type: "text",
    },
}

Security & Privacy

If you haven't worked with eleventy-fetch before, please be sure to read (and heed) this warning:

Important Security and Privacy Notice

This plugin caches complete network responses. Unless you’re willing to perform a full review of everything this plugin caches to disk for privacy and security exposure, it is strongly recommended that you add the .cache folder to your .gitignore file so that network responses aren’t checked in to your git repository.

FAQ

Why choose this over using eleventy-fetch directly?

If you find yourself writing global data files that are largely identical, fetching remote JSON data and exporting the results directly, this plugin can help eliminate a lot of the friction in getting set up.

If, however, your needs are more complex — ex., the remote data must be processed or sanitized, or you’re fetching raw image data that needs to be optimized — using eleventy-fetch directly is definitely the right choice!