Skip to content

Commit 921dfee

Browse files
authored
Merge pull request #61 from linkdotnet/feature/fallback-image
Feature/fallback image
2 parents d9f1cef + 645685b commit 921dfee

File tree

17 files changed

+217
-32
lines changed

17 files changed

+217
-32
lines changed

Readme.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,13 @@ To get better results when for example shared via LinkedIn some of the `<meta pr
167167

168168
The following tags are set depending on the page:
169169

170-
| Open Graph Tag | Index | Display Blog Post |
171-
| -------------- | --------------------------------------------------------- | -------------------------------------------- |
172-
| og:title | Title of the blog (defined in Introduction) | Title of the Blog Post |
173-
| og:url | Url to the index page | Url of the page |
174-
| og:image | Profile image (defined in Introduction) | Yes |
175-
| og:type | article | article |
176-
| og:description | Short description in plain text (defined in Introduction) | Short Description of Blog Post in plain text |
170+
| Open Graph Tag | Index | Display Blog Post |
171+
| -------------- | --------------------------------------------------------- |-----------------------------------------------------------------------------|
172+
| og:title | Title of the blog (defined in Introduction) | Title of the Blog Post |
173+
| og:url | Url to the index page | Url of the page |
174+
| og:image | Profile image (defined in Introduction) | Uses the preview image. If a fallback is defined this will be used instead. |
175+
| og:type | article | article |
176+
| og:description | Short description in plain text (defined in Introduction) | Short Description of Blog Post in plain text |
177177

178178
Furthermore the following tags are set:
179179

@@ -183,4 +183,4 @@ Furthermore the following tags are set:
183183
| &lt;meta name="keyword" content="" /&gt; | not set | Tags defined in the Blog Post |
184184

185185
## RSS Feed
186-
This blog also offers a RSS feed ([RSS 2.0 specification](https://validator.w3.org/feed/docs/rss2.html)), which can be consumed by your users or programs like feedly. Just append `feed.rss` to your url or click on the RSS feed icon in the navigation bar to get the feed. The RSS feed does not expose the whole content of a given blog post but it's title and short description including some other tags like preview image, publishing date and so on.
186+
This blog also offers a RSS feed ([RSS 2.0 specification](https://validator.w3.org/feed/docs/rss2.html)), which can be consumed by your users or programs like feedly. Just append `feed.rss` to your url or click on the RSS feed icon in the navigation bar to get the feed. The RSS feed does not expose the whole content of a given blog post but it's title and short description including some other tags like preview image, publishing date and so on.

src/LinkDotNet.Blog.Domain/BlogPost.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ private BlogPost()
1818

1919
public string PreviewImageUrl { get; private set; }
2020

21+
public string PreviewImageUrlFallback { get; private set; }
22+
2123
public DateTime UpdatedDate { get; private set; }
2224

2325
public virtual ICollection<Tag> Tags { get; private set; }
@@ -33,7 +35,8 @@ public static BlogPost Create(
3335
string previewImageUrl,
3436
bool isPublished,
3537
DateTime? updatedDate = null,
36-
IEnumerable<string> tags = null)
38+
IEnumerable<string> tags = null,
39+
string previewImageUrlFallback = null)
3740
{
3841
var blogPost = new BlogPost
3942
{
@@ -42,6 +45,7 @@ public static BlogPost Create(
4245
Content = content,
4346
UpdatedDate = updatedDate ?? DateTime.Now,
4447
PreviewImageUrl = previewImageUrl,
48+
PreviewImageUrlFallback = previewImageUrlFallback,
4549
IsPublished = isPublished,
4650
Tags = tags?.Select(Tag.Create).ToList(),
4751
};
@@ -61,6 +65,7 @@ public void Update(BlogPost from)
6165
Content = from.Content;
6266
UpdatedDate = from.UpdatedDate;
6367
PreviewImageUrl = from.PreviewImageUrl;
68+
PreviewImageUrlFallback = from.PreviewImageUrlFallback;
6469
IsPublished = from.IsPublished;
6570
ReplaceTags(from.Tags);
6671
}

src/LinkDotNet.Blog.Infrastructure/Persistence/Sql/Mapping/BlogPostConfiguration.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public void Configure(EntityTypeBuilder<BlogPost> builder)
1717
builder.Navigation(x => x.Tags).AutoInclude();
1818
builder.Property(x => x.Title).HasMaxLength(256).IsRequired();
1919
builder.Property(x => x.PreviewImageUrl).HasMaxLength(1024).IsRequired();
20+
builder.Property(x => x.PreviewImageUrlFallback).HasMaxLength(1024);
2021
builder.Property(x => x.Content).IsRequired();
2122
builder.Property(x => x.ShortDescription).IsRequired();
2223
builder.Property(x => x.Likes).IsRequired();

src/LinkDotNet.Blog.Web/Features/Admin/BlogPostEditor/Components/CreateNewBlogPost.razor

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
3030
<div class="mb-3">
3131
<label for="preview">Preview-Url</label>
3232
<InputText class="form-control" id="preview" @bind-Value="model.PreviewImageUrl"/>
33+
<small for="preview" class="form-text text-muted">The primary image which will be used.</small>
34+
</div>
35+
<div class="mb-3">
36+
<label for="preview">Fallback Preview-Url</label>
37+
<InputText class="form-control" id="fallback-preview" @bind-Value="model.PreviewImageUrlFallback"/>
38+
<small for="fallback-preview" class="form-text text-muted">Optional: Used as a fallback if the preview image can't be used by the browser.
39+
<br>For example using a jpg or png as fallback for avif which is not supported in Safari or Edge.</small>
3340
</div>
3441
<div class="form-check">
3542
<InputCheckbox class="form-check-input" id="published" @bind-Value="model.IsPublished" />

src/LinkDotNet.Blog.Web/Features/Admin/BlogPostEditor/Components/CreateNewModel.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public class CreateNewModel
3131

3232
public DateTime OriginalUpdatedDate { get; set; }
3333

34+
public string PreviewImageUrlFallback { get; set; }
35+
3436
public static CreateNewModel FromBlogPost(BlogPost blogPost)
3537
{
3638
return new CreateNewModel
@@ -43,6 +45,7 @@ public static CreateNewModel FromBlogPost(BlogPost blogPost)
4345
IsPublished = blogPost.IsPublished,
4446
PreviewImageUrl = blogPost.PreviewImageUrl,
4547
OriginalUpdatedDate = blogPost.UpdatedDate,
48+
PreviewImageUrlFallback = blogPost.PreviewImageUrlFallback,
4649
};
4750
}
4851

@@ -53,7 +56,7 @@ public BlogPost ToBlogPost()
5356
? null
5457
: OriginalUpdatedDate;
5558

56-
var blogPost = BlogPost.Create(Title, ShortDescription, Content, PreviewImageUrl, IsPublished, updatedDate, tags);
59+
var blogPost = BlogPost.Create(Title, ShortDescription, Content, PreviewImageUrl, IsPublished, updatedDate, tags, PreviewImageUrlFallback);
5760
blogPost.Id = Id;
5861
return blogPost;
5962
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
@using Microsoft.AspNetCore.StaticFiles
2+
@if (string.IsNullOrEmpty(PreviewImageUrlFallback))
3+
{
4+
<img src="@PreviewImageUrl" alt="Preview image blogpost" loading="@LazyLoadTag"/>
5+
}
6+
else
7+
{
8+
<picture>
9+
<source srcset="@PreviewImageUrl" type="@GetMimeType()"/>
10+
<img src="@PreviewImageUrlFallback" alt="Preview image blogpost" loading="@LazyLoadTag"/>
11+
</picture>
12+
}
13+
14+
@code {
15+
[Parameter]
16+
public string PreviewImageUrl { get; set; }
17+
18+
[Parameter]
19+
public string PreviewImageUrlFallback { get; set; }
20+
21+
[Parameter]
22+
public bool LazyLoadImage { get; set; }
23+
24+
private string LazyLoadTag => LazyLoadImage ? "lazy" : "eager";
25+
26+
private static readonly FileExtensionContentTypeProvider Provider = new();
27+
28+
static PreviewImage()
29+
{
30+
if (!Provider.Mappings.Keys.Contains(".avif"))
31+
{
32+
Provider.Mappings.Add(".avif", "image/avif");
33+
}
34+
}
35+
36+
private string GetMimeType()
37+
{
38+
Provider.TryGetContentType(PreviewImageUrl, out var contentType);
39+
return contentType;
40+
}
41+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
img {
2+
position: absolute;
3+
top: 0;
4+
left: 0;
5+
object-fit: cover;
6+
height: 100%;
7+
width: 100%;
8+
}

src/LinkDotNet.Blog.Web/Features/Components/ShortBlogPost.razor

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
<article>
66
<div class="blog-card @AltCssClass">
77
<div class="meta">
8-
<div class="photo" style="background-image: url(@BlogPost.PreviewImageUrl)"></div>
8+
<div class="photo">
9+
<PreviewImage PreviewImageUrl="@BlogPost.PreviewImageUrl"
10+
PreviewImageUrlFallback="@BlogPost.PreviewImageUrlFallback"
11+
LazyLoadImage="@LazyLoadPreviewImage"></PreviewImage>
12+
</div>
913
<ul class="details">
1014
<li class="date">@BlogPost.UpdatedDate.ToString("dd/MM/yyyy")</li>
1115
@if (BlogPost.Tags != null)
@@ -39,6 +43,9 @@
3943

4044
[Parameter]
4145
public bool UseAlternativeStyle { get; set; }
46+
47+
[Parameter]
48+
public bool LazyLoadPreviewImage { get; set; }
4249

4350
private string AltCssClass => UseAlternativeStyle ? "alt" : string.Empty;
4451

src/LinkDotNet.Blog.Web/Features/Components/ShortBlogPost.razor.css

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,11 @@
2323
.blog-card a:hover {
2424
color: #5ad67d;
2525
}
26-
.blog-card:hover .photo {
27-
transform: scale(1.3) rotate(3deg);
28-
}
2926
.blog-card .meta {
3027
position: relative;
3128
z-index: 0;
3229
height: 200px;
3330
}
34-
.blog-card .photo {
35-
position: absolute;
36-
top: 0;
37-
right: 0;
38-
bottom: 0;
39-
left: 0;
40-
background-size: cover;
41-
background-position: center;
42-
transition: transform 0.5s;
43-
}
4431
.blog-card .details,
4532
.blog-card .details ul {
4633
margin: auto;

src/LinkDotNet.Blog.Web/Features/Home/Index.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<div class="content px-4 my-2">
2828
@for (var i = 0; i < currentPage.Count; i++)
2929
{
30-
<ShortBlogPost BlogPost="currentPage[i]" UseAlternativeStyle="@(i % 2 != 0)"></ShortBlogPost>
30+
<ShortBlogPost BlogPost="currentPage[i]" UseAlternativeStyle="@(i % 2 != 0)" LazyLoadPreviewImage="i > 2"></ShortBlogPost>
3131
}
3232
</div>
3333
<BlogPostNavigation PageList="@currentPage"></BlogPostNavigation>

0 commit comments

Comments
 (0)