Skip to content
This repository was archived by the owner on Apr 10, 2025. It is now read-only.

Commit 9d6d087

Browse files
committed
Stop appending ?PageSpeed=off to .pagespeed. resources in the sources list for source_maps.
We must append ?PageSpeed=off to non-pagespeed URLs because otherwise IPRO might rewrite the source and lead to a non-sensical source map. However, IPRO does not modify .pages peed. URLs, so we can safely avoid this query param. Furthermore, the param does not make sense for .pagespeed. resources and seems to 404. Fixes issue 1043. backported from Stop appending ?PageSpeed=off to .pagespeed. resources in the sources list for source_maps. We must append ?PageSpeed=off to non-pagespeed URLs because otherwise IPRO might rewrite the source and lead to a non-sensical source map. However, IPRO does not modify .pages peed. URLs, so we can safely avoid this query param. Furthermore, the param does not make sense for .pagespeed. resources and seems to 404. Fixes issue 1043. backported from 701c6c0
1 parent 0f2a6fc commit 9d6d087

2 files changed

Lines changed: 64 additions & 5 deletions

File tree

‎net/instaweb/rewriter/javascript_filter.cc‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,18 @@ class JavascriptFilter::Context : public SingleRewriteContext {
168168
if (Options()->Enabled(RewriteOptions::kIncludeJsSourceMaps) &&
169169
// Source map will be empty if we can't construct it correctly.
170170
!code_block.SourceMappings().empty()) {
171-
// Note: We append PageSpeed=off query parameter to make sure that
172-
// the source URL doesn't get rewritten with IPRO.
173171
GoogleUrl original_gurl(input->url());
174-
scoped_ptr<GoogleUrl> source_gurl(
175-
original_gurl.CopyAndAddEscapedQueryParam(RewriteQuery::kPageSpeed,
176-
"off"));
172+
scoped_ptr<GoogleUrl> source_gurl;
173+
if (server_context->IsPagespeedResource(original_gurl)) {
174+
// Do not append Pagespeed=off if input is already a pagespeed resource.
175+
source_gurl.reset(new GoogleUrl);
176+
source_gurl->Reset(original_gurl);
177+
} else {
178+
// Note: We append PageSpeed=off query parameter to make sure that
179+
// the source URL doesn't get rewritten with IPRO.
180+
source_gurl.reset(original_gurl.CopyAndAddEscapedQueryParam(
181+
RewriteQuery::kPageSpeed, "off"));
182+
}
177183

178184
GoogleString source_map_text;
179185
// Note: We omit rewritten URL because of a chicken-and-egg problem.

‎net/instaweb/rewriter/javascript_filter_test.cc‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "net/instaweb/rewriter/public/debug_filter.h"
3333
#include "net/instaweb/rewriter/public/javascript_code_block.h"
3434
#include "net/instaweb/rewriter/public/javascript_library_identification.h"
35+
#include "net/instaweb/rewriter/public/js_outline_filter.h"
3536
#include "net/instaweb/rewriter/public/rewrite_driver.h"
3637
#include "net/instaweb/rewriter/public/rewrite_options.h"
3738
#include "net/instaweb/rewriter/public/rewrite_test_base.h"
@@ -1241,6 +1242,58 @@ TEST_P(JavascriptFilterTest, SourceMapUnsanitaryUrl) {
12411242
EXPECT_EQ(expected_output_js, output_js);
12421243
}
12431244

1245+
// For non-pagespeed input JS, we must add ?PageSpeed=off to the source URL
1246+
// to avoid IPRO rewriting the source. However, we should not add ?PageSpeed=off
1247+
// for .pagespeed. input files, because that doesn't make any sense.
1248+
// https://code.google.com/p/modpagespeed/issues/detail?id=1043
1249+
TEST_P(JavascriptFilterTest, ProperSourceMapForPagespeedInput) {
1250+
if (!options()->use_experimental_js_minifier()) return;
1251+
1252+
options()->EnableFilter(RewriteOptions::kIncludeJsSourceMaps);
1253+
options()->EnableFilter(RewriteOptions::kRewriteJavascriptExternal);
1254+
options()->EnableFilter(RewriteOptions::kOutlineJavascript);
1255+
options()->set_js_outline_min_bytes(0); // Make sure everything is outlined.
1256+
UseMd5Hasher(); // We should use a real hasher for outlining.
1257+
rewrite_driver_->AddFilters();
1258+
1259+
const char input_js[] = "foo = 1;";
1260+
GoogleString outlined_js_tail =
1261+
Encode("", JsOutlineFilter::kFilterId,
1262+
hasher()->Hash(input_js), "_", "js");
1263+
1264+
const char expected_mapping_vlq[] = "AAAA,GAAI,CAAE";
1265+
// Note: No ?PageSpeed=off
1266+
GoogleString expected_source_map = StrCat(
1267+
")]}'\n{\"mappings\":\"", expected_mapping_vlq, "\",\"names\":[],"
1268+
"\"sources\":[\"", kTestDomain, outlined_js_tail, "\"],"
1269+
"\"version\":3}\n");
1270+
GoogleString source_map_url =
1271+
Encode(kTestDomain, RewriteOptions::kJavascriptMinSourceMapId,
1272+
hasher()->Hash(expected_source_map), outlined_js_tail, "map");
1273+
1274+
const char rewritten_js[] = "foo=1;";
1275+
GoogleString expected_output_js =
1276+
StrCat(rewritten_js, "\n"
1277+
"//# sourceMappingURL=", source_map_url, "\n");
1278+
GoogleString rewritten_js_url =
1279+
Encode(kTestDomain, RewriteOptions::kJavascriptMinId,
1280+
hasher()->Hash(expected_output_js), outlined_js_tail, "js");
1281+
1282+
GoogleString input_html = StrCat("<script>", input_js, "</script>");
1283+
GoogleString expected_output_html =
1284+
StrCat("<script src=\"", rewritten_js_url, "\"></script>");
1285+
ValidateExpected("source_map_pagespeed", input_html, expected_output_html);
1286+
1287+
GoogleString output_js;
1288+
EXPECT_TRUE(FetchResourceUrl(rewritten_js_url, &output_js));
1289+
EXPECT_EQ(expected_output_js, output_js);
1290+
1291+
GoogleString source_map;
1292+
EXPECT_TRUE(FetchResourceUrl(source_map_url, &source_map));
1293+
EXPECT_EQ(expected_source_map, source_map);
1294+
}
1295+
1296+
12441297
TEST_P(JavascriptFilterTest, InlineAndNotExternal) {
12451298
options()->EnableFilter(RewriteOptions::kRewriteJavascriptInline);
12461299
options()->DisableFilter(RewriteOptions::kRewriteJavascriptExternal);

0 commit comments

Comments
 (0)