Skip to content

Commit

Permalink
rendering guide tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 6, 2013
1 parent dfb87a1 commit 297cc67
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 41 deletions.
2 changes: 1 addition & 1 deletion lib/Mojolicious/Controller.pm
Expand Up @@ -967,7 +967,7 @@ invoked once all data has been written.
});
});
For Comet (C<long polling>) you might also want to increase the inactivity
For Comet (long polling) you might also want to increase the inactivity
timeout, which usually defaults to C<15> seconds.
# Increase inactivity timeout for connection to 300 seconds
Expand Down
80 changes: 40 additions & 40 deletions lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -790,10 +790,46 @@ that a response has been generated.
$self->res->content->asset(Mojo::Asset::File->new(path => '/etc/passwd'));
$self->rendered(200);

=head2 Post-processing dynamic content

While post-processing tasks are generally very easy with the C<after_dispatch>
hook, for content generated by the renderer it is a lot more efficient to use
C<after_render>.

use Mojolicious::Lite;
use IO::Compress::Gzip 'gzip';

hook after_render => sub {
my ($self, $output, $format) = @_;

# Check if "gzip => 1" has been set in the stash
return unless $self->stash->{gzip};

# Check if user agent accepts GZip compression
return unless ($self->req->headers->accept_encoding // '') =~ /gzip/i;

# Compress content with GZip
$self->res->headers->content_encoding('gzip');
gzip $output, \my $compressed;
$$output = $compressed;
};

get '/' => {template => 'hello', title => 'Hello', gzip => 1};

app->start;
__DATA__

@@ hello.html.ep
<!DOCTYPE html>
<html>
<head><title><%= title %></title></head>
<body>Compressed content.</body>
</html>

=head2 Chunked transfer encoding

For very dynamic content you might not know the response C<Content-Length> in
advance, that's where the C<chunked> C<Transfer-Encoding> comes in handy. A
For very dynamic content you might not know the response content length in
advance, that's where the C<chunked> transfer encoding comes in handy. A
common use would be to send the C<head> section of an HTML document to the
browser in advance and speed up preloading of referenced images and
stylesheets.
Expand All @@ -814,8 +850,8 @@ L<Mojolicious::Controller/"finish"> marks the end of the stream.
0

Especially in combination with long inactivity timeouts this can be very
useful for Comet (C<long polling>). Due to limitations in some web servers
this might not work perfectly in all deployment environments.
useful for Comet (long polling). Due to limitations in some web servers this
might not work perfectly in all deployment environments.

=head2 Encoding

Expand Down Expand Up @@ -947,42 +983,6 @@ the renderer provides methods to help you with that.
@@ index.html.mine
...

=head2 Post-processing dynamic content

While post-processing tasks are generally very easy with the C<after_dispatch>
hook, for content generated by the renderer it is a lot more efficient to use
C<after_render>.

use Mojolicious::Lite;
use IO::Compress::Gzip 'gzip';

hook after_render => sub {
my ($self, $output, $format) = @_;

# Check if "gzip => 1" has been set in the stash
return unless $self->stash->{gzip};

# Check if user agent accepts GZip compression
return unless ($self->req->headers->accept_encoding // '') =~ /gzip/i;

# Compress content
$self->res->headers->content_encoding('gzip');
gzip $output, \my $compressed;
$$output = $compressed;
};

get '/' => {template => 'hello', title => 'Hello', gzip => 1};

app->start;
__DATA__

@@ hello.html.ep
<!DOCTYPE html>
<html>
<head><title><%= title %></title></head>
<body>Compressed content.</body>
</html>

=head1 MORE

You can continue with L<Mojolicious::Guides> now or take a look at the
Expand Down

0 comments on commit 297cc67

Please sign in to comment.