Download this file
| 1 | package bakeoff |
| 2 | |
| 3 | import ( |
| 4 | "net/url" |
| 5 | "regexp" |
| 6 | ) |
| 7 | |
| 8 | // Weight is what a first visit to a page downloads, apart from the shared |
| 9 | // CSS every app serves identically. |
| 10 | type Weight struct{ HTMLBytes, JSBytes int } |
| 11 | |
| 12 | var scriptSrc = regexp.MustCompile(`<script\b[^>]*\bsrc="([^"]+)"`) |
| 13 | |
| 14 | // PageWeight sums the page's HTML and every same-origin script it loads. |
| 15 | func PageWeight(s *Session, path string) (Weight, error) { |
| 16 | html, err := s.get(path) |
| 17 | if err != nil { |
| 18 | return Weight{}, err |
| 19 | } |
| 20 | w := Weight{HTMLBytes: len(html)} |
| 21 | for _, m := range scriptSrc.FindAllStringSubmatch(html, -1) { |
| 22 | u, err := s.Base.Parse(m[1]) |
| 23 | if err != nil || u.Host != s.Base.Host { |
| 24 | continue |
| 25 | } |
| 26 | js, err := s.get((&url.URL{Path: u.Path, RawQuery: u.RawQuery}).String()) |
| 27 | if err != nil { |
| 28 | return Weight{}, err |
| 29 | } |
| 30 | w.JSBytes += len(js) |
| 31 | } |
| 32 | return w, nil |
| 33 | } |
| 34 | |