Skip to content

Commit 77b5aa4

Browse files
authored
feat: image cdn support (#303)
* feat: image cdn support * chore: use tagged version in test * chore: update gatsby * chore: build before running test * chore: refactor to allow image cdn to be disabled * chore: add docs
1 parent fd99381 commit 77b5aa4

File tree

15 files changed

+3582
-1560
lines changed

15 files changed

+3582
-1560
lines changed

.prettierignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ node_modules
1818
public
1919
.DS_Store
2020
.parcel-cache
21-
package-lock.json
21+
package-lock.json
22+
plugin/README.md

README.md

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
![Netlify Build plugin Gatsby – Run Gatsby seamlessly on Netlify](netlify-gatsby-plugin.png)
22

3-
# Essential Gatsby Plugin - v2
3+
# Essential Gatsby Plugin
44

55
The Essential Gatsby build plugin enables caching of builds, SSR and DSG
6-
[render modes](https://v4.gatsbyjs.com/docs/conceptual/rendering-options/) and
7-
Gatsby Functions. It is installed automatically for all new Gatsby sites.
6+
[render modes](https://v4.gatsbyjs.com/docs/conceptual/rendering-options/),
7+
image CDN and Gatsby Functions. It is installed automatically for all new Gatsby
8+
sites.
89

910
> **Note:**
1011
>
@@ -29,13 +30,16 @@ Gatsby sites need two plugins to support all features.
2930

3031
### Installing the Netlify build plugin
3132

32-
New Gatsby sites on Netlify automatically install the Essential Gatsby build plugin. You can confirm this in the build logs. If you need to install it
33+
New Gatsby sites on Netlify automatically install the Essential Gatsby build
34+
plugin. You can confirm this in the build logs. If you need to install it
3335
manually, you have two options:
3436

35-
- [The Netlify UI](https://docs.netlify.com/configure-builds/build-plugins/#ui-installation). Here, you can search for "Essential Gatsby" and install the plugin.
37+
- [The Netlify UI](https://docs.netlify.com/configure-builds/build-plugins/#ui-installation).
38+
Here, you can search for "Essential Gatsby" and install the plugin.
3639

37-
- [File-based plugin installation](https://docs.netlify.com/configure-builds/build-plugins/#file-based-installation). You can install the plugin as `@netlify/plugin-gatsby` in your `netlify.toml` file.
38-
40+
- [File-based plugin installation](https://docs.netlify.com/configure-builds/build-plugins/#file-based-installation).
41+
You can install the plugin as `@netlify/plugin-gatsby` in your `netlify.toml`
42+
file.
3943

4044
### Install the Gatsby Plugin
4145

@@ -65,19 +69,70 @@ for more information, including optional plugin configuration.
6569
### Disabling Netlify functions
6670

6771
In order to support Gatsby Functions and DSG and SSR render modes, this plugin
68-
generates three Netlify Functions called `__api`, `__ssr` and `__dsg`. If you
69-
are not using any of these modes, then you can disable the creation of these
72+
generates four Netlify Functions called `__api`, `__ssr`, `__dsg` and `_ipx`. If
73+
you are not using any of these modes, then you can disable the creation of these
7074
functions. If you are using the latest version of `gatsby-plugin-netlify` then
7175
this will be handled automatically, disabling functions if the site has no
7276
Gatsby Functions, or DSG/SSR pages. Otherwise, you can do this manually by
7377
setting the environment variable `NETLIFY_SKIP_GATSBY_FUNCTIONS` to `true`. Be
7478
aware that if you do this, any DSG or SSR pages will not work, and nor will any
75-
Gatsby Functions.
79+
Gatsby Functions or the remote image CDN.
80+
81+
### Gatsby Image CDN
82+
83+
Gatsby 5 includes beta support for deferred image resizing using a CDN. This can
84+
greatly improve build times for sites with remote images, such as those that use
85+
a CMS. When using these, images do not need to be downloaded and resized at
86+
build time, and instead are built on the fly when first loaded. The image CDN is
87+
enabled by default on Netlify, but can be disabled by setting the environment
88+
variable `GATSBY_CLOUD_IMAGE_CDN` to `false`.
89+
90+
When using the image CDN, Gatsby generates URLs of the form
91+
`/_gatsby/image/...`. On Netlify, these are served by a
92+
[builder function](https://docs.netlify.com/configure-builds/on-demand-builders/),
93+
powered by [sharp](https://sharp.pixelplumbing.com/) and Nuxt's
94+
[ipx image server](https://github.com/unjs/ipx/). It supports all image formats
95+
supported by Gatsby, including AVIF and WebP.
96+
97+
On first load there will be a one-time delay while the image is resized, but
98+
subsequent requests will be super-fast as they are served from the cache.
99+
100+
Currently Gatsby supports the image CDN for sites that use Contentful or
101+
WordPress, but more should be added and will be enabled automatically when
102+
available in the plugin.
103+
104+
When using WordPress with the image CDN, we recommend disabling downloading of
105+
files if possible by setting
106+
[`createFileNodes`](https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-wordpress/docs/plugin-options.md#typemediaitemcreatefilenodes)
107+
to `false`. You should also setup an image size in WordPress to use as a
108+
placeholder. See
109+
[the `gatsby-source-wordpress` docs](https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-wordpress/docs/plugin-options.md#typemediaitemplaceholdersizename)
110+
for more details.
111+
112+
For example:
113+
114+
```js
115+
module.exports = {
116+
plugins: [
117+
{
118+
resolve: `gatsby-source-wordpress`,
119+
options: {
120+
url: 'https://example.com/graphql',
121+
type: { MediaItem: { createFileNodes: false } },
122+
},
123+
},
124+
],
125+
}
126+
```
127+
128+
For more information about Gatsby's image CDN feature, see
129+
[the Gatsby docs](https://gatsby.dev/img).
76130

77131
### Caveats
78132

79133
Currently you cannot use `StaticImage` or `gatsby-transformer-sharp` in SSR or
80-
DSG pages. The best workaround is to use an image CDN such as
134+
DSG pages. Support for Gatsby Image CDN is coming soon. The best workaround is
135+
to use an image CDN such as
81136
[Cloudinary](https://www.gatsbyjs.com/docs/how-to/images-and-media/using-cloudinary-image-service/)
82137
or [imgix](https://github.com/imgix/gatsby) to host your images. This will give
83138
you faster builds and rendering too.

demo/netlify.toml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,3 @@ package = "../plugin/src/index.ts"
88

99
[[plugins]]
1010
package = "@netlify/plugin-local-install-core"
11-
12-
[[redirects]]
13-
from = "/dog"
14-
to = "/.netlify/functions/__ssr"
15-
status = 200
16-
17-
[[redirects]]
18-
from = "/page-data/dog/page-data.json"
19-
to = "/.netlify/functions/__ssr"
20-
status = 200

demo/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
},
3030
"devDependencies": {
3131
"@netlify/functions": "latest",
32+
"@netlify/ipx": "latest",
3233
"co-body": "^6.1.0",
3334
"cookie": "^0.4.1",
3435
"download": "^8.0.0",

0 commit comments

Comments
 (0)