The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Plack::Middleware::BasicStyle - Add a basic <style> element to pages that don't have one

SYNOPSIS

  # Basic usage (all default options)
  use Plack::Builder;
  builder {
    enable 'BasicStyle';
    ...
  }

  # Default options set explicitly
  use Plack::Builder;
  builder {
    enable 'BasicStyle',
      style => $Plack::Middleware::BasicStyle::DEFAULT_STYLE,
      any_content_type => '',
      even_if_styled   => '',
      use_link_header  => '';
    ...
  }

  # Custom options
  use Plack::Builder;
  builder {
    enable 'BasicStyle',
      style => '<style>body { background-color: #ddd }</style>',
      any_content_type => 1,
      even_if_styled   => 1,
      use_link_header  => '/basic-style.css';
    ...
  }

DESCRIPTION

Plack::Middleware::BasicStyle is a Plack middleware that adds a basic <style> element to HTML pages that do not have a stylesheet.

The default style, taken from http://bettermotherfuckingwebsite.com, is (before minification):

  <style>
    body {
      margin:40px auto;
      max-width: 650px;
      line-height: 1.6;
      font-size:18px;
      color:#444;
      padding:0 10px
    }

    h1,h2,h3 {
      line-height:1.2
    }
  </style>

The middleware takes the following arguments:

style

This is the HTML fragment that will be added to unstyled pages.

It defaults to the value of $Plack::Middleware::BasicStyle::DEFAULT_STYLE.

any_content_type

If true, don't check whether Content-Type contains text/html.

If false (default), passes the response through unchanged if the Content-Type header is unset or does not contain the case-insensitive substring text/html.

even_if_styled

If true, don't check whether the response already includes a <style> or <link ... rel="stylesheet"> element.

If false (default), passes the response through unchanged if the response includes a <style> or <link ... rel="stylesheet"> element.

If false or unset (default), the given HTML fragment will be added right after the <head> start tag (if this exists), right after the <html> start tag (if this exists but <head> doesn't), or at the beginning of the document (if neither <html> nor <head> exists).

If set, its value is interpreted as an URL path. The body of the response will not be modified, instead a Link: HTTP header will be added to unstyled pages. Additionally, the middleware will intercept requests to that exact URL path and return the style (with status 200, a Content-Type of text/css, a correct Content-Length header, and a Cache-Control header instructing the browser to cache the style for 30 days).

Setting this makes the module more resilient to bugs and more efficient at the cost of asking the client to make an extra request. Therefore setting this argument is recommended.

CAVEATS

This middleware only works with simple (non-streaming) responses, where the body is an arrayref.

In other words, responses where the body is an IO::Handle, or streaming/delayed responses are NOT supported and will be passed through unchanged by this middleware.

SEE ALSO

http://bettermotherfuckingwebsite.com

AUTHOR

Marius Gavrilescu, <marius@ieval.ro>

COPYRIGHT AND LICENSE

Copyright (C) 2016-2017 by Marius Gavrilescu

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.24.0 or, at your option, any later version of Perl 5 you may have available.