Permalink
Browse files

Use a separate setting for CSS font inlining threshold (with a larger…

… default),

since current for-Chrome CSS produced by fonts.g.c is quite large.

Addresses https://code.google.com/p/modpagespeed/issues/detail?id=1040
issue #1040

backported from f62b555
  • Loading branch information...
crowell committed May 11, 2015
1 parent 9d6d087 commit 01045a9d333e4b6e0ef2c3a04826a3d15f9c277b
@@ -296,11 +296,18 @@ fetch_until $URL 'grep -c <style' 3
# restrictions on external connections
if [ -z ${DISABLE_FONT_API_TESTS:-} ]; then
test_filter inline_google_font_css Can inline Google Font API loader CSS
fetch_until $URL 'grep -c @font-face' 1
# Use a more recent version of Chrome UA than our default, which will get
# a very large (which hit our previous default size limits) CSS using woff2
WGETRC_OLD=$WGETRC
export WGETRC=$TESTTMP/wgetrc-chrome
cat > $WGETRC <<EOF
user_agent =Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.45 Safari/537.36
EOF

fetch_until $URL 'grep -c @font-face' 7

# By default we use a Chrome UA, so it should get woff
OUT=$($WGET_DUMP $URL)
check_from "$OUT" fgrep -qi "format('woff')"
check_from "$OUT" fgrep -qi "format('woff2')"
check_not_from "$OUT" fgrep -qi "format('truetype')"
check_not_from "$OUT" fgrep -qi "format('embedded-opentype')"
check_not_from "$OUT" fgrep -qi ".ttf"
@@ -32,6 +32,8 @@ namespace net_instaweb {
GoogleFontCssInlineFilter::GoogleFontCssInlineFilter(RewriteDriver* driver)
: CssInlineFilter(driver) {
set_id(RewriteOptions::kGoogleFontCssInlineId);
set_size_threshold_bytes(
driver->options()->google_font_css_inline_max_bytes());
driver->AddResourceUrlClaimant(
NewPermanentCallback(
this, &GoogleFontCssInlineFilter::CheckIfFontServiceUrl));
@@ -127,10 +127,11 @@ class GoogleFontCssInlineFilterSizeLimitTest
protected:
virtual void SetUp() {
GoogleFontCssInlineFilterTestBase::SetUp();
// GoogleFontCssInlineFilter honors css_inline_max_bytes.
// GoogleFontCssInlineFilter uses google_font_css_inline_max_bytes.
// Set a threshold at font_safieri, which should prevent longer
// font_chromezilla from inlining.
options()->set_css_inline_max_bytes(STATIC_STRLEN("font_safieri"));
options()->set_google_font_css_inline_max_bytes(STATIC_STRLEN(
"font_safieri"));
SetUpForFontFilterTest(RewriteOptions::kInlineGoogleFontCss);
}
};
@@ -21,6 +21,7 @@

#include <cstddef>

#include "net/instaweb/rewriter/cached_result.pb.h"
#include "net/instaweb/http/public/semantic_type.h"
#include "net/instaweb/rewriter/public/common_filter.h"
#include "net/instaweb/rewriter/public/css_tag_scanner.h"
@@ -30,6 +31,8 @@
#include "net/instaweb/util/public/basictypes.h"
#include "net/instaweb/util/public/string.h"
#include "net/instaweb/util/public/string_util.h"
#include "pagespeed/kernel/html/html_element.h"
#include "pagespeed/kernel/http/google_url.h"

namespace net_instaweb {

@@ -75,6 +78,8 @@ class CssInlineFilter : public CommonFilter {
// for semantics.
virtual ResourcePtr CreateResource(const char* url, bool* is_authorized);

void set_size_threshold_bytes(size_t size) { size_threshold_bytes_ = size; }

private:
class Context;
friend class Context;
@@ -87,7 +92,7 @@ class CssInlineFilter : public CommonFilter {
HtmlElement* element);

const char* id_; // filter ID code.
const size_t size_threshold_bytes_;
size_t size_threshold_bytes_;

GoogleString domain_;
CssTagScanner css_tag_scanner_;
@@ -261,6 +261,7 @@ class RewriteOptions {
static const char kFlushBufferLimitBytes[];
static const char kFlushHtml[];
static const char kFlushMoreResourcesEarlyIfTimePermits[];
static const char kGoogleFontCssInlineMaxBytes[];
static const char kForbidAllDisabledFilters[];
static const char kHideRefererUsingMeta[];
static const char kIdleFlushTimeMs[];
@@ -608,6 +609,7 @@ class RewriteOptions {
static const int64 kDefaultCssFlattenMaxBytes;
static const int64 kDefaultCssImageInlineMaxBytes;
static const int64 kDefaultCssInlineMaxBytes;
static const int64 kDefaultGoogleFontCssInlineMaxBytes;
static const int64 kDefaultCssOutlineMinBytes;
static const int64 kDefaultImageInlineMaxBytes;
static const int64 kDefaultJsInlineMaxBytes;
@@ -1277,6 +1279,12 @@ class RewriteOptions {
void set_css_inline_max_bytes(int64 x) {
set_option(x, &css_inline_max_bytes_);
}
int64 google_font_css_inline_max_bytes() const {
return google_font_css_inline_max_bytes_.value();
}
void set_google_font_css_inline_max_bytes(int64 x) {
set_option(x, &google_font_css_inline_max_bytes_);
}
int64 js_inline_max_bytes() const { return js_inline_max_bytes_.value(); }
void set_js_inline_max_bytes(int64 x) {
set_option(x, &js_inline_max_bytes_);
@@ -3365,6 +3373,7 @@ class RewriteOptions {
Option<int64> css_image_inline_max_bytes_;
Option<int64> css_inline_max_bytes_;
Option<int64> css_outline_min_bytes_;
Option<int64> google_font_css_inline_max_bytes_;

// Preserve URL options
Option<bool> css_preserve_urls_;
@@ -136,6 +136,8 @@ const char RewriteOptions::kFlushMoreResourcesEarlyIfTimePermits[] =
"FlushMoreResourcesEarlyIfTimePermits";
const char RewriteOptions::kForbidAllDisabledFilters[] =
"ForbidAllDisabledFilters";
const char RewriteOptions::kGoogleFontCssInlineMaxBytes[] =
"GoogleFontCssInlineMaxBytes";
const char RewriteOptions::kHideRefererUsingMeta[] = "HideRefererUsingMeta";
const char RewriteOptions::kIdleFlushTimeMs[] = "IdleFlushTimeMs";
const char RewriteOptions::kImageInlineMaxBytes[] = "ImageInlineMaxBytes";
@@ -404,6 +406,8 @@ const int64 RewriteOptions::kDefaultCssInlineMaxBytes = 2048;
const int64 RewriteOptions::kDefaultCssFlattenMaxBytes = 1024000;
const int64 RewriteOptions::kDefaultCssImageInlineMaxBytes = 0;
const int64 RewriteOptions::kDefaultCssOutlineMinBytes = 3000;
// 3K is bigger than Roboto loader for Chrome (2.2k)
const int64 RewriteOptions::kDefaultGoogleFontCssInlineMaxBytes = 3 * 1024;
const int64 RewriteOptions::kDefaultImageInlineMaxBytes = 3072;
const int64 RewriteOptions::kDefaultJsInlineMaxBytes = 2048;
const int64 RewriteOptions::kDefaultJsOutlineMinBytes = 3000;
@@ -1196,6 +1200,13 @@ void RewriteOptions::AddProperties() {
kCssFlattenMaxBytes,
kQueryScope,
"Number of bytes below which stylesheets will be flattened.", true);
AddBaseProperty(
kDefaultGoogleFontCssInlineMaxBytes,
&RewriteOptions::google_font_css_inline_max_bytes_, "gfci",
kGoogleFontCssInlineMaxBytes,
kQueryScope,
"Number of bytes below which Google Font stylesheets will be inlined.",
true);
AddBaseProperty(
kDefaultCssImageInlineMaxBytes,
&RewriteOptions::css_image_inline_max_bytes_,
@@ -882,6 +882,7 @@ TEST_F(RewriteOptionsTest, LookupOptionByNameTest) {
RewriteOptions::kFlushHtml,
RewriteOptions::kFlushMoreResourcesEarlyIfTimePermits,
RewriteOptions::kForbidAllDisabledFilters,
RewriteOptions::kGoogleFontCssInlineMaxBytes,
RewriteOptions::kHideRefererUsingMeta,
RewriteOptions::kIdleFlushTimeMs,
RewriteOptions::kImageInlineMaxBytes,

0 comments on commit 01045a9

Please sign in to comment.