Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce "blurhash"-like functionality using NextJS-builtin "placeholder" functionality. #74

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions blog/app/metadata/[slug]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export async function GET(_request: NextRequest, { params }: PageProperties) {

if (cover_data) {
for (const [key, cover] of Object.entries(cover_data)) {
if (typeof cover === 'string') {
continue;
}

const awaitedData = await cover;

data.assets.covers[key] = awaitedData.default.src || data['cover'];
Expand Down
13 changes: 12 additions & 1 deletion blog/buildAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ const getCoverImages = async () => {
format,
data,
});

foundImageFile = true;
break;
} catch {
Expand Down Expand Up @@ -175,6 +174,15 @@ const handleCoverImages = async () => {
for (const cover of covers) {
result += ` '${cover.post}': {\n`;

const potatoImage = await sharp(cover.data)
.resize(64, 36)
.webp({
quality: 4,
effort: 6,
alphaQuality: 0,
})
.toBuffer();

for (const settings of COVER_IMG_SETTINGS) {
const { prefix, suffix, width, height, format } = settings;

Expand All @@ -194,6 +202,9 @@ const handleCoverImages = async () => {
format || 'webp'
}') as Promise<{default: StaticImageData}>,\n`;
}
result += ` 'cover-potato': 'data:image/webp;base64,${potatoImage.toString(
'base64'
)}',\n`;

result += ' },\n';
}
Expand Down
10 changes: 9 additions & 1 deletion blog/src/components/PostPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export const BlogPostPreview = async ({ post, variant }: Properties) => {
})
: undefined;

const potato = Object.keys(covers).includes(post.file)
? covers[post.file as keyof typeof covers]['cover-potato']
: undefined;

const { readingTime } = (await import(
`../../../content/${post.file}/readme.mdx`
)) as {
Expand All @@ -46,8 +50,12 @@ export const BlogPostPreview = async ({ post, variant }: Properties) => {
{cover ? (
<Image
src={cover}
width={1200}
height={600}
className="aspect-cover h-full w-full object-cover"
alt={post.title}
placeholder="blur"
blurDataURL={potato}
/>
) : (
<img
Expand All @@ -59,7 +67,7 @@ export const BlogPostPreview = async ({ post, variant }: Properties) => {
</span>
<span
className={clsx(
'flex h-full flex-col gap-2 p-5 md:p-6 max-h-[200px] my-auto',
'my-auto flex h-full max-h-[200px] flex-col gap-2 p-5 md:p-6',
variant == 'horizontal'
? 'border-t md:border-none'
: 'border-t'
Expand Down
Loading