Skip to content

Instantly share code, notes, and snippets.

@kfly8
Last active December 21, 2015 15:39
Show Gist options
  • Select an option

  • Save kfly8/6327648 to your computer and use it in GitHub Desktop.

Select an option

Save kfly8/6327648 to your computer and use it in GitHub Desktop.
dataURIだと、リクエストを二回しているように見えるのだけど、これは何でだろうか・・? http://gyazo.com/14be7c5edfeec270a8303be41fa251fe
<!doctype html>
<head>
<meta charset="utf-8">
</head>
<body>
<img id="hoge" />
<script>
var img = new Image();
var src = "https://1.gravatar.com/avatar/918b601dbd1297d58105508fb880b598?d=https%3A%2F%2Fidenticons.github.com%2Fe553551532c5b2c4074cc6c188f1d49<
//var src = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2NjIpLCBxdWFsaXR5ID0gOTAK/<
var hoge = document.getElementById("hoge");
console.log('before onload');
console.log(window.performance.getEntries());
img.onload = function() {
console.log('img onload');
console.log(window.performance.getEntries());
hoge.src = img.src; // DataUriの時は、ここでもDevTools > Networkに表示されるが、画像パスの時は表示されない
}
hoge.onload = function() {
console.log('hoge onload');
console.log(window.performance.getEntries());
alert('hoge loaded'); // DataUriでも画像パスを指定したときでも、これは呼ばれる
}
img.src = src;
</script>
</body>
</html>
@kfly8

kfly8 commented Aug 24, 2013

Copy link
Copy Markdown
Author

DevToolsのNetworkのInitator列を見ると、どこからリクエストされたかがわかる。
( https://developers.google.com/chrome-developer-tools/docs/network#network_panel_overview )

それを見ると、まず、DataURIでも画像パスでも初回のリクエストが、
https://gist.github.com/kfly8/6327648#file-gistfile1-html-L26
で、されている。

次に、DataURIの場合は、
https://gist.github.com/kfly8/6327648#file-gistfile1-html-L19
でリクエストされているが、画像パスの場合はリクエストされていない

@kfly8

kfly8 commented Aug 24, 2013

Copy link
Copy Markdown
Author

https://gist.github.com/kfly8/6327648#comment-893382
より、画像パスの場合は、二回目のリクエストの場合、
resource timing objectを生成していないのではないかと思われる。(多分、最適化のため?)

だから、二回目のアクセスがDataURIで表示されてないことが不可思議だと思ったけど、
むしろ、二回目以降、画像パス指定の場合は、Networkに表示されないことの方が不可思議

@kfly8

kfly8 commented Aug 24, 2013

Copy link
Copy Markdown
Author

http://www.w3.org/TR/resource-timing/#resources-included

The rest of this section is non-normative.

Examples:

If the same canonical URL is used as the src attribute of two HTML IMG elements, the fetch of the resource initiated by the first HTML IMG element should be included in the PerformanceResourceTiming interface. The user agent might not re-request the URL from the networking layer for the second HTML IMG element, instead using an in-memory browser cache. In this case, there is only a single request sent to the networking layer for retrieval, so the fetch of the resource by the first IMG element would be the only occurrence in the PerformanceResourceTiming interface.

標準の挙動じゃないってところに、ばっちり、
「同じUserAgentで同じパスだったら、2回目のHTML要素では、再びリクエストすることはない。」ってあった。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment