Skip to content

Commit

Permalink
feat: cache assets in Vercel adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
kidonng authored and soilSpoon committed Jul 20, 2023
1 parent 47c698b commit 373665d
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-tips-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/vercel': major
---

Add cache headers to assets in Vercel adapter
5 changes: 5 additions & 0 deletions packages/integrations/vercel/src/edge/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ export default function vercelEdge({
version: 3,
routes: [
...getRedirects(routes, _config),
{
src: `^/${_config.build.assets}/(.*)$`,
headers: { 'cache-control': 'public, max-age=31536000, immutable' },
continue: true,
},
{ handle: 'filesystem' },
{ src: '/.*', dest: 'render' },
],
Expand Down
11 changes: 10 additions & 1 deletion packages/integrations/vercel/src/serverless/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,16 @@ export default function vercelServerless({
// https://vercel.com/docs/build-output-api/v3#build-output-configuration
await writeJson(new URL(`./config.json`, _config.outDir), {
version: 3,
routes: [...getRedirects(routes, _config), { handle: 'filesystem' }, ...routeDefinitions],
routes: [
...getRedirects(routes, _config),
{
src: `^/${_config.build.assets}/(.*)$`,
headers: { 'cache-control': 'public, max-age=31536000, immutable' },
continue: true,
},
{ handle: 'filesystem' },
...routeDefinitions,
],
...(imageService || imagesConfig
? { images: imagesConfig ? imagesConfig : defaultImageConfig }
: {}),
Expand Down
10 changes: 9 additions & 1 deletion packages/integrations/vercel/src/static/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,15 @@ export default function vercelStatic({
// https://vercel.com/docs/build-output-api/v3#build-output-configuration
await writeJson(new URL(`./config.json`, getVercelOutput(_config.root)), {
version: 3,
routes: [...getRedirects(routes, _config), { handle: 'filesystem' }],
routes: [
...getRedirects(routes, _config),
{
src: `^/${_config.build.assets}/(.*)$`,
headers: { 'cache-control': 'public, max-age=31536000, immutable' },
continue: true,
},
{ handle: 'filesystem' },
],
...(imageService || imagesConfig
? { images: imagesConfig ? imagesConfig : defaultImageConfig }
: {}),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { defineConfig } from 'astro/config';

export default defineConfig({
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/astro-vercel-static-assets",
"version": "0.0.0",
"private": true,
"dependencies": {
"@astrojs/vercel": "workspace:*",
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>Testing</title>
</head>
<body>
<h1>Testing</h1>
</body>
</html>
84 changes: 84 additions & 0 deletions packages/integrations/vercel/test/static-assets.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { expect } from 'chai';
import { loadFixture } from './test-utils.js';

describe('Static Assets', () => {
/** @type {import('../../../astro/test/test-utils.js').Fixture} */
let fixture;

const VALID_CACHE_CONTROL = 'public, max-age=31536000, immutable';

async function build({ adapter, assets }) {
fixture = await loadFixture({
root: './fixtures/static-assets/',
adapter,
build: {
assets,
}
});
await fixture.build();
}

async function getConfig() {
const json = await fixture.readFile('../.vercel/output/config.json');
const config = JSON.parse(json);

return config;
}

async function getAssets() {
return fixture.config.build.assets;
}

async function checkValidCacheControl(assets) {
const config = await getConfig();

const route = config.routes.find((r) => r.src === `^/${assets ?? getAssets()}/(.*)$`);
expect(route.headers['cache-control']).to.equal(VALID_CACHE_CONTROL);
expect(route.continue).to.equal(true);
}

describe('static adapter', () => {
const adapter = import('@astrojs/vercel/static');

it('has cache control', async () => {
await build({ adapter });
checkValidCacheControl();
});

it('has cache control other assets', async () => {
const assets = '_foo';
await build({ adapter, assets });
checkValidCacheControl(assets);
});
});

describe('serverless adapter', () => {
const adapter = import('@astrojs/vercel/serverless');

it('has cache control', async () => {
await build({ adapter });
checkValidCacheControl();
});

it('has cache control other assets', async () => {
const assets = '_foo';
await build({ adapter, assets });
checkValidCacheControl(assets);
});
});

describe('edge adapter', () => {
const adapter = import('@astrojs/vercel/edge');

it('has cache control', async () => {
await build({ adapter });
checkValidCacheControl();
});

it('has cache control other assets', async () => {
const assets = '_foo';
await build({ adapter, assets });
checkValidCacheControl(assets);
});
});
});
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 373665d

Please sign in to comment.