Partners

The DatoCMS Blog

Hyper-fast development with Gatsby and DatoCMS

Posted on April 16th, 2019 by Stefano Verna

The wait is over! We finally have a new, blazing fast and super-stable plugin for our beloved Gatsby users!

Changes include, but are not limited to:

  • โœจ Hyper-fast real-time content preview in watch mode

  • ๐ŸŒŽ Locale fallbacks

  • ๐Ÿ–ผ๏ธ Support for traced SVG image placeholders

  • ๐Ÿ› ๏ธ GraphQL schema customization (no more random errors due to inferred types!)

Despite being a complete rewrite of the plugin, we made significant efforts not to break backward compatibility in any way, so you all should be able to upgrade your Gatsby v2 projects in like 30 seconds:

$ yarn upgrade gatsby-source-datocms
$ npm upgrade gatsby-source-datocms

In addition to that, we've also released a new Gatsby Remark plugin that intercepts any DatoCMS image contained in your markdown and processes it so that it can be safely used in the production build without the fear of serving heavy raw high-res images! Adding it to your project is โ€” again โ€” a matter of seconds:

$ yarn add gatsby-remark-images-datocms
$ npm install gatsby-remark-images-datocms --save

Continue reading below to discover in detail all the great, new features weโ€™ve shipped! ๐Ÿ’œ

๐Ÿ› ๏ธ No more random errors due to inferred types!

Yes, we know it might not sound like the fanciest update you can get, but this was THE single, most reported issue for our plugin โ€” and for any other Gatsby source plugin, really โ€” and finally it has been solved, once and for all. Hurray! ๐ŸŽ‰

The way Gatsby has always offered a unified GraphQL API to fetch content coming from various external sources relies on Gatsby itself trying to guess a schema from whatever content is available at any given moment. While this strategy allowed plugin developers to build new integrations with Gatsby very quickly, it exposed the end-user to an endless number of confusing and hard to debug problems.

Gatsby 2.2.0 finally released some new APIs that allow plugin developers to finally customize the GraphQL schema. We started testing the new APIs with the Gatsby team the minute after they have been announced (thanks Mikhail Novikov for your support!) and the final result it's now ready.

So yep, no more broken builds for random reasons!

โšก Hyper fast real-time content preview

We've always had watch mode, but now we've made it super-fast, as just the content that changes gets re-downloaded from the API.

Watch mode is enabled by default, so no need to add further flags in the plugin's options hash.

๐Ÿ–ผ๏ธ Support for traced SVG image placeholders

In addition to blur-up placeholders for gatsby-image, now you can get traced SVG image placeholders as well!

To use them, you just need to replace the standard GatsbyDatoCmsFluid fragment with GatsbyDatoCmsFluid_tracedSVG (the same applies for GatsbyDatoCmsFixed and GatsbyDatoCmsFixed_tracedSVG) in your code:

import Img from 'gatsby-image';
class Authors extends React.Component {
render() {
const authors = this.props.data.authors.nodes;
return (
<ul>
{
authors.map(author => (
<li key={author.id}>
<Img fluid={author.avatar.fluid} />
<h3>{author.name}</h3>
</li>
))
}
</ul>
);
}
}
export const query = graphql`
query SupportQuery {
authors: allDatoCmsAuthor {
nodes {
id
name
avatar {
fluid(maxWidth: 60) {
...GatsbyDatoCmsFixed_tracedSVG
}
}
}
}
}
`;

Furthermore, gatsby-image will now automatically use WebP images when the browser supports the file format and fallback to the default image format otherwise.

๐Ÿ“ Responsive, optimized images in Markdown

We've released a new Gatsby Remark plugin that will intercept any DatoCMS image contained in your DatoCMS Markdown fields, and process them so they can be used in the production build.

In the processing, it make images coming from DatoCMS responsive by:

  • Adding an elastic container to hold the size of the image while it loads to avoid layout jumps.

  • Generating multiple versions of images at different widths and sets the srcset and sizes of the img element so regardless of the width of the device, the correct image is downloaded.

  • Converting GIF images into <video> elements.

  • Showing a blurred up or traced SVG placeholder until the actual image is downloaded.

If you already have the gatsby-transformer-remark plugin in your project (which is probably the case), then just add gatsby-remark-images-datocms to its plugins:

$ yarn add gatsby-remark-images-datocms
module.exports = {
plugins: [
// ...
{
resolve: `gatsby-transformer-remark`,
options: {
plugins: [
`gatsby-plugin-sharp`,
{
resolve: `gatsby-remark-images-datocms`,
options: {
apiToken: process.env.DATO_API_TOKEN,
},
},
],
},
},
// ...
],
};

๐ŸŒŽ Locale fallbacks

If you have a multi-language website, some translations for secondary languages might be missing. You can now globally specify a set of languages to use instead of the missing one:

plugins: [
{
resolve: `gatsby-source-datocms`,
options: {
// In this example, if some field value is missing in Italian, fall back to English
localeFallbacks: {
it: ['en'],
},
},
},
]

๐Ÿ‡ฌ๐Ÿ‡ง๐Ÿ‡ฎ๐Ÿ‡น๐Ÿ‡ช๐Ÿ‡ธ Get all locales in a single query

If you need to get a field's value in every locale, you can now use the _all<FIELDNAME>Locales method:

{
allDatoCmsBlogPost {
nodes {
_allTitleLocales {
locale
value
}
}
}
}

This will return an array easy to iterate on:

{
allDatoCmsBlogPost: {
nodes: [
{
_allTitleLocales: [
{ locale: "en", value: "Hi there!" },
{ locale: "it", value: "Ciao!" }
]
}
]
}
}

๐Ÿ” Every meta attribute at your disposal

You can now get a lot of new meta-information regarding your records within the meta field:

{
allDatoCmsBlogPost {
nodes {
meta {
createdAt
updatedAt
publishedAt
firstPublishedAt
isValid
status
}
}
}
}

Wrap-up

We truly canโ€™t wait to see the great things you build and how you will use all these new features and improvements. If you have any issues with the upgrade, just head over to our #gatsby Slack channel, we'll be there to help you.

Happy progressive web-apping everyone!