Documentation
¶
Overview ¶
Package resourcehints provides detection and generation of resource hints (preconnect, dns-prefetch, preload, prefetch) for web performance optimization.
Resource Hints Overview ¶
Resource hints help browsers prepare for external resources before they're needed:
- preconnect: Establishes early connection to origin (DNS + TCP + TLS)
- dns-prefetch: Performs DNS lookup in advance
- preload: Fetches critical resources early
- prefetch: Fetches resources for future navigation
Usage ¶
The package provides two main capabilities:
- Detection - Scan HTML/CSS for external domains
- Generation - Create link tags for detected or configured hints
Example:
detector := resourcehints.NewDetector() domains := detector.DetectExternalDomains(htmlContent) hints := detector.SuggestHints(domains) generator := resourcehints.NewGenerator() tags := generator.GenerateHintTags(hints)
Known Domains ¶
The package includes built-in knowledge of common CDNs and services:
- Google Fonts (fonts.googleapis.com, fonts.gstatic.com)
- CDNs (cdn.jsdelivr.net, unpkg.com, cdnjs.cloudflare.com)
- Analytics (www.google-analytics.com, www.googletagmanager.com)
Performance Impact ¶
Typical improvements from resource hints:
- 100-500ms reduction in resource loading time
- 3-8 points improvement in Lighthouse performance score
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateComment ¶
GenerateComment generates an HTML comment to wrap resource hints.
func GetKnownDomains ¶
func GetKnownDomains() map[string]KnownDomainHint
GetKnownDomains returns the list of domains with predefined hints.
Types ¶
type DetectedDomain ¶
type DetectedDomain struct {
// Domain is the hostname (e.g., "fonts.googleapis.com")
Domain string
// Scheme is the URL scheme (e.g., "https")
Scheme string
// SourceType indicates where the domain was found
SourceType string // "html", "css", "script", "font", etc.
}
DetectedDomain represents an external domain found in content.
type Detector ¶
type Detector struct {
// contains filtered or unexported fields
}
Detector detects external domains in HTML and CSS content.
func (*Detector) DetectExternalDomains ¶
func (d *Detector) DetectExternalDomains(htmlContent string) []DetectedDomain
DetectExternalDomains scans HTML content and returns a list of detected external domains.
func (*Detector) SetExcludeDomains ¶
SetExcludeDomains sets the list of domains to exclude from detection.
func (*Detector) SuggestHints ¶
func (d *Detector) SuggestHints(domains []DetectedDomain) []SuggestedHint
SuggestHints suggests appropriate hints for detected domains. Uses known domain database for optimal hint types.
type Generator ¶
type Generator struct{}
Generator generates HTML resource hint tags.
func (*Generator) GenerateFromConfig ¶
func (g *Generator) GenerateFromConfig(config *models.ResourceHintsConfig, detectedDomains []DetectedDomain) string
GenerateFromConfig generates hint tags from a ResourceHintsConfig. Combines manually configured domains with auto-detected ones.
func (*Generator) GenerateHintTags ¶
func (g *Generator) GenerateHintTags(hints []SuggestedHint) string
GenerateHintTags generates HTML link tags for the given hints. Returns a string of newline-separated link tags.
type HintType ¶
type HintType string
HintType represents a resource hint type.
const ( // HintTypePreconnect establishes early connections to origins. HintTypePreconnect HintType = "preconnect" // HintTypeDNSPrefetch performs DNS lookup in advance. HintTypeDNSPrefetch HintType = "dns-prefetch" // HintTypePreload fetches critical resources early. HintTypePreload HintType = "preload" // HintTypePrefetch fetches resources for future navigation. HintTypePrefetch HintType = "prefetch" )
type KnownDomainHint ¶
KnownDomainHint contains pre-configured hints for common services.