⚡️ Declara un favicon vacío si la página no tiene

Evita que el navegador pida `/favicon.ico` en cada página, una ruta
inexistente que acababa en una página 404 completa.
This commit is contained in:
Manuel Cillero 2026-10-12 12:37:10 +02:00
parent 779e98eeb0
commit 8377ddb4c6
9 changed files with 40 additions and 9 deletions

View file

@ -180,6 +180,11 @@ impl Context {
// **< Context RENDER >*************************************************************************
/// Renderiza los recursos del contexto.
///
/// Si no se ha definido ningún [`Favicon`], declara un icono vacío para evitar que el navegador
/// pida `/favicon.ico` en cada página, una ruta que normalmente no existe y que respondería con
/// una página de error completa. Con el icono vacío no hace ninguna petición y muestra su icono
/// genérico.
pub fn render_assets(&mut self) -> Markup {
use std::mem::take as mem_take;
@ -193,6 +198,8 @@ impl Context {
let markup = html! {
@if let Some(fi) = &favicon {
(fi.render(self))
} @else {
link rel="icon" href="data:,";
}
// Primero los recursos para precarga para iniciar las descargas inmediatamente.
(preloads.render(self))

View file

@ -15,6 +15,10 @@ use crate::{AutoDefault, CowStr};
/// > Los archivos de los iconos deben estar disponibles en el servidor web de la aplicación. Pueden
/// > servirse usando [`serve_static_files!`](crate::serve_static_files).
///
/// Si una página no declara ningún *favicon*, PageTop declara un icono vacío para que el navegador
/// no pida `/favicon.ico` en cada página. Por eso un icono servido en `/favicon.ico` no se muestra
/// si no se declara con este tipo.
///
/// # Ejemplo
///
/// ```rust,no_run

View file

@ -169,7 +169,7 @@ async fn flex_without_display_adds_no_class_of_its_own() {
let html = container.render(&mut cx).await.into_string();
assert!(html.contains(r#"class="own""#));
assert!(cx.render_assets().into_string().is_empty());
assert!(cx.responsive_styles().is_empty());
}
#[pagetop::test]

View file

@ -202,7 +202,7 @@ async fn grid_without_display_adds_no_class_of_its_own() {
let html = container.render(&mut cx).await.into_string();
assert!(html.contains(r#"class="own""#));
assert!(cx.render_assets().into_string().is_empty());
assert!(cx.responsive_styles().is_empty());
}
#[pagetop::test]

View file

@ -0,0 +1,19 @@
use pagetop::prelude::*;
// Empty icon declared when the page has none.
const DEFAULT_ICON: &str = r#"<link rel="icon" href="data:,">"#;
#[pagetop::test]
async fn pages_without_favicon_declare_an_empty_icon() {
// Without any declared icon, browsers would request `/favicon.ico` on every page.
let assets = Context::default().render_assets().into_string();
assert!(assets.contains(DEFAULT_ICON));
}
#[pagetop::test]
async fn a_declared_favicon_replaces_the_default_icon() {
let mut cx = Context::default().with_assets(Favicon::new().with_icon("/app/icon.png"));
let assets = cx.render_assets().into_string();
assert!(assets.contains("/app/icon.png"));
assert!(!assets.contains(DEFAULT_ICON));
}

View file

@ -7,7 +7,7 @@ async fn default_flex_item_adds_nothing() {
let html = html! { span (props.unpack(&mut cx)) {} }.into_string();
assert_eq!(html, "<span></span>");
assert!(cx.render_assets().into_string().is_empty());
assert!(cx.responsive_styles().is_empty());
}
#[pagetop::test]

View file

@ -7,7 +7,7 @@ async fn default_grid_item_adds_nothing() {
let html = html! { span (props.unpack(&mut cx)) {} }.into_string();
assert_eq!(html, "<span></span>");
assert!(cx.render_assets().into_string().is_empty());
assert!(cx.responsive_styles().is_empty());
}
#[pagetop::test]

View file

@ -7,7 +7,7 @@ async fn default_margin_adds_nothing() {
let html = html! { span (props.unpack(&mut cx)) {} }.into_string();
assert_eq!(html, "<span></span>");
assert!(cx.render_assets().into_string().is_empty());
assert!(cx.responsive_styles().is_empty());
}
#[pagetop::test]
@ -17,7 +17,7 @@ async fn default_padding_adds_nothing() {
let html = html! { span (props.unpack(&mut cx)) {} }.into_string();
assert_eq!(html, "<span></span>");
assert!(cx.render_assets().into_string().is_empty());
assert!(cx.responsive_styles().is_empty());
}
#[pagetop::test]
@ -147,7 +147,7 @@ async fn padding_auto_is_ignored() {
let html = html! { span (props.unpack(&mut cx)) {} }.into_string();
assert_eq!(html, "<span></span>");
assert!(cx.render_assets().into_string().is_empty());
assert!(cx.responsive_styles().is_empty());
}
#[pagetop::test]

View file

@ -485,14 +485,15 @@ async fn render_assets_includes_style_tag_with_responsive_styles() {
"flex-basis",
"100%",
));
let baseline = Context::default().render_assets().into_string();
assert_eq!(
cx.render_assets().into_string(),
"<style>.col{flex-basis:100%}</style>"
format!("{baseline}<style>.col{{flex-basis:100%}}</style>")
);
}
#[pagetop::test]
async fn render_assets_omits_style_tag_when_no_responsive_styles() {
let mut cx = Context::default();
assert_eq!(cx.render_assets().into_string(), "");
assert!(!cx.render_assets().into_string().contains("<style"));
}