Skip to content

Commit

Permalink
Lazily load iframes (ordinals#1456)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey committed Feb 6, 2023
1 parent f2d8d0d commit 933a005
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/templates/iframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,20 @@ impl Iframe {
impl Display for Iframe {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
if self.thumbnail {
write!(
f,
"<a href=/inscription/{}><iframe sandbox=allow-scripts scrolling=no src=/preview/{}></iframe></a>",
self.inscription_id,
self.inscription_id,
)
} else {
write!(
f,
"<iframe sandbox=allow-scripts scrolling=no src=/preview/{}></iframe>",
self.inscription_id,
)
write!(f, "<a href=/inscription/{}>", self.inscription_id)?;
}

write!(
f,
"<iframe sandbox=allow-scripts scrolling=no loading=lazy src=/preview/{}></iframe>",
self.inscription_id
)?;

if self.thumbnail {
write!(f, "</a>",)?
}

Ok(())
}
}

Expand All @@ -49,15 +50,15 @@ mod tests {
assert_regex_match!(
Iframe::thumbnail(inscription_id(1))
.0.to_string(),
"<a href=/inscription/1{64}i1><iframe sandbox=allow-scripts scrolling=no src=/preview/1{64}i1></iframe></a>",
"<a href=/inscription/1{64}i1><iframe sandbox=allow-scripts scrolling=no loading=lazy src=/preview/1{64}i1></iframe></a>",
);
}

#[test]
fn main() {
assert_regex_match!(
Iframe::main(inscription_id(1)).0.to_string(),
"<iframe sandbox=allow-scripts scrolling=no src=/preview/1{64}i1></iframe>",
"<iframe sandbox=allow-scripts scrolling=no loading=lazy src=/preview/1{64}i1></iframe>",
);
}
}

0 comments on commit 933a005

Please sign in to comment.