1

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?

Max Desiatov
  • 5,087
  • 3
  • 48
  • 56

1 Answers1

2

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")}
  ...
}
Debugger
  • 71
  • 2
  • 7
  • How do I add icons in multiple formats to support both `.ico` and `.svg` as described here? https://css-tricks.com/svg-favicons-and-all-the-fun-things-we-can-do-with-them/ – Max Desiatov Dec 11 '20 at 18:40
  • @MaxDesiatov If I understand correctly, you are asking for two favicon `link` tags. The first one referring to a `.svg`, and the second one referring to `.ico` which will act as a fallback. This requires implementing your own head method. How I would do this is, in your custom theme, add extension to Node like `private extension Node where Context == HTML.DocumentContext` Under which copy the implementation of the existing head. In it's return statement, just before where it unwraps the default favicon, add a `.element` named `link` and the appropriate attributes to refer the favicon – Debugger Dec 12 '20 at 06:48
  • @MaxDesiatov Now, when you call `.head` from any function in your HTMLFactory should call head from your custom Node extension instead of calling head from PlotComponents. – Debugger Dec 12 '20 at 06:51