pub struct HeadLink { /* private fields */ }Expand description
A <link> for the page’s head that is not an asset of the build: an icon,
a canonical URL, a feed. The typed home for what ksx Studio
splices before </head>.
Its rel is one or more of a closed list of link types, none of which
loads style or script:
| Link types | Why they are kept |
|---|---|
icon, apple-touch-icon, mask-icon | An image the browser shows for the page, fetched under the policy’s img-src |
manifest | The web app manifest; see below |
canonical, alternate, author, license | Metadata: the browser fetches and runs nothing |
preconnect, dns-prefetch | A connection hint to an origin the page will use: nothing is fetched or run |
Everything else is refused, with a code: stylesheet, modulepreload,
preload, prefetch, prerender and import load style or script, and
a page’s stylesheets and module preloads come from the build’s manifest
(HEAD-101); any other word, shortcut included (write icon), is not on
the list (HEAD-102). Link types are matched without regard to case and
written in lower case. Every value is escaped for its attribute, and a
script URL is refused, so a head link is never markup and never runs
anything. Add it to a render with
RenderCtx::with_head_link; it is
written after the route’s stylesheets and preloads, in the order added.
manifest needs a policy the default does not give. The browser
fetches a web app manifest under manifest-src, which falls back to the
strict policy’s default-src 'none', so the default policy blocks it.
PolicyExtensions has no manifest source
(it names image and frame sources, a form target and the opener
policy), so a page that links a manifest today sees it refused,
with a policy violation in the console.
use forma_server::{HeadLink, RenderCtx};
let ctx = RenderCtx::new()
.with_head_link(HeadLink::new("icon", "/favicon.svg")?.with_type("image/svg+xml"))
.with_head_link(HeadLink::new("apple-touch-icon", "/apple-touch-icon.png")?);Verified by: crates/forma-server/tests/internal_renderer/typed_homes.rs > “head_links_are_written_escaped_in_the_head_in_the_order_added” Verified by: crates/forma-server/tests/internal_renderer/typed_homes.rs > “a_head_link_carries_only_the_listed_link_types”
Implementations§
Source§impl HeadLink
impl HeadLink
Sourcepub fn new(rel: &str, href: &str) -> Result<HeadLink, HeadLinkError>
pub fn new(rel: &str, href: &str) -> Result<HeadLink, HeadLinkError>
A link of type rel (one or more of HeadLink’s link types,
separated by whitespace) to href.
§Errors
A HeadLinkError with its code:
| Code | Cause |
|---|---|
| HEAD-101 | rel names a link type that loads style or script |
| HEAD-102 | rel names no link type, or one outside the list |
| HEAD-103 | href is a javascript: or vbscript: URL, in any case and with any tab, newline or leading control character a browser strips |
| HEAD-104 | href is empty |
Verified by: crates/forma-server/tests/internal_renderer/typed_homes.rs > “a_head_link_carries_only_the_listed_link_types” Verified by: crates/forma-server/tests/internal_renderer/typed_homes.rs > “a_head_link_refuses_a_script_url_and_keeps_every_other”
Sourcepub fn with_sizes(self, sizes: &str) -> Self
pub fn with_sizes(self, sizes: &str) -> Self
Add a sizes attribute, such as 180x180.