gatsby-imageを用いてサムネイルを追加する

目次

Gatsby.jsで作られたスターターにサムネイルを追加します。

私はgatsby-starter-lumenを使用してますが、他のスターターにも当然使えます。

GraphQLで画像を取得します。

src\templates\index-template.js

export const query = graphql`
  query IndexTemplate($postsLimit: Int!, $postsOffset: Int!) {
    allMarkdownRemark(
        limit: $postsLimit,
        skip: $postsOffset,
        filter: { frontmatter: { template: { eq: "post" }, draft: { ne: true } } },
        sort: { order: DESC, fields: [frontmatter___date] }
      ){
      edges {
        node {
          fields {
            slug
            categorySlug
            thumbnail{
              childImageSharp {
                fluid(maxWidth: 640, maxHeight: 360) {
                  ...GatsbyImageSharpFluid
                }
              }
            }
          }
          frontmatter {
            title
            date
            category
            description
          }
        }
      }
    }
  }
`;

実際に画像を挿入したい部分にIMGタグを追加。

<Img fluid={edge.node.fields.thumbnail.childImageSharp.fluid} />

src\components\Feed\Feed.js全体

// @flow strict
import React from 'react';
import moment from 'moment';
import 'moment/locale/ja';
import { Link } from 'gatsby';
import Img from 'gatsby-image';
import type { Edges } from '../../types';
import styles from './Feed.module.scss';

type Props = {
  edges: Edges
};

const Feed = ({ edges }: Props) => (
  <div className={styles['feed']}>
    {edges.map((edge) => (
      <div className={styles['feed__item']} key={edge.node.fields.slug}>
        <Link className={styles['feed__item-title-link']} to={edge.node.fields.slug}>
          <div className={styles['feed__thumbnail']}>
            <div className={styles['feed__thumbnail__inner']}></div>
            <Img fluid={edge.node.fields.thumbnail.childImageSharp.fluid} />
          </div>
          <div className={styles['feed__item-title-and-meta']}>
            <h2 className={styles['feed__item-title']}>
              {edge.node.frontmatter.title}
            </h2>
            <div className={styles['feed__item-meta']}>
              <time className={styles['feed__item-meta-time']} dateTime={moment(edge.node.frontmatter.date).format('YYYY-MM-DD')}>
                {moment(edge.node.frontmatter.date).locale('ja').format('LLL')}
              </time>
              <span className={styles['feed__item-meta-divider']} />
              <span className={styles['feed__item-meta-category']}>
              <object><Link to={edge.node.fields.categorySlug} className={styles['feed__item-meta-category-link']}>{edge.node.frontmatter.category}</Link></object>
              </span>
            </div>
          </div>
        </Link>
      </div>
    ))}
  </div>
);

export default Feed;

src\components\Feed\Feed.module.scss

@import '../../assets/scss/variables';
@import '../../assets/scss/mixins';

.feed {
  display:-webkit-box;
  display:-moz-box;
  display:-ms-box;
  display:-webkit-flexbox;
  display:-moz-flexbox;
  display:-ms-flexbox;
  display:-webkit-flex;
  display:-moz-flex;
  display:-ms-flex;
  display:flex;
  -webkit-box-lines:multiple;
  -moz-box-lines:multiple;
  -webkit-flex-wrap:wrap;
  -moz-flex-wrap:wrap;
  -ms-flex-wrap:wrap;
  flex-wrap:wrap;

  &__thumbnail {
    width: 100%;

    img{
      width: 100%;
    }
  }

  &__item {
    @include margin-bottom(1.25);
    width: 100%;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;

    border : solid 1px #e9e9e9;
    &:hover {
      background-color:#f5f4f3;
    }

    &:last-child {
      @include margin-bottom(.5);
    }

    &-title-and-meta {
      padding: 5px;
    }

    &-title {
      font-size: $typographic-base-font-size * 1.2;//1.6875;
      @include line-height(1.5);
      @include margin-top(0);
      @include margin-bottom(.5);

      &-link {
        color: $color-base;

        &:hover,
        &:focus {
          color: $color-base;
          border-bottom: 1px solid $color-base;
        }

      }

    }

    &-meta {
      &-time {
        font-size: $typographic-small-font-size;
        color: $color-base;
        font-weight: 600;
        text-transform: uppercase;
      }

      &-divider {
        margin: 0 5px;
      }

      &-category {
        &-link {
          font-size: $typographic-small-font-size;
          color: $color-secondary;
          font-weight: 600;
          text-transform: uppercase;

          &:hover,
          &:focus {
            color: $color-primary;
          }

        }

      }

    }

  }

}

@include breakpoint-sm {
  .feed {
    width: 100%;
	  //display: inline-block;
    &__item {
      lost-column: 1/2;

      &-title {
        font-size: $typographic-base-font-size * 1.2;//1.6875;
      }
    }
    
  }
}

ソースコード全体はGithubにあるので是非参考にしてください。