Skip to content

Commit

Permalink
feat: Add option to set Cloudfront managed cache policy (#10)
Browse files Browse the repository at this point in the history
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'.
  • Loading branch information
H0R5E authored Sep 9, 2024
1 parent 3be81b9 commit 792ac06
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ features are as follows:
export interface AWSAdapterProps {
artifactPath?: string // Build output directory (default: build)
autoDeploy?: boolean // Should automatically deploy in SvelteKit build step (default: false)
cachePolicy?: string // Cloudfront managed cache policy (default: 'Managed-CachingOptimized')
defaultHeaders?: string[] // Default whitelist of headers for the SSR server. (default: ['Accept','Accept-Language','If-None-Match','Host','Origin','Referer','X-Forwarded-Host'])
extraHeaders?: string[] // Additional headers to add to whitelist. (default: [])
esbuildOptions?: any // Override or extend default esbuild options for the SSR server. Supports `external` (default `['node:*']`), `format` (default `cjs`), `target` (default `node18`), `banner` (default `{}`).
extraHeaders?: string[] // Additional headers to add to whitelist. (default: [])
FQDN?: string // Full qualified domain name of CloudFront deployment (e.g. demo.example.com)
memorySize?: number // Memory size of SSR lambda in MB (default: 128)
pulumiPaths: string[] // For internal use only
region?: string // Region to deploy resources (default: us-east-2)
serverStreaming?: boolean // Use lambda streaming responses for SSR server (default: false)
stackName?: string // Pulumi stack name (default: dev)
Expand Down Expand Up @@ -118,7 +118,7 @@ in the SvelteKit documentation for further details.
A script is provided to destroy the infrastructure, with the following
signature:

```
```console
adapter-stack-destroy [artifactPath]

Destroy the SvelteKit adapter's Pulumi stacks
Expand Down
21 changes: 12 additions & 9 deletions adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,22 @@ const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
export interface AWSAdapterProps {
artifactPath?: string
autoDeploy?: boolean
cachePolicy?: string
defaultHeaders?: string[]
extraHeaders?: string[]
esbuildOptions?: any
extraHeaders?: string[]
FQDN?: string
pulumiPaths?: string[]
memorySize?: number
pulumiPaths?: string[]
region?: string
serverStreaming?: boolean
stackName?: string
stackName: string
}

export function adapter({
artifactPath = 'build',
autoDeploy = false,
cachePolicy = 'Managed-CachingOptimized',
defaultHeaders = [
'Accept',
'Accept-Language',
Expand All @@ -44,11 +46,10 @@ export function adapter({
esbuildOptions = {},
FQDN,
memorySize = 128,
pulumiPaths = [],
region = 'us-east-2',
serverStreaming = false,
stackName = 'dev',
}: AWSAdapterProps = {}) {
}: AWSAdapterProps) {
/** @type {import('@sveltejs/kit').Adapter} */
return {
name: 'adapter-aws-pulumi',
Expand All @@ -64,8 +65,6 @@ export function adapter({
const options_directory = await buildOptions(builder, artifactPath)

if (autoDeploy) {
let adapterProps: AWSAdapterProps = {}

builder.log.minor('Deploy using Pulumi.')

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

await serverStack.setAllConfig({
'aws:region': { value: region },
cachePolicy: { value: cachePolicy },
projectPath: { value: process.cwd() },
serverPath: { value: server_directory },
optionsPath: { value: options_directory },
Expand Down Expand Up @@ -158,8 +158,11 @@ export function adapter({
onOutput: console.info,
})

adapterProps.pulumiPaths = [serverPath, mainPath]
adapterProps.stackName = stackName
const adapterProps: AWSAdapterProps = {
pulumiPaths: [serverPath, mainPath],
stackName: stackName,
}

writeFileSync(
join(artifactPath, '.adapterprops.json'),
JSON.stringify(adapterProps),
Expand Down
8 changes: 5 additions & 3 deletions stacks/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import {
} from './resources.js'

const pulumiConfig = new pulumi.Config()
const cachePolicy = pulumiConfig.require('cachePolicy')
const edgePath = pulumiConfig.require('edgePath')
const staticPath = pulumiConfig.require('staticPath')
const FQDN = pulumiConfig.get('FQDN')
const optionsArn = pulumiConfig.require('optionsArn')
const prerenderedPath = pulumiConfig.require('prerenderedPath')
const serverArn = pulumiConfig.require('serverArn')
const optionsArn = pulumiConfig.require('optionsArn')
const FQDN = pulumiConfig.get('FQDN')
const serverHeadersStr = pulumiConfig.get('serverHeaders')
const staticPath = pulumiConfig.require('staticPath')

let serverHeaders: string[] = []

Expand All @@ -43,6 +44,7 @@ const distribution = buildCDN(
serverHeaders,
FQDN,
certificateArn,
cachePolicy,
)

if (FQDN) {
Expand Down
3 changes: 2 additions & 1 deletion stacks/main/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ export function buildCDN(
serverHeaders: string[],
FQDN?: string,
certificateArn?: pulumi.Input<string>,
cachePolicy: string = 'Managed-CachingOptimized',
): aws.cloudfront.Distribution {
const defaultRequestPolicy = new aws.cloudfront.OriginRequestPolicy(
registerName('DefaultRequestPolicy'),
Expand Down Expand Up @@ -244,7 +245,7 @@ export function buildCDN(
)

const optimizedCachePolicy = aws.cloudfront.getCachePolicyOutput({
name: 'Managed-CachingOptimized',
name: cachePolicy,
})

const distribution = new aws.cloudfront.Distribution(
Expand Down
2 changes: 0 additions & 2 deletions tests/stacks.main.resources.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@ describe('stacks/main/resources.ts', () => {
role: 'mock',
})
const bucket = new aws.s3.Bucket('MockBucket')
const routes = ['mock/*', 'another/*']
const serverHeaders = ['mock1', 'mock2']
const staticHeaders = ['mock3']
const FQDN = 'server.example.com'
const certificateArn = 'MockCertificateArn'
const bucketId = await promiseOf(bucket.id)
Expand Down

0 comments on commit 792ac06

Please sign in to comment.