Skip to content

Commit

Permalink
[netlify] allow opting out of Image CDN (#120)
Browse files Browse the repository at this point in the history
* chore: add image CDN tests

* feat: allow image cdn opt-out

* write docs

* add changeset

* fix: put proper jsdoc values
  • Loading branch information
Skn0tt authored Jan 3, 2024
1 parent 4055ff0 commit cf39b9d
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/slimy-donuts-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/netlify': minor
---

Adds opt-out option for Image CDN.
16 changes: 14 additions & 2 deletions packages/netlify/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,19 @@ export interface NetlifyIntegrationConfig {
* If enabled, Astro Middleware is deployed as an Edge Function and applies to all routes.
* Caveat: Locals set in Middleware are not applied to prerendered pages, because they've been rendered at build-time and are served from the CDN.
*
* @default disabled
* @default {false}
*/
edgeMiddleware?: boolean;

/**
* If enabled, Netlify Image CDN is used for image optimization.
* This transforms images on-the-fly without impacting build times.
*
* If disabled, Astro's built-in image optimization is run at build-time instead.
*
* @default {true}
*/
imageCDN?: boolean
}

export default function netlifyIntegration(
Expand Down Expand Up @@ -226,6 +236,8 @@ export default function netlifyIntegration(

outDir = new URL('./dist/', rootDir);

const enableImageCDN = isRunningInNetlify && (integrationConfig?.imageCDN ?? true);

updateConfig({
outDir,
build: {
Expand All @@ -242,7 +254,7 @@ export default function netlifyIntegration(
},
image: {
service: {
entrypoint: isRunningInNetlify ? '@astrojs/netlify/image-service.js' : undefined,
entrypoint: enableImageCDN ? '@astrojs/netlify/image-service.js' : undefined,
},
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default defineConfig({
output: 'server',
adapter: netlify({
edgeMiddleware: process.env.EDGE_MIDDLEWARE === 'true',
imageCDN: process.env.DISABLE_IMAGE_CDN ? false : undefined,
}),
site: `http://example.com`,
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
import { Image } from 'astro:assets';
import astronautImage from "../astronaut.jpg"
export const prerender = true;
---

<Image src={astronautImage} alt="an astronaut floating in space" />

43 changes: 43 additions & 0 deletions packages/netlify/test/functions/image-cdn.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { loadFixture } from '@astrojs/test-utils';
import { expect } from 'chai';
import { describe } from 'node:test';

describe('Image CDN', () => {
const root = new URL('./fixtures/middleware/', import.meta.url);

describe("when running outside of netlify", () => {
it("does not enable Image CDN", async () => {
const fixture = await loadFixture({ root });
await fixture.build();

const astronautPage = await fixture.readFile('astronaut/index.html');
expect(astronautPage).contains(`src="/_astro/astronaut.`)
})
})

describe("when running inside of netlify", () => {
it("enables Netlify Image CDN", async () => {
process.env.NETLIFY = 'true'
const fixture = await loadFixture({ root });
await fixture.build();

const astronautPage = await fixture.readFile('astronaut/index.html');
expect(astronautPage).contains(`src="/.netlify/image`)

process.env.NETLIFY = undefined
})

it("respects image CDN opt-out", async () => {
process.env.NETLIFY = 'true'
process.env.DISABLE_IMAGE_CDN = 'true'
const fixture = await loadFixture({ root });
await fixture.build();

const astronautPage = await fixture.readFile('astronaut/index.html');
expect(astronautPage).contains(`src="/_astro/astronaut.`)

process.env.NETLIFY = undefined
process.env.DISABLE_IMAGE_CDN = undefined
})
})
});

0 comments on commit cf39b9d

Please sign in to comment.