From 57f3edf13a76504a384ee7fac1efb840d8e91851 Mon Sep 17 00:00:00 2001
From: Kid <44045911+kidonng@users.noreply.github.com>
Date: Mon, 19 Jun 2023 01:05:50 +0800
Subject: [PATCH 1/7] feat: cache assets in Vercel adapter
---
.changeset/tidy-tips-doubt.md | 5 ++
.../integrations/vercel/src/edge/adapter.ts | 5 ++
.../vercel/src/serverless/adapter.ts | 11 ++-
.../integrations/vercel/src/static/adapter.ts | 10 ++-
.../fixtures/static-assets/astro.config.mjs | 4 +
.../test/fixtures/static-assets/package.json | 9 ++
.../static-assets/src/pages/index.astro | 8 ++
.../vercel/test/static-assets.test.js | 84 +++++++++++++++++++
8 files changed, 134 insertions(+), 2 deletions(-)
create mode 100644 .changeset/tidy-tips-doubt.md
create mode 100644 packages/integrations/vercel/test/fixtures/static-assets/astro.config.mjs
create mode 100644 packages/integrations/vercel/test/fixtures/static-assets/package.json
create mode 100644 packages/integrations/vercel/test/fixtures/static-assets/src/pages/index.astro
create mode 100644 packages/integrations/vercel/test/static-assets.test.js
diff --git a/.changeset/tidy-tips-doubt.md b/.changeset/tidy-tips-doubt.md
new file mode 100644
index 000000000000..8d802b9a27e3
--- /dev/null
+++ b/.changeset/tidy-tips-doubt.md
@@ -0,0 +1,5 @@
+---
+'@astrojs/vercel': major
+---
+
+Add cache headers to assets in Vercel adapter
diff --git a/packages/integrations/vercel/src/edge/adapter.ts b/packages/integrations/vercel/src/edge/adapter.ts
index 5af00dfce004..b83c9f2b729e 100644
--- a/packages/integrations/vercel/src/edge/adapter.ts
+++ b/packages/integrations/vercel/src/edge/adapter.ts
@@ -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' },
],
diff --git a/packages/integrations/vercel/src/serverless/adapter.ts b/packages/integrations/vercel/src/serverless/adapter.ts
index 2d12db5ad6ca..1a2f9d82a1bd 100644
--- a/packages/integrations/vercel/src/serverless/adapter.ts
+++ b/packages/integrations/vercel/src/serverless/adapter.ts
@@ -187,7 +187,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 }
: {}),
diff --git a/packages/integrations/vercel/src/static/adapter.ts b/packages/integrations/vercel/src/static/adapter.ts
index f710356aa7af..bc83b24afcf1 100644
--- a/packages/integrations/vercel/src/static/adapter.ts
+++ b/packages/integrations/vercel/src/static/adapter.ts
@@ -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 }
: {}),
diff --git a/packages/integrations/vercel/test/fixtures/static-assets/astro.config.mjs b/packages/integrations/vercel/test/fixtures/static-assets/astro.config.mjs
new file mode 100644
index 000000000000..20b0b8e2ba7f
--- /dev/null
+++ b/packages/integrations/vercel/test/fixtures/static-assets/astro.config.mjs
@@ -0,0 +1,4 @@
+import { defineConfig } from 'astro/config';
+
+export default defineConfig({
+});
diff --git a/packages/integrations/vercel/test/fixtures/static-assets/package.json b/packages/integrations/vercel/test/fixtures/static-assets/package.json
new file mode 100644
index 000000000000..e1b608a856fd
--- /dev/null
+++ b/packages/integrations/vercel/test/fixtures/static-assets/package.json
@@ -0,0 +1,9 @@
+{
+ "name": "@test/astro-vercel-static-assets",
+ "version": "0.0.0",
+ "private": true,
+ "dependencies": {
+ "@astrojs/vercel": "workspace:*",
+ "astro": "workspace:*"
+ }
+}
diff --git a/packages/integrations/vercel/test/fixtures/static-assets/src/pages/index.astro b/packages/integrations/vercel/test/fixtures/static-assets/src/pages/index.astro
new file mode 100644
index 000000000000..9c077e2a381b
--- /dev/null
+++ b/packages/integrations/vercel/test/fixtures/static-assets/src/pages/index.astro
@@ -0,0 +1,8 @@
+
+
+ Testing
+
+
+ Testing
+
+
diff --git a/packages/integrations/vercel/test/static-assets.test.js b/packages/integrations/vercel/test/static-assets.test.js
new file mode 100644
index 000000000000..78ca1d6e56db
--- /dev/null
+++ b/packages/integrations/vercel/test/static-assets.test.js
@@ -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);
+ });
+ });
+});
From d0209cf8a1d4dcb395657065fd0a5fecde3de03e Mon Sep 17 00:00:00 2001
From: Hee
Date: Thu, 20 Jul 2023 20:54:55 +0900
Subject: [PATCH 2/7] Update tidy-tips-doubt.md
---
.changeset/tidy-tips-doubt.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/.changeset/tidy-tips-doubt.md b/.changeset/tidy-tips-doubt.md
index 8d802b9a27e3..e0a38c69e858 100644
--- a/.changeset/tidy-tips-doubt.md
+++ b/.changeset/tidy-tips-doubt.md
@@ -1,5 +1,5 @@
---
-'@astrojs/vercel': major
+'@astrojs/vercel': minor
---
Add cache headers to assets in Vercel adapter
From b877fdf4e0a7673d19515efc6f24044d7373bb83 Mon Sep 17 00:00:00 2001
From: Nate Moore
Date: Tue, 1 Aug 2023 16:24:29 -0500
Subject: [PATCH 3/7] chore: update lockfile
---
pnpm-lock.yaml | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 01d7ffa3ba12..82c130ed93b9 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -5047,6 +5047,15 @@ importers:
specifier: workspace:*
version: link:../../../../../astro
+ packages/integrations/vercel/test/fixtures/static-assets:
+ dependencies:
+ '@astrojs/vercel':
+ specifier: workspace:*
+ version: link:../../..
+ astro:
+ specifier: workspace:*
+ version: link:../../../../../astro
+
packages/integrations/vercel/test/hosted/hosted-astro-project:
dependencies:
'@astrojs/vercel':
From df413a15089c813fc91f56eb89f5f588fdfbe5e4 Mon Sep 17 00:00:00 2001
From: Nate Moore
Date: Tue, 1 Aug 2023 17:06:24 -0500
Subject: [PATCH 4/7] Update
packages/integrations/vercel/test/static-assets.test.js
---
packages/integrations/vercel/test/static-assets.test.js | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/packages/integrations/vercel/test/static-assets.test.js b/packages/integrations/vercel/test/static-assets.test.js
index 78ca1d6e56db..2cdc1b225d68 100644
--- a/packages/integrations/vercel/test/static-assets.test.js
+++ b/packages/integrations/vercel/test/static-assets.test.js
@@ -37,8 +37,8 @@ describe('Static Assets', () => {
expect(route.continue).to.equal(true);
}
- describe('static adapter', () => {
- const adapter = import('@astrojs/vercel/static');
+ describe('static adapter', async () => {
+ const adapter = await import('@astrojs/vercel/static');
it('has cache control', async () => {
await build({ adapter });
From fdcd6af297cdcfe374fe596ca6fd40ec4b11820d Mon Sep 17 00:00:00 2001
From: Nate Moore
Date: Tue, 1 Aug 2023 17:06:29 -0500
Subject: [PATCH 5/7] Update
packages/integrations/vercel/test/static-assets.test.js
---
packages/integrations/vercel/test/static-assets.test.js | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/packages/integrations/vercel/test/static-assets.test.js b/packages/integrations/vercel/test/static-assets.test.js
index 2cdc1b225d68..5312aabcbd37 100644
--- a/packages/integrations/vercel/test/static-assets.test.js
+++ b/packages/integrations/vercel/test/static-assets.test.js
@@ -52,8 +52,8 @@ describe('Static Assets', () => {
});
});
- describe('serverless adapter', () => {
- const adapter = import('@astrojs/vercel/serverless');
+ describe('serverless adapter', async () => {
+ const adapter = await import('@astrojs/vercel/serverless');
it('has cache control', async () => {
await build({ adapter });
From 9099cc2ef4bee43f6a7b75469c6f62f22b6ffec6 Mon Sep 17 00:00:00 2001
From: Nate Moore
Date: Tue, 1 Aug 2023 17:06:33 -0500
Subject: [PATCH 6/7] Update
packages/integrations/vercel/test/static-assets.test.js
---
packages/integrations/vercel/test/static-assets.test.js | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/packages/integrations/vercel/test/static-assets.test.js b/packages/integrations/vercel/test/static-assets.test.js
index 5312aabcbd37..b2bb4542fd6e 100644
--- a/packages/integrations/vercel/test/static-assets.test.js
+++ b/packages/integrations/vercel/test/static-assets.test.js
@@ -67,8 +67,8 @@ describe('Static Assets', () => {
});
});
- describe('edge adapter', () => {
- const adapter = import('@astrojs/vercel/edge');
+ describe('edge adapter', async () => {
+ const adapter = await import('@astrojs/vercel/edge');
it('has cache control', async () => {
await build({ adapter });
From de770f6600b835cab2b8157b10a53c9d15d04f66 Mon Sep 17 00:00:00 2001
From: Nate Moore
Date: Tue, 1 Aug 2023 17:07:36 -0500
Subject: [PATCH 7/7] chore: update split test
---
packages/integrations/vercel/test/split.test.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/packages/integrations/vercel/test/split.test.js b/packages/integrations/vercel/test/split.test.js
index 4f3f3904edca..9044954f28f5 100644
--- a/packages/integrations/vercel/test/split.test.js
+++ b/packages/integrations/vercel/test/split.test.js
@@ -24,6 +24,6 @@ describe('build: split', () => {
it('creates the route definitions in the config.json', async () => {
const json = await fixture.readFile('../.vercel/output/config.json');
const config = JSON.parse(json);
- expect(config.routes).to.have.a.lengthOf(3);
+ expect(config.routes).to.have.a.lengthOf(4);
});
});