Documentation ¶
Overview ¶
Package htmltask implements some optimization logic for HTML documents.
The logic is implemented on a common interface named HTMLTask. Each HTMLTask implementation has a single clear focus, such as "add the preload links for stylesheets used in the HTML document." HTMLTasks are passed collectively to htmlproc.NewHTMLProcessor to define its processing logic.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var AggressiveTaskSet = []HTMLTask{ ExtractSubContentTypes(), ExtractPreloadTags(), PreloadStylesheets(), InsecurePreloadScripts(), }
AggressiveTaskSet gets as many resources preloaded as Web Packager can. It includes HTMLTasks that might make negative effect in some cases.
var ConservativeTaskSet = []HTMLTask{ ExtractSubContentTypes(), ExtractPreloadTags(), }
ConservativeTaskSet is the set of HTMLTasks used in the default config. It consists only of HTMLTasks that almost always work well.
Functions ¶
This section is empty.
Types ¶
type HTMLTask ¶
type HTMLTask interface {
Run(resp *htmldoc.HTMLResponse) error
}
HTMLTask manipulates HTMLResponse to optimize the page loading.
func ExtractPreloadTags ¶
func ExtractPreloadTags() HTMLTask
ExtractPreloadTags detects <link rel="preload"> in the <head> element and adds them to the Preloads field.
func ExtractSubContentTypes ¶
func ExtractSubContentTypes() HTMLTask
ExtractSubContentTypes detects internal subcontents in HTML docuemnt, such as CSS (<style>) and JavaScript (<script> without src attribute), and adds their MIME types (e.g. "text/style", "application/javascript") to ExtraData, using exchange.SubContentType as the key.
func InsecurePreloadScripts ¶
func InsecurePreloadScripts() HTMLTask
InsecurePreloadScripts detects scripts loaded at the top of the document in a blocking manner and adds them to the Preloads field. More precisely, InsecurePreloadScripts scans over the parsed tree up to the first node that renders non-trivial content, such as images and non-whitespace text, as the top of the document. Scripts are considered as blocking when loaded without async or defer attribute.
SECURITY NOTICE: InsecurePreloadScripts should be used with special care. Web Packager produces signed exchanges for all resources to be preloaded. Remember that signed exchanges will remain used and distributed until they expire, once they are made public: it is possible that they continue to be distributed on caches you do not know about, even if they turn out to have security issues in JavaScript code. To migitate the risk, it is advised to consider making signed exchanges expire shortly (say, within 24 hours), or even better not preloading scripts. For example, you can load your scripts with defer attribute (e.g. <script defer src="foo.js">) to allow browsers to keep rendering the web page without waiting for scripts getting loaded and executed, thus eliminate the need for preloading.
func PreloadStylesheets ¶
func PreloadStylesheets() HTMLTask
PreloadStylesheets detects <link rel="stylesheet"> in the <head> element and adds referenced stylesheets to the Preloads field.
PreloadStylesheets does not include stylesheets that have "alternate" in the rel attribute. Those stylesheets are unused in the initial rendering. They are not used at all on some unsupported browsers.