resourcehints

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 19, 2026 License: MIT Imports: 6 Imported by: 0

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:

  1. Detection - Scan HTML/CSS for external domains
  2. 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

func GenerateComment(content string) string

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 NewDetector

func NewDetector() *Detector

NewDetector creates a new Detector.

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

func (d *Detector) SetExcludeDomains(domains []string)

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 NewGenerator

func NewGenerator() *Generator

NewGenerator creates a new Generator.

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

type KnownDomainHint struct {
	HintTypes   []HintType
	CrossOrigin string
	As          string
}

KnownDomainHint contains pre-configured hints for common services.

type SuggestedHint

type SuggestedHint struct {
	Domain      string
	Scheme      string
	HintTypes   []HintType
	CrossOrigin string
	As          string
}

SuggestedHint represents a suggested resource hint for a domain.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL