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

fix(feed): 一部のフィードでMaximum call stack size exceededが発生する #161

Merged
merged 1 commit into from
Aug 24, 2024
Merged
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
20 changes: 16 additions & 4 deletions eleventy.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import path from 'path';
import ts from 'typescript';
import { imageCacheOptions } from './src/common/eleventy-cache-option';
import CleanCSS from "clean-css";
import { Sharp } from 'sharp';
import sharpIco, {ImageData} from "sharp-ico";
import url from 'url';
import Eleventy from '@11ty/eleventy';
Expand All @@ -31,6 +30,13 @@ const minifyHtmlTransform = (content: string, outputPath: string) => {
}

const imageThumbnailShortcode = async (src: string, alt: string, pathPrefix: string = '') => {
// 取れなければ代替画像
const alternativeImageTag = `<img src='${pathPrefix}images/alternate-feed-image.png' alt='${alt}' loading='lazy' width='256' height='256'>`;

if (!src) {
return alternativeImageTag;
}

let metadata: EleventyImage.Metadata;

try {
Expand All @@ -50,7 +56,7 @@ const imageThumbnailShortcode = async (src: string, alt: string, pathPrefix: str
} catch {
// エラーが起きたら代替画像にする
console.log('[image-thumbnail-short-code] error', src);
return `<img src='${pathPrefix}images/alternate-feed-image.png' alt='${alt}' loading='lazy' width='256' height='256'>`
return alternativeImageTag;
}

return EleventyImage.generateHTML(metadata, {
Expand All @@ -62,6 +68,13 @@ const imageThumbnailShortcode = async (src: string, alt: string, pathPrefix: str
}

const imageIconShortcode = async (src: string, alt: string, pathPrefix: string = '') => {
// 取れなければ画像なし
const alternativeImageTag = ``;

if (!src) {
return alternativeImageTag;
}

const parsedUrl = url.parse(src);
const fileName = path.basename(parsedUrl.pathname || '');
const fileExtension = path.extname(fileName).toLowerCase();
Expand All @@ -81,9 +94,8 @@ const imageIconShortcode = async (src: string, alt: string, pathPrefix: string =
imageSrc = await sharpIcoImage.image.png().toBuffer();
}
} catch (error) {
// エラーが起きたら画像なし
console.error('[image-icon-short-code] Error processing ICO:', src, error);
return ``;
return alternativeImageTag;
}
}

Expand Down
13 changes: 11 additions & 2 deletions src/feed/utils/feed-crawler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,11 +383,18 @@ export class FeedCrawler {
const ogObject = ogsResponse.result;
const ogImages = ogObject?.ogImage;

// データの調整
let validOgImages: ImageObject[] = [];

// データの調整しつつ、利用可能なものを取得
if (ogImages !== undefined) {
for (const ogImage of ogImages) {
const ogImageUrl = ogImage.url;

// 画像がないものはスキップ
if (ogImageUrl == null || ogImageUrl.trim() === '') {
continue;
}

// 一部URLがおかしいものの対応
if (ogImageUrl && ogImageUrl.startsWith('https://tech.fusic.co.jphttps')) {
ogImage.url = ogImageUrl.substring('https://tech.fusic.co.jp'.length);
Expand All @@ -397,12 +404,14 @@ export class FeedCrawler {
if (ogImageUrl && !ogImageUrl.startsWith('http')) {
ogImage.url = new URL(ogImageUrl, url).toString();
}

validOgImages.push(ogImage);
}
}

// 画像は一つとして扱いたいのでカスタム
const customOgObject: CustomOgObject = ogObject;
customOgObject.customOgImage = ogImages?.[0];
customOgObject.customOgImage = validOgImages[0] || null;

// faviconはフルURLにする
if (customOgObject.favicon && !customOgObject.favicon.startsWith('http')) {
Expand Down
2 changes: 1 addition & 1 deletion src/feed/utils/feed-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class FeedGenerator {
const feedItemContent = (feedItem.summary || feedItem.contentSnippet || '').replace(/(\n|\t+|\s+)/g, ' ');

const ogObject = feedItemOgObjectMap.get(feedItem.link);
// 配列担っているが2つ目以降を使う理由もないので0を使う
// 配列になっているが2つ目以降を使う理由もないので0を使う
const ogImage = ogObject?.customOgImage;

// 日付がないものは入れない
Expand Down
2 changes: 1 addition & 1 deletion src/site/blog-feed.njk
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ permalink: "blogs/{{ blogFeed.linkMd5Hash }}/"
{% asyncEach feedItem in blogFeed.items %}
<div class='ui-feed-item'>
<a class='ui-feed-item__og-image' href='{{ feedItem.link }}'>
{% if feedItem.ogImageUrl %}
{% if feedItem.ogImageUrl !== null %}
{% imageThumbnail feedItem.ogImageUrl, '記事のアイキャッチ画像', page.url | relativeUrl %}
{% else %}
<img src='{{ page.url | relativeUrl }}images/alternate-feed-image.png' loading="lazy" alt='記事のアイキャッチ画像' width='256' height='256'>
Expand Down
2 changes: 1 addition & 1 deletion src/site/blogs.njk
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ eleventyComputed:
{% asyncEach blogFeed in blogFeeds %}
<div class='ui-blog'>
<a class='ui-blog__og-image' href='./{{ blogFeed.linkMd5Hash }}/'>
{% if blogFeed.ogImageUrl %}
{% if blogFeed.ogImageUrl !== null %}
{% imageThumbnail blogFeed.ogImageUrl, 'ブログのアイキャッチ画像', '../' %}
{% else %}
<img src='{{ page.url | relativeUrl }}images/alternate-feed-image.png' loading="lazy" alt='記事のアイキャッチ画像' width='256' height='256'>
Expand Down