Git Product home page Git Product logo

astro-remote-picture's Introduction

astro-remote-pictures

This integration enables you to use dynamic images with the performance benefits of the <Picture /> and <Image /> components of Astro.

Installation

npm i astro-remote-pictures

Basic example

This example just uses static URLs to demonstrate the basic usage of the integration. It doesn't make much sense to use this for static URLs, but it's a good starting point to understand how it works. See the dynamic example below.

Configuration

import { defineConfig } from "astro/config";
import remotePictures from "astro-remote-pictures";

export default defineConfig({
  integrations: [
    remotePictures({
      collections: [
        {
          id: "backgrounds",
          pictures: [
            {
              id: "PolarBear",
              url: "https://images.unsplash.com/photo-1709048260183-44acb7826928",
            },
          ],
        },
      ],
    }),
  ],
});

Usage

---
import { Picture } from "astro:assets";
import { PolarBear } from "astro-remote-pictures/wallpapers";
---

<Picture
  src={PolarBear}
  alt="A polar bear"
/>

The PolarBear import is a reference to the picture with the ID PolarBear from the backgrounds collection. The integration automatically generates these references for you. The import path is always astro-remote-pictures/$collectionId.

Dynamic example

This integration really starts to make sense once you want to use images dynamically fetched from an CMS or any other API.

Configuration

import { defineConfig } from "astro/config";
import remotePictures, { toPictureId } from "astro-remote-pictures";

// Fetch wallpapers from API
const wallpapers = await fetch(...)

export default defineConfig({
  integrations: [
    remotePictures({
      fetchOptions: {
        headers: {
            "Authorization": "Bearer super_secret_token"
        }
      },
      collections: [
        {
          id: "wallpapers",
          pictures: wallpapers.map(wallpaper => ({
            id: toPictureId(wallpaper.name),
            url: wallpaper.url,
          })),
        },
      ],
    }),
  ],
});

Usage

---
import { Picture } from "astro:assets";
import { toPictureId } from "astro-remote-pictures";

// Import all pictures from the wallpapers collection
import * as wallpaperRefs from "astro-remote-pictures/wallpapers";

// Fetch wallpapers from API
const wallpapers = fetch(...)
---

{wallpapers.map(wallpaper => (
  <Picture
    src={wallpaperRefs[toPictureId(wallpaper.name)]}
    alt={wallpaper.name}
  />
))}

Notes

You can move the fetch call to a separate file and import it in your Astro config file and the Astro component to remove code duplication.

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.