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

Commit 61f4e96

Browse files
committed
Don't include rel=canonical headers on IPRO resources.
Fixes #1222
1 parent 55e0962 commit 61f4e96

File tree

6 files changed

+46
-3
lines changed

6 files changed

+46
-3
lines changed

net/instaweb/rewriter/in_place_rewrite_context.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,12 @@ void InPlaceRewriteContext::FixFetchFallbackHeaders(
433433
headers->SetDateAndCaching(now_ms, expire_at_ms - now_ms);
434434
AddVaryIfRequired(cached_result, headers);
435435
}
436+
RemoveRedundantRelCanonicalHeader(cached_result, headers);
437+
}
438+
439+
void InPlaceRewriteContext::RemoveRedundantRelCanonicalHeader(
440+
const CachedResult& cached_result, ResponseHeaders* headers) {
441+
headers->Remove(HttpAttributes::kLink, RelCanonicalHeaderValue(url_));
436442
}
437443

438444
void InPlaceRewriteContext::UpdateDateAndExpiry(

net/instaweb/rewriter/public/in_place_rewrite_context.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ class InPlaceRewriteContext : public SingleRewriteContext {
140140
// if the fetch result may be browser dependent.
141141
void AddVaryIfRequired(const CachedResult& cached_result,
142142
ResponseHeaders* headers) const;
143+
// Image rewriting adds a Link rel=canonical header. Because a single cached
144+
// result can be served from multiple urls we do need to keep generating it.
145+
// But when serving via IPRO we should remove it if the url hasn't changed.
146+
void RemoveRedundantRelCanonicalHeader(const CachedResult& cached_result,
147+
ResponseHeaders* headers);
143148

144149
GoogleString url_;
145150
// Boolean indicating whether or not the resource was rewritten successfully.

net/instaweb/rewriter/public/single_rewrite_context.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ class SingleRewriteContext : public RewriteContext {
5757
void AddLinkRelCanonical(const ResourcePtr& input,
5858
const OutputResourcePtr& output);
5959

60+
// Constructs a <url>; rel="canonical" value for use with a Link header.
61+
GoogleString RelCanonicalHeaderValue(StringPiece url);
62+
6063
private:
6164
DISALLOW_COPY_AND_ASSIGN(SingleRewriteContext);
6265
};

net/instaweb/rewriter/single_rewrite_context.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,11 @@ void SingleRewriteContext::AddLinkRelCanonical(
102102
}
103103

104104
output->response_headers()->Add(
105-
HttpAttributes::kLink,
106-
StrCat("<", GoogleUrl::Sanitize(input->url()), ">; ",
107-
"rel=\"canonical\""));
105+
HttpAttributes::kLink, RelCanonicalHeaderValue(input->url()));
106+
}
107+
108+
GoogleString SingleRewriteContext::RelCanonicalHeaderValue(StringPiece url) {
109+
return StrCat("<", GoogleUrl::Sanitize(url), ">; rel=\"canonical\"");
108110
}
109111

110112
} // namespace net_instaweb

pagespeed/automatic/system_test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ fi
8989

9090
run_test content_length
9191
run_test keep_data_urls
92+
run_test rel_canonical
9293

9394
wait_for_async_tests
9495

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
start_test rel-canonical
2+
3+
# .pagespeed. resources should have Link rel=canonical headers, IPRO resources
4+
# should not have them.
5+
6+
start_test link rel=canonical header not present with IPRO resources
7+
8+
REL_CANONICAL_REGEXP='Link:.*rel.*canonical'
9+
10+
URL=$EXAMPLE_ROOT/images/Puzzle.jpg
11+
# Fetch it a few times until IPRO is done and has given it an ipro ("aj") etag.
12+
fetch_until -save "$URL" 'grep -c E[Tt]ag:.W/.PSA-aj.' 1 --save-headers
13+
# rel=canonical should not be present.
14+
check [ $(grep -c "$REL_CANONICAL_REGEXP" $FETCH_FILE) = 0 ]
15+
16+
start_test link rel=canonical header present with pagespeed.ce resources
17+
18+
URL=$REWRITTEN_ROOT/images/Puzzle.jpg.pagespeed.ce.HASH.jpg
19+
OUT=$($CURL -D- -o/dev/null -sS $URL)
20+
check_from "$OUT" grep "$REL_CANONICAL_REGEXP"
21+
22+
start_test link rel=canonical header present with pagespeed.ic resources
23+
24+
URL=$REWRITTEN_ROOT/images/xPuzzle.jpg.pagespeed.ic.HASH.jpg
25+
OUT=$($CURL -D- -o/dev/null -sS $URL)
26+
check_from "$OUT" grep "$REL_CANONICAL_REGEXP"

0 commit comments

Comments
 (0)