OSDN Git Service

Plugin::JavaScriptLoader: support pre-gzipped
authorhylom <hylom@users.sourceforge.jp>
Wed, 3 Oct 2018 10:03:33 +0000 (19:03 +0900)
committerhylom <hylom@users.sourceforge.jp>
Wed, 3 Oct 2018 10:03:33 +0000 (19:03 +0900)
src/newslash_web/lib/Newslash/Plugin/JavaScriptLoader.pm

index 8cdf534..d1901b4 100644 (file)
@@ -96,13 +96,11 @@ sub _load_js {
     my $js_body = do { local($/); <$fh> } ;
     close($fh);
 
-    my $md5;
     if (utf8::is_utf8($js_body)) {
-        $md5 = md5_hex(Encode::encode('utf8', $js_body));
-    }
-    else {
-        $md5 = md5_hex($js_body);
+        $js_body = encode_utf8($js_body);
     }
+    my $md5 = md5_hex($js_body);
+
     my $path;
     if ($self->{mode} eq "development") {
         my $basedir = $self->{conf}->{source_directory};
@@ -121,6 +119,10 @@ sub _load_js {
                    path => $path,
                    type => "text/javascript",
                  };
+    if ($self->{compress}) {
+        gzip(\$js_body, \my $compressed);
+        $result->{gz_content} = $compressed;
+    }
     return $result;
 }
 
@@ -158,13 +160,10 @@ sub _pack_files {
     }
 
     my $joined = join("\n", @result);
-    my $md5;
     if (utf8::is_utf8($joined)) {
-        $md5 = md5_hex(Encode::encode('utf8', $joined));
-    }
-    else {
-        $md5 = md5_hex($joined);
+        $joined = encode_utf8($joined);
     }
+    my $md5 = md5_hex($joined);
     $self->{app}->log->debug("JavaScriptLoader: packing to $filename done. md5: $md5");
 
     my @sources = keys %done;
@@ -180,6 +179,10 @@ sub _pack_files {
                    type => "text/javascript",
                    sources => \@sources,
                  };
+    if ($self->{compress}) {
+        gzip(\$joined, \my $compressed);
+        $result->{gz_content} = $compressed;
+    }
     return $result;
 }
 
@@ -309,26 +312,22 @@ sub register {
                         }
                     }
                     if ($content) {
+                        my $output = $content->{content};
                         if ($self->{compress}
                             && $content->{type} =~ m{^text/}
                             && ($c->req->headers->accept_encoding // '') =~ /gzip/i
+                            && $content->{gz_content}
                            ) {
                             # use gzip compression
                             # Add header
                             $c->res->headers->append(Vary => 'Accept-Encoding');
                             $c->res->headers->content_encoding('gzip');
 
-                            # Compress content with gzip
-                            my $gzip_in = encode_utf8($content->{content});
-                            gzip(\$gzip_in, \my $compressed);
-                            $c->res->headers->content_type($content->{type});
-                            $c->res->body($compressed);
-
-                        }
-                        else {
-                            $c->res->headers->content_type($content->{type});
-                            $c->res->body(encode_utf8($content->{content}));
+                            $output = $content->{gz_content};
                         }
+
+                        $c->res->headers->content_type($content->{type});
+                        $c->res->body($output);
                         $c->rendered(200);
                         return;
                     }