Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[netlify] allow opting out of Image CDN #120

Merged
merged 5 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
*
alexanderniebuhr marked this conversation as resolved.
Show resolved Hide resolved
* @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,
ematipico marked this conversation as resolved.
Show resolved Hide resolved
},
},
});
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,
ematipico marked this conversation as resolved.
Show resolved Hide resolved
}),
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
})
})
});