How to add a favicon to a website built with the Publish static site generator?

205 views Asked by At

It looks like the Publish static site generator usually picks up static assets from the Resources directory when I run the generator. I've added a favicon.ico file to this directory, but it isn't copied to the default Output directory. There is a Favicon type available with a corresponding favicon property on the Website protocol, but it's unclear how it should be set, and whether that would automatically copy the file to the Output directory.

What's the best way to get the favicon.ico file copied as a resource? Does this need a separate plugin, or how can it be achieved with the Website protocol API?

1

There are 1 answers

3
Debugger On

Place the favicon in the Resources folder. The Website protocol has a favicon property

public protocol Website {
  ...
  /// The website's favicon, if any.
  var favicon: Favicon? { get }
  ...
}

Use that in your site which conforms to Website. You can refer to Favicon type in Publish for more info.

struct MySite: Website {
  ...
  var favicon: Favicon? { .init(path: Path("images/favicon.ico"), type: "image/x-icon")}
  ...
}