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

express-svg-fallback

v0.1.4

Published

Middleware for node.js Express to convert SVG images to PNG format on the fly when the request comes from a legacy browser.

Downloads

10

Readme

express-svg-fallback

express-svg-fallback is a middleware for node.js Express that handles requests for SVG files. If the requesting browser does not support SVG, the file is converted to PNG format and the converted content is served.

Installation

Use Node Package Manager (npm) to download the module and install it in your project:

npm install express-svg-fallback --save

Usage

In your Express application, simply require the module and configure Express to use it as middleware. Example:

var express = require('express');
var svgFallback = require('express-svg-fallback');

var app = express();
app.use(svgFallback({
    // options
}));

app.listen(process.env.PORT || 80);

If you are including middleware for serving static files, express-svg-fallback must be used before the other middleware. Example:

var app = express();
app.use(svgFallback());
app.use(express.static('assets'));
// add other middleware

Whenever a request comes from a browser that does not support rendering of SVG files (IE8 or older, Android 2.3 or older), the module handles the request. The files are converted on the fly and then stored to disk. All subsequent requests for the same file will not result in a new conversion, instead, the previously generated PNG file will be served.

Initialisation options:

  • fallbackPath (string): specifies the disk location (relative to the root path of the Express application) where the converted PNG files will be saved. To avoid filename clashes, the directory will contain a subtree that replicates the path of the request. For example, whenever requesting for a file with path /assets/images/svg/logo.svg, the converted file will be saved (with default settings) to /svg-fallback/assets/images/svg/logo.svg.png. The default value for this setting is svg-fallback.

  • logger (object): by default, the module will print errors and debug information to the standard output using a normal console.error or console.log. If your application uses a custom logging library (like Winston or Bunyan, or any other similar library, as long as it supports the methods error, info and debug), you can pass the logger object via this property, so that all logging will be performed through it instead of the standard method.

  • debug (boolean): when set to true, the module will print debug information to the standard output. When set to false, it will only print errors. Default value is false. Note: when passing a custom logger object (see definition above), the value for the debug property will be ignored, and the log level settings for the specified logger object will be used instead.

URL query string options

It is possible to let the module serve the converted PNG files even to browsers that natively support rendering of SVG files.

  • To always get the PNG version of a file, add the ?type=png query string to the URL of the SVG file. For example http://www.example.com/image.svg?type=png will return the PNG version even when called from Chrome or Firefox.

  • To force the conversion of an image (ignoring any previously executed conversion), use the query string ?force=true.

Tests

Tests are provided to verify the correct execution of the middleware. To run those tests:

  • from the Console (or Command Prompt in Windows), navigate to the directory where the project has been saved

  • make sure that all the dependencies have been installed, by running npm install. This is only required once, and only in cases where the module has been installed using a method different from npm install express-svg-fallback - like cloning the GitHub repo or extracting from zip.

  • run npm test