Plack:...eflater(3pm)

πŸ“¦
version 0.14 Β· 3 versions
, in debian trixie, ubuntu resolute, ubuntu stonking

πŸ’– Plack::Middleware::Deflater πŸ’–

okay bestie, gather round, because this is Plack::Middleware::Deflater from the Perl world (User Contributed Perl Documentation, we love her) and she does exactly one thing extremely well: squishing your response body down with Gzip or Deflate. 🌸


✨ NAME ✨

Plack::Middleware::Deflater β€” compress that response body with Gzip or Deflate. that's the whole assignment. πŸŽ€


πŸ¦‹ SYNOPSIS πŸ¦‹

here's the vibe, verbatim, no notes:

  use Plack::Builder;

  builder {
    enable sub {
        my $app = shift;
        sub {
            my $env = shift;
            my $ua = $env->{HTTP_USER_AGENT} || '';
            # Netscape has some problem
            $env->{"psgix.compress-only-text/html"} = 1 if $ua =~ m!^Mozilla/4!;
            # Netscape 4.06-4.08 have some more problems
             $env->{"psgix.no-compress"} = 1 if $ua =~ m!^Mozilla/4\.0[678]!;
            # MSIE (7|8) masquerades as Netscape, but it is fine
            if ( $ua =~ m!\bMSIE (?:7|8)! ) {
                $env->{"psgix.no-compress"} = 0;
                $env->{"psgix.compress-only-text/html"} = 0;
            }
            $app->($env);
        }
    };
    enable "Deflater",
        content_type => ['text/css','text/html','text/javascript','application/javascript'],
        vary_user_agent => 1;
    sub { [200,['Content-Type','text/html'],["OK"]] }
  };

like. look at that. it's giving user-agent archaeology and honestly the Netscape lore is sending me. 😭


πŸ’… DESCRIPTION πŸ’…

Plack::Middleware::Deflater is a middleware that encodes your response body in gzip or deflate, and it decides to do that based on the Accept-Encoding HTTP request header. she reads the room before she acts. queen behavior. πŸ‘‘

it would save the bandwidth a little bit but it should increase the Plack server load, so ideally you should handle this on the frontend reverse proxy servers. like, let the bouncer do it, not the person actually running the party. 🫢

also, important: this middleware removes Content-Length and streams encoded content. which means the server should support HTTP/1.1 chunked response, or downgrade to HTTP/1.0 and close the connection. no cap, that's just how it is.

🌈 CONFIGURATIONS 🌈

πŸ“ content_type

  content_type => 'text/html',
  content_type => [ 'text/html', 'text/css', 'text/javascript', 'application/javascript', 'application/x-javascript' ]

this is the Content-Type header to apply deflater on. and if content-type is not defined, Deflater will try to deflate all contents. she's an overachiever, we respect it. ✨

🧚 vary_user_agent

  vary_user_agent => 1

adds User-Agent to the Vary header. that's it, that's the whole option. πŸ’œ


⭐ ENVIRONMENT VALUE ⭐

psgix.no-compress

do not apply deflater. πŸ™ˆ

psgix.compress-only-text/html

apply deflater only if content_type is text/html. picky, but make it fashion. πŸ’…

plack.skip-deflater

skip all Deflater features. the full ghost. πŸ’€

🌸 Compare psgix.no-compress with plack.skip-deflater

okay this distinction is lowkey the most important part of the whole page, so pay attention bestie:

  • if no-compress is true, PM::Deflater skips gzip or deflate. but it still adds Vary: Accept-Encoding and Vary: User-Agent headers.
  • skip-deflater forces it to skip all PM::Deflater features β€” it doesn't allow the Vary header to be added.

so one of them is "don't do the thing but still sign the guestbook" and the other is "leave the building entirely." different energies fr fr. πŸ”₯


πŸŽ€ LICENSE πŸŽ€

this software is licensed under the same terms as Perl itself. periodt. πŸ‘‘


πŸ’– AUTHOR πŸ’–

Tatsuhiko Miyagawa β€” icon, legend, the moment. 🫢


πŸ”₯ SEE ALSO πŸ”₯


cute sign-off time!! this page came to you via perl v5.38.2, dated 2024-05-15. mwah, go compress something responsibly bestie πŸ’‹πŸ¦‹πŸŒΈ
πŸ“¦ where this page comes from