Skip to content

Commit

Permalink
Merge pull request #10 from lalalilo/fix-cache-invalidation
Browse files Browse the repository at this point in the history
Fix cache invalidation
  • Loading branch information
nicgirault authored Mar 12, 2020
2 parents 72f46cd + 4588cd1 commit 7e030e6
Show file tree
Hide file tree
Showing 7 changed files with 3,513 additions and 49 deletions.
10 changes: 9 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 2
jobs:
build:
docker:
- image: circleci/node:10.15
- image: circleci/node:12.16

working_directory: ~/aws-spa

Expand Down Expand Up @@ -35,3 +35,11 @@ jobs:
- run:
name: Code coverage
command: npx codecov

- run:
name: Build
command: yarn build

- run:
name: Release
command: npx semantic-release
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12.16
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"scripts": {
"build": "babel src -d lib --extensions '.ts'",
"prebuild": "yarn check-types && yarn test",
"prepublish": "yarn build",
"run-cli": "babel-node --extensions '.ts' src/cli.ts",
"check-types": "tsc",
"test": "jest"
Expand All @@ -49,6 +48,12 @@
"pre-commit": "yarn check-types"
}
},
"release": {
"branches": [
"master",
"next"
]
},
"dependencies": {
"adm-zip": "^0.4.13",
"aws-sdk": "^2.569.0",
Expand Down Expand Up @@ -76,6 +81,7 @@
"husky": "^3.0.9",
"jest": "^24.9.0",
"rimraf": "^3.0.0",
"semantic-release": "^17.0.4",
"typescript": "^3.7.2"
}
}
21 changes: 20 additions & 1 deletion src/cloudfront.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
invalidateCloudfrontCache,
identifyingTag,
createCloudFrontDistribution,
setSimpleAuthBehavior
setSimpleAuthBehavior,
getCacheInvalidations
} from "./cloudfront";
import { lambdaPrefix } from "./lambda";

Expand Down Expand Up @@ -298,4 +299,22 @@ describe("cloudfront", () => {
]);
});
});

describe("getCacheInvalidations", () => {
it.each([
{ input: "index.html", expectedOutput: "/index.html" },
{
input: "index.html, hello.html",
subFolder: undefined,
expectedOutput: "/index.html,/hello.html"
},
{
input: "index.html",
subFolder: "some-branch",
expectedOutput: "/some-branch/index.html"
}
])("add missing slash", ({ input, subFolder, expectedOutput }) => {
expect(getCacheInvalidations(input, subFolder)).toEqual(expectedOutput);
});
});
});
10 changes: 10 additions & 0 deletions src/cloudfront.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,16 @@ export const setSimpleAuthBehavior = async (
);
};

export const getCacheInvalidations = (
cacheInvalidations: string,
subFolder: string | undefined
) =>
cacheInvalidations
.split(",")
.map(string => string.trim().replace(/$\//, ""))
.map(string => (subFolder ? `/${subFolder}/${string}` : `/${string}`))
.join(",");

const updateLambdaFunctionAssociations = async (
distributionId: string,
DistributionConfig: DistributionConfig,
Expand Down
9 changes: 7 additions & 2 deletions src/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
createCloudFrontDistribution,
invalidateCloudfrontCache,
DistributionIdentificationDetail,
setSimpleAuthBehavior
setSimpleAuthBehavior,
getCacheInvalidations
} from "./cloudfront";
import {
findHostedZone,
Expand Down Expand Up @@ -101,5 +102,9 @@ export const deploy = async (
}

await syncToS3(folder, domainName, cacheBustedPrefix, s3Folder);
await invalidateCloudfrontCache(distribution.Id, cacheInvalidations, wait);
await invalidateCloudfrontCache(
distribution.Id,
getCacheInvalidations(cacheInvalidations, s3Folder),
wait
);
};
Loading

0 comments on commit 7e030e6

Please sign in to comment.