Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Commit c68a4fc

Browse files
authored
fix(domain, nextjs-component): set aliases correctly with domainType of apex and domain starting with www (#723)
1 parent beebc26 commit c68a4fc

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

packages/serverless-components/domain/serverless.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ class Domain extends Component {
133133
subdomain,
134134
certificate.CertificateArn,
135135
inputs.domainType,
136-
inputs.defaultCloudfrontInputs
136+
inputs.defaultCloudfrontInputs,
137+
this.context
137138
);
138139

139140
this.context.debug(
@@ -146,7 +147,7 @@ class Domain extends Component {
146147
domainHostedZoneId,
147148
subdomain.url.replace("https://", ""),
148149
inputs.domainType,
149-
this
150+
this.context
150151
);
151152
} else {
152153
this.context.debug(
@@ -215,7 +216,8 @@ class Domain extends Component {
215216
clients.route53,
216217
domainState.domain,
217218
domainHostedZoneId,
218-
domainState.url.replace("https://", "")
219+
domainState.url.replace("https://", ""),
220+
this.context
219221
);
220222
} else {
221223
this.context.debug(

packages/serverless-components/domain/utils.js

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ const getDomainHostedZoneId = async (route53, domain, privateZone) => {
8484
const params = {
8585
DNSName: domain
8686
};
87-
87+
8888
const hostedZonesRes = await route53.listHostedZonesByName(params).promise();
8989

9090
const hostedZone = hostedZonesRes.HostedZones.find(
@@ -288,7 +288,7 @@ const configureDnsForCloudFrontDistribution = async (
288288
domainHostedZoneId,
289289
distributionUrl,
290290
domainType,
291-
that
291+
context
292292
) => {
293293
const dnsRecordParams = {
294294
HostedZoneId: domainHostedZoneId,
@@ -329,6 +329,11 @@ const configureDnsForCloudFrontDistribution = async (
329329
});
330330
}
331331

332+
context.debug(
333+
"Updating Route53 DNS records with parameters:\n" +
334+
JSON.stringify(dnsRecordParams, null, 2)
335+
);
336+
332337
return route53.changeResourceRecordSets(dnsRecordParams).promise();
333338
};
334339

@@ -339,7 +344,8 @@ const removeCloudFrontDomainDnsRecords = async (
339344
route53,
340345
domain,
341346
domainHostedZoneId,
342-
distributionUrl
347+
distributionUrl,
348+
context
343349
) => {
344350
const params = {
345351
HostedZoneId: domainHostedZoneId,
@@ -364,6 +370,10 @@ const removeCloudFrontDomainDnsRecords = async (
364370
// TODO: should the CNAME records be removed too?
365371

366372
try {
373+
context.debug(
374+
"Updating Route53 with parameters:\n" + JSON.stringify(params, null, 2)
375+
);
376+
367377
await route53.changeResourceRecordSets(params).promise();
368378
} catch (e) {
369379
if (e.code !== "InvalidChangeBatch") {
@@ -377,7 +387,8 @@ const addDomainToCloudfrontDistribution = async (
377387
subdomain,
378388
certificateArn,
379389
domainType,
380-
defaultCloudfrontInputs
390+
defaultCloudfrontInputs,
391+
context
381392
) => {
382393
// Update logic is a bit weird...
383394
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CloudFront.html#updateDistribution-property
@@ -398,21 +409,21 @@ const addDomainToCloudfrontDistribution = async (
398409

399410
// 5. then make our changes
400411
params.DistributionConfig.Aliases.Items.push(subdomain.domain);
401-
params.DistributionConfig.Aliases.Quantity += 1;
402412
if (subdomain.domain.startsWith("www.")) {
403413
if (domainType === "apex") {
404-
params.DistributionConfig.Aliases.Items[-1] = `${subdomain.domain.replace(
405-
"www.",
406-
""
407-
)}`;
414+
params.DistributionConfig.Aliases.Items[
415+
params.DistributionConfig.Aliases.Items.length - 1
416+
] = `${subdomain.domain.replace("www.", "")}`;
408417
} else if (domainType !== "www") {
409418
params.DistributionConfig.Aliases.Items.push(
410419
`${subdomain.domain.replace("www.", "")}`
411420
);
412-
params.DistributionConfig.Aliases.Quantity += 1;
413421
}
414422
}
415423

424+
params.DistributionConfig.Aliases.Quantity =
425+
params.DistributionConfig.Aliases.Items.length;
426+
416427
params.DistributionConfig.ViewerCertificate = {
417428
ACMCertificateArn: certificateArn,
418429
SSLSupportMethod: "sni-only",
@@ -422,6 +433,11 @@ const addDomainToCloudfrontDistribution = async (
422433
...defaultCloudfrontInputs.viewerCertificate
423434
};
424435

436+
context.debug(
437+
"Updating CloudFront distribution with parameters:\n" +
438+
JSON.stringify(params, null, 2)
439+
);
440+
425441
// 6. then finally update!
426442
const res = await cf.updateDistribution(params).promise();
427443

@@ -432,7 +448,11 @@ const addDomainToCloudfrontDistribution = async (
432448
};
433449
};
434450

435-
const removeDomainFromCloudFrontDistribution = async (cf, subdomain) => {
451+
const removeDomainFromCloudFrontDistribution = async (
452+
cf,
453+
subdomain,
454+
context
455+
) => {
436456
const params = await cf
437457
.getDistributionConfig({ Id: subdomain.distributionId })
438458
.promise();
@@ -453,6 +473,11 @@ const removeDomainFromCloudFrontDistribution = async (cf, subdomain) => {
453473
MinimumProtocolVersion: DEFAULT_MINIMUM_PROTOCOL_VERSION
454474
};
455475

476+
context.debug(
477+
"Updating CloudFront distribution with parameters:\n" +
478+
JSON.stringify(params, null, 2)
479+
);
480+
456481
const res = await cf.updateDistribution(params).promise();
457482

458483
return {

packages/serverless-components/nextjs-component/.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ declare module "@serverless/core" {
1010
instance: {
1111
debugMode: boolean;
1212
};
13+
status(status: string): void;
14+
debug(message: string): void;
1315
};
1416
}
1517
}

0 commit comments

Comments
 (0)