@wolsok/spa-cdk-stack 1.2.33 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -1
- package/package.json +1 -1
- package/src/index.d.ts +1 -0
- package/src/index.js +3 -0
- package/src/index.js.map +1 -1
- package/src/lib/spa/spa.construct.d.ts +22 -0
- package/src/lib/spa/spa.construct.js +27 -4
- package/src/lib/spa/spa.construct.js.map +1 -1
- package/src/lib/spa/viewer-request.d.ts +26 -0
- package/src/lib/spa/viewer-request.js +72 -0
- package/src/lib/spa/viewer-request.js.map +1 -0
package/README.md
CHANGED
|
@@ -15,6 +15,7 @@ Features are:
|
|
|
15
15
|
regions.
|
|
16
16
|
- Redirect from HTTP to HTTPS with an ACM certificate.
|
|
17
17
|
- Route53 alias record creation,
|
|
18
|
+
- Optionally, a permanent redirect from `/path/` to `/path`.
|
|
18
19
|
|
|
19
20
|
## Usage
|
|
20
21
|
|
|
@@ -58,7 +59,8 @@ new SpaStack(app, 'MyApp', {
|
|
|
58
59
|
Each `addExtraAssets` call is one deployment, which unpacks all its sources into
|
|
59
60
|
one folder. So a file and a path below it, such as a page uploaded under its bare
|
|
60
61
|
path `blog` and another under `blog/first-post`, cannot share a call: the
|
|
61
|
-
construct throws at synth
|
|
62
|
+
construct throws at synth (prerendered pages need no such uploads with
|
|
63
|
+
`resolveFolderIndex`, below). Add the deeper paths in a call of their own:
|
|
62
64
|
|
|
63
65
|
```ts
|
|
64
66
|
stack.spa.addExtraAssets(
|
|
@@ -71,6 +73,38 @@ stack.spa.addExtraAssets(
|
|
|
71
73
|
);
|
|
72
74
|
```
|
|
73
75
|
|
|
76
|
+
CloudFront in front of S3 resolves no folder index, so a prerendered page at
|
|
77
|
+
`about/index.html` would answer `/about` with the `index.html` fallback. Set
|
|
78
|
+
`resolveFolderIndex: true` and a viewer-request CloudFront Function serves an
|
|
79
|
+
extension-less path (no `.` in its last segment) from its `index.html`, while
|
|
80
|
+
the browser keeps `/about`; the page needs no second upload under its bare path.
|
|
81
|
+
Add `redirectTrailingSlash: true` to redirect `/about/` to `/about`, so each
|
|
82
|
+
page keeps one URL:
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
new SpaStack(app, 'Site', {
|
|
86
|
+
// ...
|
|
87
|
+
resolveFolderIndex: true,
|
|
88
|
+
redirectTrailingSlash: true,
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Both options share one function, as a cache behaviour takes one per event; see
|
|
93
|
+
their doc comments.
|
|
94
|
+
|
|
95
|
+
A page whose folder name holds a `.` (`blog/v1.2/index.html`) is not resolved,
|
|
96
|
+
as `/blog/v1.2` looks like a file. `servesFolderIndex(path)` is the deployed
|
|
97
|
+
function's rule in TypeScript, so a stack can refuse such pages at synth:
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
import { servesFolderIndex } from '@wolsok/spa-cdk-stack';
|
|
101
|
+
|
|
102
|
+
const unreachable = pages.filter((page) => !servesFolderIndex(`/${page}`));
|
|
103
|
+
if (unreachable.length > 0) {
|
|
104
|
+
throw new Error(`Not served from their index.html: ${unreachable}`);
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
74
108
|
## Building
|
|
75
109
|
|
|
76
110
|
## Running unit tests
|
package/package.json
CHANGED
package/src/index.d.ts
CHANGED
package/src/index.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.servesFolderIndex = void 0;
|
|
3
4
|
const tslib_1 = require("tslib");
|
|
4
5
|
tslib_1.__exportStar(require("./lib/spa/spa.construct"), exports);
|
|
5
6
|
tslib_1.__exportStar(require("./lib/spa/spa.stack"), exports);
|
|
7
|
+
var viewer_request_1 = require("./lib/spa/viewer-request");
|
|
8
|
+
Object.defineProperty(exports, "servesFolderIndex", { enumerable: true, get: function () { return viewer_request_1.servesFolderIndex; } });
|
|
6
9
|
tslib_1.__exportStar(require("./lib/utils/envOrDie"), exports);
|
|
7
10
|
//# sourceMappingURL=index.js.map
|
package/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../libs/public/spa-cdk-stack/src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../libs/public/spa-cdk-stack/src/index.ts"],"names":[],"mappings":";;;;AAAA,kEAAwC;AACxC,8DAAoC;AACpC,2DAA6D;AAApD,mHAAA,iBAAiB,OAAA;AAC1B,+DAAqC"}
|
|
@@ -29,6 +29,26 @@ export interface SpaProps {
|
|
|
29
29
|
* the later header wins. Wildcards may only appear in the last segment.
|
|
30
30
|
*/
|
|
31
31
|
cacheOverrides?: CacheOverride[];
|
|
32
|
+
/**
|
|
33
|
+
* Answer a path ending in `/` (other than `/`) with a permanent redirect to
|
|
34
|
+
* the path without it, query string kept, so `/about/` goes to `/about`
|
|
35
|
+
* instead of falling back to index.html. Pair it with resolveFolderIndex
|
|
36
|
+
* for prerendered pages, so the target resolves. Takes the default
|
|
37
|
+
* behaviour's viewer-request slot, which it shares with resolveFolderIndex.
|
|
38
|
+
* @default false
|
|
39
|
+
*/
|
|
40
|
+
redirectTrailingSlash?: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Serve an extension-less path (no `.` in its last segment) from its folder
|
|
43
|
+
* index, `/about` from `about/index.html`, as CloudFront with an S3 origin
|
|
44
|
+
* resolves none itself; the browser keeps the URL. A path ending in `/` is
|
|
45
|
+
* served from `index.html` below it, unless redirectTrailingSlash redirects
|
|
46
|
+
* it first. For prerendered sites, whose pages then need no upload under
|
|
47
|
+
* their bare path. Takes the default behaviour's viewer-request slot, which
|
|
48
|
+
* it shares with redirectTrailingSlash.
|
|
49
|
+
* @default false
|
|
50
|
+
*/
|
|
51
|
+
resolveFolderIndex?: boolean;
|
|
32
52
|
}
|
|
33
53
|
export interface CacheOverride {
|
|
34
54
|
/** Globs relative to buildOutputPath, e.g. `assets/manifest*.json`. */
|
|
@@ -75,6 +95,8 @@ export declare class SpaConstruct extends Construct {
|
|
|
75
95
|
private createBucket;
|
|
76
96
|
private createCertificate;
|
|
77
97
|
private createDistribution;
|
|
98
|
+
/** The viewer-request function both options share, or none without them. */
|
|
99
|
+
private viewerRequestAssociations;
|
|
78
100
|
private createARecord;
|
|
79
101
|
private createBucketDeployment;
|
|
80
102
|
private extractDomains;
|
|
@@ -12,6 +12,7 @@ const aws_s3_1 = require("aws-cdk-lib/aws-s3");
|
|
|
12
12
|
const aws_s3_deployment_1 = require("aws-cdk-lib/aws-s3-deployment");
|
|
13
13
|
const constructs_1 = require("constructs");
|
|
14
14
|
const deployment_sources_1 = require("./deployment-sources");
|
|
15
|
+
const viewer_request_1 = require("./viewer-request");
|
|
15
16
|
/**
|
|
16
17
|
* Exclude globs that keep only `files`. '**' also skips every folder as a
|
|
17
18
|
* whole, so the folders above a nested file are re-included first, and
|
|
@@ -54,7 +55,7 @@ class SpaConstruct extends constructs_1.Construct {
|
|
|
54
55
|
this.spaName = spaName;
|
|
55
56
|
this.bucketDeployments = [];
|
|
56
57
|
const removalPolicy = props.bucketRemovalPolicy ?? defaultBucketRemovalPolicy;
|
|
57
|
-
const { buildOutputPath,
|
|
58
|
+
const { buildOutputPath, noCacheFiles = [], cacheOverrides = [] } = props;
|
|
58
59
|
const overrides = [
|
|
59
60
|
...(noCacheFiles.length > 0
|
|
60
61
|
? [{ files: noCacheFiles, cacheControl: aws_s3_deployment_1.CacheControl.noCache() }]
|
|
@@ -70,7 +71,7 @@ class SpaConstruct extends constructs_1.Construct {
|
|
|
70
71
|
const certificate = props.certificate ??
|
|
71
72
|
this.createCertificate(zone, ...certificateDomainNames(siteDomain));
|
|
72
73
|
this.bucket = this.createBucket(siteDomain, removalPolicy);
|
|
73
|
-
this.distribution = this.createDistribution(siteDomain, certificate, this.bucket,
|
|
74
|
+
this.distribution = this.createDistribution(siteDomain, certificate, this.bucket, props);
|
|
74
75
|
this.createARecord(siteDomain, this.distribution, zone);
|
|
75
76
|
// No deployment prunes: a pruning sync of index.html alone would delete
|
|
76
77
|
// every other file, and an upload whose asset did not change is not run
|
|
@@ -157,12 +158,12 @@ class SpaConstruct extends constructs_1.Construct {
|
|
|
157
158
|
});
|
|
158
159
|
return certificate;
|
|
159
160
|
}
|
|
160
|
-
createDistribution(siteDomain, certificate, siteBucket,
|
|
161
|
+
createDistribution(siteDomain, certificate, siteBucket, { extraAllowedOrigins = [], redirectTrailingSlash, resolveFolderIndex, }) {
|
|
161
162
|
const responseHeadersPolicy = new aws_cloudfront_1.ResponseHeadersPolicy(this, 'ResponseHeadersPolicy', {
|
|
162
163
|
corsBehavior: {
|
|
163
164
|
accessControlAllowCredentials: false,
|
|
164
165
|
accessControlAllowOrigins: [
|
|
165
|
-
...
|
|
166
|
+
...extraAllowedOrigins,
|
|
166
167
|
'https://' + siteDomain,
|
|
167
168
|
'http://localhost',
|
|
168
169
|
'http://127.0.0.1',
|
|
@@ -193,6 +194,10 @@ class SpaConstruct extends constructs_1.Construct {
|
|
|
193
194
|
compress: true,
|
|
194
195
|
allowedMethods: aws_cloudfront_1.AllowedMethods.ALLOW_GET_HEAD_OPTIONS,
|
|
195
196
|
viewerProtocolPolicy: aws_cloudfront_1.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
|
|
197
|
+
functionAssociations: this.viewerRequestAssociations({
|
|
198
|
+
redirectTrailingSlash,
|
|
199
|
+
resolveFolderIndex,
|
|
200
|
+
}),
|
|
196
201
|
},
|
|
197
202
|
});
|
|
198
203
|
new aws_cdk_lib_1.CfnOutput(this, `${this.spaName}-DistributionId`, {
|
|
@@ -200,6 +205,24 @@ class SpaConstruct extends constructs_1.Construct {
|
|
|
200
205
|
});
|
|
201
206
|
return distribution;
|
|
202
207
|
}
|
|
208
|
+
/** The viewer-request function both options share, or none without them. */
|
|
209
|
+
viewerRequestAssociations(options) {
|
|
210
|
+
const tasks = [
|
|
211
|
+
options.redirectTrailingSlash && 'redirects /path/ to /path',
|
|
212
|
+
options.resolveFolderIndex && 'serves /path from /path/index.html',
|
|
213
|
+
].filter(Boolean);
|
|
214
|
+
if (tasks.length === 0) {
|
|
215
|
+
return undefined;
|
|
216
|
+
}
|
|
217
|
+
const viewerRequest = new aws_cloudfront_1.Function(this, 'ViewerRequest', {
|
|
218
|
+
code: aws_cloudfront_1.FunctionCode.fromInline((0, viewer_request_1.viewerRequestSource)(options)),
|
|
219
|
+
runtime: aws_cloudfront_1.FunctionRuntime.JS_2_0,
|
|
220
|
+
comment: `Viewer request: ${tasks.join(', ')}`,
|
|
221
|
+
});
|
|
222
|
+
return [
|
|
223
|
+
{ eventType: aws_cloudfront_1.FunctionEventType.VIEWER_REQUEST, function: viewerRequest },
|
|
224
|
+
];
|
|
225
|
+
}
|
|
203
226
|
createARecord(siteDomain, distribution, zone) {
|
|
204
227
|
// Route53 alias record for the CloudFront distribution
|
|
205
228
|
new aws_route53_1.ARecord(this, 'SiteAliasRecord', {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"spa.construct.js","sourceRoot":"","sources":["../../../../../../../libs/public/spa-cdk-stack/src/lib/spa/spa.construct.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"spa.construct.js","sourceRoot":"","sources":["../../../../../../../libs/public/spa-cdk-stack/src/lib/spa/spa.construct.ts"],"names":[],"mappings":";;;AA+HA,wDAGC;AAlID,6CAAiE;AACjE,+EAI4C;AAC5C,+DAYoC;AACpC,+EAAoE;AACpE,yDAKiC;AACjC,yEAAmE;AACnE,+CAAwE;AACxE,qEAKuC;AACvC,2CAAuC;AACvC,6DAA0E;AAC1E,qDAA6E;AAgE7E;;;;GAIG;AACH,SAAS,WAAW,CAAC,KAAe;IAClC,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAU,CAAC;IACnC,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,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AC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAC7D,MAAM,iBAAiB,GACrB,UAAU,KAAK,UAAU,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,UAAU,EAAE,CAAC;QAE7D,oCAAoC;QACpC,IAAI,UAAU,KAAK,UAAU,EAAE,CAAC;YAC9B,OAAO,CAAC,IAAI,CACV,cAAc,UAAU,qEAAqE,UAAU,EAAE,CAC1G,CAAC;QACJ,CAAC;QACD,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,iBAAiB,EAAE,CAAC;IACvD,CAAC;CACF;AA9SD,oCA8SC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** What the viewer-request CloudFront Function of a SPA does. */
|
|
2
|
+
export interface ViewerRequestOptions {
|
|
3
|
+
/** See `SpaProps.redirectTrailingSlash`. */
|
|
4
|
+
redirectTrailingSlash?: boolean;
|
|
5
|
+
/** See `SpaProps.resolveFolderIndex`. */
|
|
6
|
+
resolveFolderIndex?: boolean;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Whether `resolveFolderIndex` serves `path` from its `index.html`: no `.` in
|
|
10
|
+
* its last segment. The deployed function holds the same rule in ES5, and the
|
|
11
|
+
* spec checks that both agree.
|
|
12
|
+
*/
|
|
13
|
+
export declare function servesFolderIndex(path: string): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* The source of the CloudFront Function behind `SpaProps.redirectTrailingSlash`
|
|
16
|
+
* and `SpaProps.resolveFolderIndex`, for runtime `cloudfront-js-2.0` (plain
|
|
17
|
+
* ES5). A cache behaviour takes one function per event, so both options share
|
|
18
|
+
* it, each as a literal switch at its top. Kept as source text, so the spec
|
|
19
|
+
* runs exactly the code that is deployed.
|
|
20
|
+
*
|
|
21
|
+
* A path ending in `/` (other than `/`) is redirected to the path without it
|
|
22
|
+
* or, without that option, served from its `index.html`; an extension-less
|
|
23
|
+
* path (no `.` in its last segment) is served from `<path>/index.html`. The
|
|
24
|
+
* redirect keeps a single leading slash, so it never leaves the site.
|
|
25
|
+
*/
|
|
26
|
+
export declare function viewerRequestSource({ redirectTrailingSlash, resolveFolderIndex, }: ViewerRequestOptions): string;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.servesFolderIndex = servesFolderIndex;
|
|
4
|
+
exports.viewerRequestSource = viewerRequestSource;
|
|
5
|
+
/**
|
|
6
|
+
* Whether `resolveFolderIndex` serves `path` from its `index.html`: no `.` in
|
|
7
|
+
* its last segment. The deployed function holds the same rule in ES5, and the
|
|
8
|
+
* spec checks that both agree.
|
|
9
|
+
*/
|
|
10
|
+
function servesFolderIndex(path) {
|
|
11
|
+
return path.indexOf('.', path.lastIndexOf('/')) < 0;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The source of the CloudFront Function behind `SpaProps.redirectTrailingSlash`
|
|
15
|
+
* and `SpaProps.resolveFolderIndex`, for runtime `cloudfront-js-2.0` (plain
|
|
16
|
+
* ES5). A cache behaviour takes one function per event, so both options share
|
|
17
|
+
* it, each as a literal switch at its top. Kept as source text, so the spec
|
|
18
|
+
* runs exactly the code that is deployed.
|
|
19
|
+
*
|
|
20
|
+
* A path ending in `/` (other than `/`) is redirected to the path without it
|
|
21
|
+
* or, without that option, served from its `index.html`; an extension-less
|
|
22
|
+
* path (no `.` in its last segment) is served from `<path>/index.html`. The
|
|
23
|
+
* redirect keeps a single leading slash, so it never leaves the site.
|
|
24
|
+
*/
|
|
25
|
+
function viewerRequestSource({ redirectTrailingSlash = false, resolveFolderIndex = false, }) {
|
|
26
|
+
return `
|
|
27
|
+
var REDIRECT_TRAILING_SLASH = ${redirectTrailingSlash};
|
|
28
|
+
var RESOLVE_FOLDER_INDEX = ${resolveFolderIndex};
|
|
29
|
+
|
|
30
|
+
function handler(event) {
|
|
31
|
+
var request = event.request;
|
|
32
|
+
var uri = request.uri;
|
|
33
|
+
if (uri.length < 2) {
|
|
34
|
+
return request;
|
|
35
|
+
}
|
|
36
|
+
if (uri.charAt(uri.length - 1) === '/') {
|
|
37
|
+
if (REDIRECT_TRAILING_SLASH) {
|
|
38
|
+
return redirectToBarePath(request);
|
|
39
|
+
}
|
|
40
|
+
if (RESOLVE_FOLDER_INDEX) {
|
|
41
|
+
request.uri = uri + 'index.html';
|
|
42
|
+
}
|
|
43
|
+
return request;
|
|
44
|
+
}
|
|
45
|
+
if (RESOLVE_FOLDER_INDEX && uri.indexOf('.', uri.lastIndexOf('/')) < 0) {
|
|
46
|
+
request.uri = uri + '/index.html';
|
|
47
|
+
}
|
|
48
|
+
return request;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function redirectToBarePath(request) {
|
|
52
|
+
// One leading slash only: '//evil.com' would leave the site.
|
|
53
|
+
var path = '/' + request.uri.replace(/^[\\/\\\\]+/, '').replace(/\\/+$/, '');
|
|
54
|
+
var query = [];
|
|
55
|
+
Object.keys(request.querystring).forEach(function (key) {
|
|
56
|
+
var entry = request.querystring[key];
|
|
57
|
+
var values = entry.multiValue || [entry];
|
|
58
|
+
values.forEach(function (item) {
|
|
59
|
+
query.push(key + '=' + item.value);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
return {
|
|
63
|
+
statusCode: 301,
|
|
64
|
+
statusDescription: 'Moved Permanently',
|
|
65
|
+
headers: {
|
|
66
|
+
location: { value: query.length ? path + '?' + query.join('&') : path },
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
`;
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=viewer-request.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"viewer-request.js","sourceRoot":"","sources":["../../../../../../../libs/public/spa-cdk-stack/src/lib/spa/viewer-request.ts"],"names":[],"mappings":";;AAaA,8CAEC;AAcD,kDAiDC;AAtED;;;;GAIG;AACH,SAAgB,iBAAiB,CAAC,IAAY;IAC5C,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC;AACtD,CAAC;AAED;;;;;;;;;;;GAWG;AACH,SAAgB,mBAAmB,CAAC,EAClC,qBAAqB,GAAG,KAAK,EAC7B,kBAAkB,GAAG,KAAK,GACL;IACrB,OAAO;gCACuB,qBAAqB;6BACxB,kBAAkB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA0C9C,CAAC;AACF,CAAC"}
|