Commit dca4ebf6 authored by Mickaël Desfrênes's avatar Mickaël Desfrênes
Browse files

fix font imports

parent 59714661
Loading
Loading
Loading
Loading
+19
Changes for freeze.go: 19 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -168,6 +168,12 @@ func freezeSite(startURL, output string) error {
					_ = r.Request.Visit(u)
				}
			}
			for _, ref := range cssURLRefs(r.Body) {
				u := r.Request.AbsoluteURL(ref)
				if u != "" {
					_ = r.Request.Visit(u)
				}
			}
		}
		if isJSContentType(r.Headers.Get("Content-Type"), r.Request.URL.Path) {
			for _, ref := range jsImportRefs(r.Body) {
@@ -398,6 +404,19 @@ func cssImportRefs(body []byte) []string {
	return refs
}

// CSS url(...) values can be fonts, images, or other assets. Rewriting alone is
// not enough: those files must be visited so they are present in the frozen tree.
func cssURLRefs(body []byte) []string {
	matches := cssURLRegexp.FindAllSubmatch(body, -1)
	refs := make([]string, 0, len(matches))
	for _, match := range matches {
		if len(match) >= 2 {
			refs = append(refs, string(match[1]))
		}
	}
	return refs
}

// JavaScript module specifiers are fetch dependencies when they are local URLs;
// bare package names are left alone because they cannot be mirrored from MaX.
func jsImportRefs(body []byte) []string {
+1 −1
Changes for main.go: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@ import (
//go:embed max_releases.json
var fallbackReleases []byte

const version = "0.4.7"
const version = "0.4.8"

const maxConfigFile = "config.xml"
const minSupportedMaxVersion = "0.0.5"
+52
Changes for main_test.go: 52 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -114,6 +114,23 @@ body{background:url("./not-an-import.png")}`)
	}
}

func TestCSSURLRefs(t *testing.T) {
	input := []byte(`@font-face {
  src: url("./fonts/text.woff2") format("woff2"),
       url('/fonts/text.woff') format("woff");
}
body{background:url(../images/bg.png)}`)
	got := cssURLRefs(input)
	want := []string{
		"./fonts/text.woff2",
		"/fonts/text.woff",
		"../images/bg.png",
	}
	if strings.Join(got, ",") != strings.Join(want, ",") {
		t.Fatalf("unexpected css url refs: want %v, got %v", want, got)
	}
}

func TestFreezeSiteCrawlsCSSImports(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		switch r.URL.Path {
@@ -146,6 +163,41 @@ func TestFreezeSiteCrawlsCSSImports(t *testing.T) {
	}
}

func TestFreezeSiteCrawlsCSSFontURLs(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		switch r.URL.Path {
		case "/":
			w.Header().Set("Content-Type", "text/html")
			_, _ = w.Write([]byte(`<link rel="stylesheet" href="/css/main.css">`))
		case "/css/main.css":
			w.Header().Set("Content-Type", "text/css")
			_, _ = w.Write([]byte(`@import "./fonts.css";`))
		case "/css/fonts.css":
			w.Header().Set("Content-Type", "text/css")
			_, _ = w.Write([]byte(`@font-face{font-family:test;src:url("./fonts/test.woff2") format("woff2")}`))
		case "/css/fonts/test.woff2":
			w.Header().Set("Content-Type", "font/woff2")
			_, _ = w.Write([]byte("fake-font"))
		default:
			http.NotFound(w, r)
		}
	}))
	defer server.Close()

	output := t.TempDir()
	if err := freezeSite(server.URL+"/", output); err != nil {
		t.Fatal(err)
	}
	for _, path := range []string{
		filepath.Join(output, "css", "fonts.css"),
		filepath.Join(output, "css", "fonts", "test.woff2"),
	} {
		if !fileExists(path) {
			t.Fatalf("expected frozen css dependency to exist: %s", path)
		}
	}
}

func TestRewriteJSImportsToRelative(t *testing.T) {
	currentURL, err := url.Parse("http://localhost:8080/static/js/app.js")
	if err != nil {