Code Monkey home page Code Monkey logo

plack-middleware-precompressed's Introduction

NAME

Plack::Middleware::Precompressed - serve pre-gzipped content to compression-enabled clients

SYNOPSIS

use Plack::Builder;

builder {
    enable 'Precompressed', match => qr!\.js\z!;
    $handler;
};

DESCRIPTION

Plack::Middleware::Precompressed is an alternative (or rather, complement) to middlewares like Deflater, which will compress response bodies on the fly. For dynamic resources, that behaviour is necessary, but for static resources it is a waste: identical entities will be compressed over and over. Instead, Precompressed allows you to compress static resources once, e.g. as part of your build process, and then serve the compressed resource in place of the uncompressed one for compression-enabled clients.

To do so, by default it appends a .gz suffix to the PATH_INFO and tries to serve that. If that fails, it will then try again with the unmodified URI.

Note: this means requests for resources that are not pre-compressed will always be dispatched twice. You are are advised to use either the match parameter or the Conditional middleware or something of the sort, to prevent requests from passing through this middleware unnecessarily.

CONFIGURATION OPTIONS

match

Specifies a regex that must match the PATH_INFO to trigger the middleware.

rules

A callback that is expected to transform the path instead of using the default behaviour of appending .gz to the file path. PATH_INFO will be aliased to the $_ variable, so you can do something like this:

enable 'Precompressed', match => qr!\.js\z!, rules => sub { s!^/?!/z/! };

This example will prepend /z/ to file paths instead of appending .gz to them.

env_keys

An array of PSGI environment key names. If you specify any, then the rules callback will receive a reference to a hash with just these keys, aliased to the values in the PSGI environment that will be passed to the wrapped app. You can modify these values to modify the environment it will see. This allows you do to something like this:

enable 'Precompressed', env_keys => [ 'HTTP_HOST' ],
        rules => sub { $_[0]{'HTTP_HOST'} = 'gzip.assets.example.com' };

This somewhat peculiar interface is necessary so the middleware can abstract away the details of trying to copy as little data as possible during a request.

SUBCLASSING

If you reuse a particular configuration of Plack::Middleware::Precompressed in many projects, you can avoid repeating the same configuration in each of them by subclassing this middleware and overriding the rewrite and env_keys methods.

The rewrite method will be called just as the rules callback would be.

The env_keys method should return an array reference and will have the same effect on the rewrite method as the configuration option on the rules callback.

SEE ALSO

Plack::Middleware::Deflater

plack-middleware-precompressed's People

Contributors

ap avatar

Watchers

 avatar  avatar  avatar

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.