-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add cache headers to assets in Vercel adapter (#7729)
* feat: cache assets in Vercel adapter * Update tidy-tips-doubt.md * chore: update lockfile * Update packages/integrations/vercel/test/static-assets.test.js * Update packages/integrations/vercel/test/static-assets.test.js * Update packages/integrations/vercel/test/static-assets.test.js * chore: update split test --------- Co-authored-by: Kid <44045911+kidonng@users.noreply.github.com> Co-authored-by: Nate Moore <nate@astro.build> Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
- Loading branch information
1 parent
4dd6c79
commit 560d0da
Showing
10 changed files
with
144 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@astrojs/vercel': minor | ||
--- | ||
|
||
Add cache headers to assets in Vercel adapter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
packages/integrations/vercel/test/fixtures/static-assets/astro.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { defineConfig } from 'astro/config'; | ||
|
||
export default defineConfig({ | ||
}); |
9 changes: 9 additions & 0 deletions
9
packages/integrations/vercel/test/fixtures/static-assets/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:*" | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/integrations/vercel/test/fixtures/static-assets/src/pages/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', async () => { | ||
const adapter = await 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', async () => { | ||
const adapter = await 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', async () => { | ||
const adapter = await 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); | ||
}); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.