Skip to content

Commit

Permalink
feat(netlify): set cache headers (#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic authored Jul 12, 2024
1 parent 76decd2 commit 6dd65a0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-chicken-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/netlify': minor
---

Sets immutable cache headers for static assets
16 changes: 13 additions & 3 deletions packages/netlify/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,25 @@ async function writeNetlifyFrameworkConfig(config: AstroConfig, logger: AstroInt
.filter(Boolean as unknown as (pattern?: string) => pattern is string)
);

const headers = config.build.assetsPrefix
? undefined
: [
{
for: `${config.base}${config.base.endsWith('/') ? '' : '/'}${config.build.assets}/*`,
values: {
'Cache-Control': 'public, max-age=31536000, immutable',
},
},
];

// See https://docs.netlify.com/image-cdn/create-integration/
const deployConfigDir = new URL('.netlify/v1/', config.root);
await mkdir(deployConfigDir, { recursive: true });
await writeFile(
new URL('./config.json', deployConfigDir),
JSON.stringify({
images: { remote_images: remoteImages },
headers,
})
);
}
Expand Down Expand Up @@ -432,9 +444,7 @@ export default function netlifyIntegration(
rootDir = config.root;
_config = config;

if (config.image?.domains?.length || config.image?.remotePatterns?.length) {
await writeNetlifyFrameworkConfig(config, logger);
}
await writeNetlifyFrameworkConfig(config, logger);

const edgeMiddleware = integrationConfig?.edgeMiddleware ?? false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import netlify from '@astrojs/netlify';
import { defineConfig } from 'astro/config';

export default defineConfig({
output: 'static',
adapter: netlify(),
site: `http://example.com`,
site: `http://example.com`,
output: 'static',
adapter: netlify(),
site: "http://example.com",
redirects: {
'/other': '/',
'/two': {
Expand All @@ -14,4 +13,4 @@ export default defineConfig({
},
'/blog/[...slug]': '/team/articles/[...slug]',
},
});
});
25 changes: 25 additions & 0 deletions packages/netlify/test/static/headers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { loadFixture } from '@astrojs/test-utils';

describe('SSG - headers', () => {
let fixture;

before(async () => {
fixture = await loadFixture({ root: new URL('./fixtures/redirects/', import.meta.url) });
await fixture.build();
});

it('Generates headers for static assets', async () => {
const config = await fixture.readFile('../.netlify/v1/config.json');
const headers = JSON.parse(config).headers;
assert.deepEqual(headers, [
{
for: '/_astro/*',
values: {
'Cache-Control': 'public, max-age=31536000, immutable',
},
},
]);
});
});

0 comments on commit 6dd65a0

Please sign in to comment.