Skip to content

Commit 792ac06

Browse files
authored
feat: Add option to set Cloudfront managed cache policy (#10)
The cachePolicy argument can now be used to customize the cloudfront cache policy using one of the managed policies. It defaults to the previous fixed value of 'Managed-CachingOptimized'.
1 parent 3be81b9 commit 792ac06

File tree

5 files changed

+22
-18
lines changed

5 files changed

+22
-18
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ features are as follows:
8484
export interface AWSAdapterProps {
8585
artifactPath?: string // Build output directory (default: build)
8686
autoDeploy?: boolean // Should automatically deploy in SvelteKit build step (default: false)
87+
cachePolicy?: string // Cloudfront managed cache policy (default: 'Managed-CachingOptimized')
8788
defaultHeaders?: string[] // Default whitelist of headers for the SSR server. (default: ['Accept','Accept-Language','If-None-Match','Host','Origin','Referer','X-Forwarded-Host'])
88-
extraHeaders?: string[] // Additional headers to add to whitelist. (default: [])
8989
esbuildOptions?: any // Override or extend default esbuild options for the SSR server. Supports `external` (default `['node:*']`), `format` (default `cjs`), `target` (default `node18`), `banner` (default `{}`).
90+
extraHeaders?: string[] // Additional headers to add to whitelist. (default: [])
9091
FQDN?: string // Full qualified domain name of CloudFront deployment (e.g. demo.example.com)
9192
memorySize?: number // Memory size of SSR lambda in MB (default: 128)
92-
pulumiPaths: string[] // For internal use only
9393
region?: string // Region to deploy resources (default: us-east-2)
9494
serverStreaming?: boolean // Use lambda streaming responses for SSR server (default: false)
9595
stackName?: string // Pulumi stack name (default: dev)
@@ -118,7 +118,7 @@ in the SvelteKit documentation for further details.
118118
A script is provided to destroy the infrastructure, with the following
119119
signature:
120120

121-
```
121+
```console
122122
adapter-stack-destroy [artifactPath]
123123

124124
Destroy the SvelteKit adapter's Pulumi stacks

adapter.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,22 @@ const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
1717
export interface AWSAdapterProps {
1818
artifactPath?: string
1919
autoDeploy?: boolean
20+
cachePolicy?: string
2021
defaultHeaders?: string[]
21-
extraHeaders?: string[]
2222
esbuildOptions?: any
23+
extraHeaders?: string[]
2324
FQDN?: string
24-
pulumiPaths?: string[]
2525
memorySize?: number
26+
pulumiPaths?: string[]
2627
region?: string
2728
serverStreaming?: boolean
28-
stackName?: string
29+
stackName: string
2930
}
3031

3132
export function adapter({
3233
artifactPath = 'build',
3334
autoDeploy = false,
35+
cachePolicy = 'Managed-CachingOptimized',
3436
defaultHeaders = [
3537
'Accept',
3638
'Accept-Language',
@@ -44,11 +46,10 @@ export function adapter({
4446
esbuildOptions = {},
4547
FQDN,
4648
memorySize = 128,
47-
pulumiPaths = [],
4849
region = 'us-east-2',
4950
serverStreaming = false,
5051
stackName = 'dev',
51-
}: AWSAdapterProps = {}) {
52+
}: AWSAdapterProps) {
5253
/** @type {import('@sveltejs/kit').Adapter} */
5354
return {
5455
name: 'adapter-aws-pulumi',
@@ -64,8 +65,6 @@ export function adapter({
6465
const options_directory = await buildOptions(builder, artifactPath)
6566

6667
if (autoDeploy) {
67-
let adapterProps: AWSAdapterProps = {}
68-
6968
builder.log.minor('Deploy using Pulumi.')
7069

7170
// Setup server stack.
@@ -78,6 +77,7 @@ export function adapter({
7877

7978
await serverStack.setAllConfig({
8079
'aws:region': { value: region },
80+
cachePolicy: { value: cachePolicy },
8181
projectPath: { value: process.cwd() },
8282
serverPath: { value: server_directory },
8383
optionsPath: { value: options_directory },
@@ -158,8 +158,11 @@ export function adapter({
158158
onOutput: console.info,
159159
})
160160

161-
adapterProps.pulumiPaths = [serverPath, mainPath]
162-
adapterProps.stackName = stackName
161+
const adapterProps: AWSAdapterProps = {
162+
pulumiPaths: [serverPath, mainPath],
163+
stackName: stackName,
164+
}
165+
163166
writeFileSync(
164167
join(artifactPath, '.adapterprops.json'),
165168
JSON.stringify(adapterProps),

stacks/main/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ import {
1111
} from './resources.js'
1212

1313
const pulumiConfig = new pulumi.Config()
14+
const cachePolicy = pulumiConfig.require('cachePolicy')
1415
const edgePath = pulumiConfig.require('edgePath')
15-
const staticPath = pulumiConfig.require('staticPath')
16+
const FQDN = pulumiConfig.get('FQDN')
17+
const optionsArn = pulumiConfig.require('optionsArn')
1618
const prerenderedPath = pulumiConfig.require('prerenderedPath')
1719
const serverArn = pulumiConfig.require('serverArn')
18-
const optionsArn = pulumiConfig.require('optionsArn')
19-
const FQDN = pulumiConfig.get('FQDN')
2020
const serverHeadersStr = pulumiConfig.get('serverHeaders')
21+
const staticPath = pulumiConfig.require('staticPath')
2122

2223
let serverHeaders: string[] = []
2324

@@ -43,6 +44,7 @@ const distribution = buildCDN(
4344
serverHeaders,
4445
FQDN,
4546
certificateArn,
47+
cachePolicy,
4648
)
4749

4850
if (FQDN) {

stacks/main/resources.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ export function buildCDN(
213213
serverHeaders: string[],
214214
FQDN?: string,
215215
certificateArn?: pulumi.Input<string>,
216+
cachePolicy: string = 'Managed-CachingOptimized',
216217
): aws.cloudfront.Distribution {
217218
const defaultRequestPolicy = new aws.cloudfront.OriginRequestPolicy(
218219
registerName('DefaultRequestPolicy'),
@@ -244,7 +245,7 @@ export function buildCDN(
244245
)
245246

246247
const optimizedCachePolicy = aws.cloudfront.getCachePolicyOutput({
247-
name: 'Managed-CachingOptimized',
248+
name: cachePolicy,
248249
})
249250

250251
const distribution = new aws.cloudfront.Distribution(

tests/stacks.main.resources.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,7 @@ describe('stacks/main/resources.ts', () => {
146146
role: 'mock',
147147
})
148148
const bucket = new aws.s3.Bucket('MockBucket')
149-
const routes = ['mock/*', 'another/*']
150149
const serverHeaders = ['mock1', 'mock2']
151-
const staticHeaders = ['mock3']
152150
const FQDN = 'server.example.com'
153151
const certificateArn = 'MockCertificateArn'
154152
const bucketId = await promiseOf(bucket.id)

0 commit comments

Comments
 (0)