Adding a Favicon

Add a site icon (favicon.ico / PNG) so browsers display your brand in tabs and bookmarks. Kestrun provides Add-KrFavicon to register a favicon quickly without manually mapping a route.

Prerequisites: see Introduction.

Full source

<#
    Sample Kestrun Server on how to add a favicon.
    This example demonstrates how to configure a favicon in a Kestrun server.
    FileName: 3.4-Add-FavIcon.ps1
#>

# Import the Kestrun module
Install-PSResource -Name Kestrun

# Initialize Kestrun root directory
# the default value is $PWD
# This is recommended in order to use relative paths without issues
Initialize-KrRoot -Path $PSScriptRoot

# Create a new Kestrun server
New-KrServer -Name "Simple Server"

# Add a listener on port 5000 and IP address 127.0.0.1 (localhost)
Add-KrListener -Port 5000 -IPAddress ([IPAddress]::Loopback)

# Add a file server with browsing enabled
Add-KrFileServer -RequestPath '/' -RootPath '.\Assets\wwwroot' -EnableDirectoryBrowsing -ServeUnknownFileTypes

# Add a favicon
Add-KrFavicon -IconPath '.\Assets\favicon.png'

# Enable Kestrun configuration
Enable-KrConfiguration

# Start the server asynchronously
Start-KrServer

Check response headers for a valid Content-Type like image/x-icon or image/png.

Integrating with HTML

Add a <link> tag in your index.html (if not relying on the implicit /favicon.ico request):

<link rel="icon" type="image/png" href="/favicon.ico" />

Or explicitly reference the PNG if you prefer that path:

<link rel="icon" type="image/png" href="/Assets/favicon.png" />

Troubleshooting

Symptom Cause Fix
404 for favicon.ico Wrong -IconPath or file missing Ensure path is correct relative to root; confirm file exists.
Browser still shows old icon Browser cache Hard refresh, open dev tools, or change icon filename.
Incorrect MIME type Unusual extension Use .ico or .png for best compatibility.
Icon looks blurry Low resolution Provide at least 32x32 (or multi-size ICO).

Cmdlet references


Next

Previous: Static Route Overrides
Next: Variable Routes
Review earlier: Serving Static Files | File Server