Giter VIP home page Giter VIP logo

eleventy-plugin-directus's People

Contributors

lexoyo avatar restyler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

eleventy-plugin-directus's Issues

Downloading (and caching) assets locally instead of hotlinking

I was wondering the best way of downloading directus' assets and storing them locally instead of hotlinking to my directus instance using directus_asseturl

internet2000.net already seems to do this (e.g. https://internet2000.net/optimized/8a7c9b06-0d80-4b3b-9624-ebcb359e8ea3-235.webp and https://internet2000.net/uploads/93c3b71f-16fc-4a88-b883-b7be02a1e919.svg).

After a few hours of tinkering (JS is not my strong suit), I came up with a hacky solution using Eleventy-Fetch that seems to work, although it could be greatly improved:

filters/directus_asset.js

const fs = require('fs');
const EleventyFetch = require("@11ty/eleventy-fetch");


module.exports = async function (img) {
    const directus_c = this.ctx.directus // from https://github.com/11ty/eleventy/issues/813

    let url = `${directus_c.url}/assets/${img.filename_disk}?download&access_token=${directus_c.token}`;

    let imageBuffer = await EleventyFetch(url, {
        duration: "1d",
        type: "buffer"
    });

    let folder_path = `./public/img/${img.folder.name}/`
    let path = `${folder_path}${img.filename_download}`
    fs.mkdirSync(folder_path, { recursive: true })
    fs.writeFile(path, imageBuffer, "binary", (err) => err && console.error(err))

    let final_path = `/img/${img.folder.name}/${img.filename_download}`
    return final_path;
};

.eleventy.js

...
  eleventyConfig.addAsyncFilter('directus_asset', require('./filters/directus_download.js'));
...

template.njk

{% for cliente in directus.collections.Clientes %}
      <div><img src="{{ cliente.Logo | directus_asset }}" alt="{{ cliente.Nome }}"></div>
{% endfor %}

I'm not sure if this is the best way of doing this, but I was wondering if it would be possible to share how Internet2000 does it or implement a native function for doing this within the plugin.

Road map

Here is what I am working on:

  • better way to fetch related data: fetch all collections without the related data and provide filter to get related data (Here is a discussion about why and how difficult it is)
  • provide a filter to get related data: {% assign page = item | directus_relation: 'link' %}
  • provide a filter to get translation: {% assign translated = item | directus_translate: 'fr' %}
  • provide a filter to get assets URL and optionally download it from directus to eleventy's asset folder: <img src="{{ item.image | directus_asseturl: true }}"> #3
  • use eleventy fetch plugin to fetch directus data
  • doc to use with 11ty image plugin #3

cc @eitchtee

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.