Contentlayer with next/image

This page summarizes the projects mentioned and recommended in the original post on dev.to

Our great sponsors
  • SurveyJS - Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • rehype

    HTML processor powered by plugins part of the @unifiedjs collective

  • The next idea was to use normal markdown images and to place the images in the public folder. This eliminates the need for static import and treats our image like a remote image. But in order to make this work, we have to tell next/image the dimensions of the image. If we would use a static import for the image, the import magic would provide the dimensions for us. To pass the width and height to the image component we use a rehype plugin called rehype-img-size.

  • rehype-img-size

    rehype plugin to set local image size properties to img tag.

  • The next idea was to use normal markdown images and to place the images in the public folder. This eliminates the need for static import and treats our image like a remote image. But in order to make this work, we have to tell next/image the dimensions of the image. If we would use a static import for the image, the import magic would provide the dimensions for us. To pass the width and height to the image component we use a rehype plugin called rehype-img-size.

  • SurveyJS

    Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.

    SurveyJS logo
  • remark

    markdown processor powered by plugins part of the @unifiedjs collective (by remarkjs)

  • contentlayer uses remark to parse the markdown in an mdast. We can now use remark plugins to modify the mdast. Then rehype comes into play and converts the mdast into a hast. rehype plugins can now modify the hast. Finally the hast is converted into react components.

  • mdast

    Markdown Abstract Syntax Tree format

  • contentlayer uses remark to parse the markdown in an mdast. We can now use remark plugins to modify the mdast. Then rehype comes into play and converts the mdast into a hast. rehype plugins can now modify the hast. Finally the hast is converted into react components.

  • hast

    Hypertext Abstract Syntax Tree format

  • contentlayer uses remark to parse the markdown in an mdast. We can now use remark plugins to modify the mdast. Then rehype comes into play and converts the mdast into a hast. rehype plugins can now modify the hast. Finally the hast is converted into react components.

  • sdorra.dev

    A blog about web development

  • For a more complete example have a look at the source of this blog.

  • contentlayer

    Contentlayer turns your content into data - making it super easy to import MD(X) and CMS content in your app

  • My first reaction was to use MDX and use next/image just as in the example. But that means that we can't use normal markdown images and it turns out that this won't work with contentlayer. This wont work, because Next.js does some magic on the import of the static image. The object which gets returned by the import, contains not only a path to the image, it contains also the width and height, plus a very small version of the image for the blurred placeholder. This magic does not work if the MDX file is loaded with contentlayer, because contentlayer uses its own bundler, which does not know about the import magic for images.

  • WorkOS

    The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.

    WorkOS logo
  • sharp

    High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP, AVIF and TIFF images. Uses the libvips library.

  • Now that we have the path, we can use sharp to read the width and height of the image.

  • React

    The library for web and native user interfaces.

  • contentlayer uses remark to parse the markdown in an mdast. We can now use remark plugins to modify the mdast. Then rehype comes into play and converts the mdast into a hast. rehype plugins can now modify the hast. Finally the hast is converted into react components.

  • Next.js

    The React Framework

  • My first reaction was to use MDX and use next/image just as in the example. But that means that we can't use normal markdown images and it turns out that this won't work with contentlayer. This wont work, because Next.js does some magic on the import of the static image. The object which gets returned by the import, contains not only a path to the image, it contains also the width and height, plus a very small version of the image for the blurred placeholder. This magic does not work if the MDX file is loaded with contentlayer, because contentlayer uses its own bundler, which does not know about the import magic for images.

  • mdx

    Markdown for the component era

  • My first reaction was to use MDX and use next/image just as in the example. But that means that we can't use normal markdown images and it turns out that this won't work with contentlayer. This wont work, because Next.js does some magic on the import of the static image. The object which gets returned by the import, contains not only a path to the image, it contains also the width and height, plus a very small version of the image for the blurred placeholder. This magic does not work if the MDX file is loaded with contentlayer, because contentlayer uses its own bundler, which does not know about the import magic for images.

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts