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

strapi-image-downloader

v1.4.1

Published

This package ships a script which downloads images from Strapi. It can be used as prebuild script for static Javascript apps using Strapi as CMS to avoid loading images from remote and instead save them as static assets.

Downloads

173

Readme

Strapi Image Downloader

Package on npmjs.com >>>

This package includes a script that downloads images from Strapi. It's particularly useful as a prebuild script for static JavaScript applications that use Strapi as a CMS. This allows you to store images as static assets, rather than loading them remotely.

  • The script downloads all images that are used in any attribute of any Strapi record. It does not download images that are simply uploaded to the Media Library. Make sure, that the uploads GET endpoint of the Strapi API is set to public!
  • All downloaded images are saved in their original version and in three resized versions (see the created uploads folder).

Installation

npm install strapi-image-downloader

or

yarn add strapi-image-downloader

Usage

saveMediaFromStrapi(strapiUrl, staticPath)

Here, strapiUrl is the absolute URL of the Strapi instance (for example, http://localhost:1337) and staticPath is the output folder, which should be the public or static in your app. Note, that the script creates a subfolder uploads.

Example

In a JavaScript application like SvelteKit or Next,create a prebuild.mjs file in the root directory with the following content:

import { saveMediaFromStrapi } from 'strapi-image-downloader';

saveMediaFromStrapi(process.env.PUBLIC_STRAPI_URL, 'public')

Note: 'public' is the default folder for static files in the app. Adjust it if necessary.

In your package.json, you can set the prebuild script to run prior to building:

  "scripts": {
    "dev": "node prebuild.mjs && next dev",
    "build": "node prebuild.mjs && build"

Now, you can use the image URL provided by Strapi (for example, post.data.attributes.headerImage.data.attributes.url) as the relative path now matches your downloaded image path.

Extra feature: Svelte and React components

In your Svelte (or SvelteKit) or React (only Next.js tested) app, you can use the built in StrapiImage component. For each image it uses the three downscaled versions processed by the saveMediaFromStrapi script in order to build a responsive image using image-srcset.

Usage example SvelteKit

<script>
  import { StrapiImage } from 'strapi-image-downloader';

  export let image;
</script>

<div class="hero-image">
  <StrapiImage class="w-100" src={image.attributes.url} alt="A red cat" />
</div>

Usage example Next.js

import { StrapiImage } from "strapi-image-downloader";

function MyComponent({ data }) {
  return (
    <div class="hero-image">
      <StrapiImage className="w-100" src={image.attributes.url} />
    </div>
  )
}

Development

Run npm link from this repository and npm link strapi-image-downloaderin the project you want to import this package from. After developing, unlink with npm unlink strapi-image-downloader

Make releases

Increae version number in package.json and then run npm publish