MCPcopy
hub / github.com/esm-dev/esm.sh / preload

Method preload

web/handler.go:985–1044  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

983}
984
985func (s *Handler) preload() {
986 indexHtmlFilename := filepath.Join(s.config.AppDir, "index.html")
987 indexHtmlFile, err := os.Open(indexHtmlFilename)
988 if err != nil {
989 return
990 }
991 defer indexHtmlFile.Close()
992 entries := map[string]struct{}{}
993 tokenizer := html.NewTokenizer(indexHtmlFile)
994 for {
995 tt := tokenizer.Next()
996 if tt == html.ErrorToken {
997 break
998 }
999 if tt == html.StartTagToken {
1000 tagName, moreAttr := tokenizer.TagName()
1001 if string(tagName) == "script" {
1002 var (
1003 srcAttr string
1004 hrefAttr string
1005 )
1006 for moreAttr {
1007 var key, val []byte
1008 key, val, moreAttr = tokenizer.TagAttr()
1009 if bytes.Equal(key, []byte("src")) {
1010 srcAttr = string(val)
1011 } else if bytes.Equal(key, []byte("href")) {
1012 hrefAttr = string(val)
1013 }
1014 }
1015 if hrefAttr != "" && isHttpSepcifier(srcAttr) {
1016 if !isHttpSepcifier(hrefAttr) && (hrefAttr == "uno.css" || strings.HasSuffix(hrefAttr, "/uno.css") || isModulePath(hrefAttr)) {
1017 entries[hrefAttr] = struct{}{}
1018 }
1019 } else if !isHttpSepcifier(srcAttr) && isModulePath(srcAttr) {
1020 entries[srcAttr] = struct{}{}
1021 }
1022 }
1023 }
1024 }
1025 if len(entries) > 0 {
1026 rootUrl := url.URL{Path: "/"}
1027 w := &dummyResponseWriter{}
1028 q := url.Values{}
1029 for entry := range entries {
1030 pathname := rootUrl.ResolveReference(&url.URL{Path: entry}).Path
1031 r := &http.Request{
1032 Method: "GET",
1033 URL: &url.URL{
1034 Path: pathname,
1035 },
1036 }
1037 if strings.HasSuffix(entry, "tailwind.css") {
1038 s.ServeFrameworkCSS(w, r, q, "tailwind")
1039 } else {
1040 s.ServeModule(w, r, pathname, q, nil)
1041 }
1042 }

Callers 1

startLoaderWorkerMethod · 0.95

Calls 6

ServeFrameworkCSSMethod · 0.95
ServeModuleMethod · 0.95
isHttpSepcifierFunction · 0.85
isModulePathFunction · 0.85
NextMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected