Skip to main content

HeadLink

Struct HeadLink 

Source
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 typesWhy they are kept
icon, apple-touch-icon, mask-iconAn image the browser shows for the page, fetched under the policy’s img-src
manifestThe web app manifest; see below
canonical, alternate, author, licenseMetadata: the browser fetches and runs nothing
preconnect, dns-prefetchA 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

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:

CodeCause
HEAD-101rel names a link type that loads style or script
HEAD-102rel names no link type, or one outside the list
HEAD-103href is a javascript: or vbscript: URL, in any case and with any tab, newline or leading control character a browser strips
HEAD-104href 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”

Source

pub fn with_type(self, mime: &str) -> Self

Add a type attribute, such as image/svg+xml.

Source

pub fn with_sizes(self, sizes: &str) -> Self

Add a sizes attribute, such as 180x180.

Trait Implementations§

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more