Skip to content

Commit

Permalink
fix(lambda-at-edge, nextjs-component): fix triggering regeneration wh…
Browse files Browse the repository at this point in the history
…en sqs queue has custom name (#1595)
  • Loading branch information
dphang committed Aug 26, 2021
1 parent 70917d9 commit 396b8dc
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 9 deletions.
14 changes: 10 additions & 4 deletions packages/libs/lambda-at-edge/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type BuildOptions = {
baseDir?: string;
cleanupDotNext?: boolean;
assetIgnorePatterns?: string[];
regenerationQueueName?: string;
};

const defaultBuildOptions = {
Expand All @@ -64,7 +65,8 @@ const defaultBuildOptions = {
resolve: undefined,
baseDir: process.cwd(),
cleanupDotNext: true,
assetIgnorePatterns: []
assetIgnorePatterns: [],
regenerationQueueName: undefined
};

class Builder {
Expand Down Expand Up @@ -754,8 +756,11 @@ class Builder {
await this.readPublicFiles(assetIgnorePatterns)
);

const { enableHTTPCompression, logLambdaExecutionTimes } =
this.buildOptions;
const {
enableHTTPCompression,
logLambdaExecutionTimes,
regenerationQueueName
} = this.buildOptions;

const apiBuildManifest = {
...apiManifest,
Expand All @@ -764,7 +769,8 @@ class Builder {
const defaultBuildManifest = {
...pageManifest,
enableHTTPCompression,
logLambdaExecutionTimes
logLambdaExecutionTimes,
regenerationQueueName
};
const imageBuildManifest = {
...imageManifest,
Expand Down
10 changes: 9 additions & 1 deletion packages/libs/lambda-at-edge/src/default-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,19 @@ const handleOriginResponse = async ({
staticRoute?.page &&
staticRegenerationResponse.secondsRemainingUntilRevalidation === 0
) {
const regenerationQueueName =
manifest.regenerationQueueName ?? `${bucketName}.fifo`; // if queue name not specified, we used [bucketName].fifo as used in deployment

if (!regenerationQueueName) {
throw new Error("Regeneration queue name is undefined.");
}

const { throttle } = await triggerStaticRegeneration({
basePath,
request,
response,
pagePath: staticRoute.page
pagePath: staticRoute.page,
queueName: regenerationQueueName
});

// Occasionally we will get rate-limited by the Queue (in the event we
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ interface TriggerStaticRegenerationOptions {
response: AWSLambda.CloudFrontResponse;
basePath: string | undefined;
pagePath: string;
queueName: string;
}

export const triggerStaticRegeneration = async (
options: TriggerStaticRegenerationOptions
): Promise<{ throttle: boolean }> => {
const { region } = options.request.origin?.s3 || {};
const bucketName = s3BucketNameFromEventRequest(options.request);
const queueName = options.queueName;

if (!bucketName) {
throw new Error("Expected bucket name to be defined");
Expand All @@ -39,7 +41,7 @@ export const triggerStaticRegeneration = async (
try {
await sqs.send(
new SendMessageCommand({
QueueUrl: `https://sqs.${region}.amazonaws.com/${bucketName}.fifo`,
QueueUrl: `https://sqs.${region}.amazonaws.com/${queueName}`,
MessageBody: JSON.stringify(regenerationEvent), // This is not used, however it is a required property
// We only want to trigger the regeneration once for every previous
// update. This will prevent the case where this page is being
Expand Down
1 change: 1 addition & 0 deletions packages/libs/lambda-at-edge/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type OriginRequestApiHandlerManifest = ApiManifest & {
export type OriginRequestDefaultHandlerManifest = PageManifest & {
logLambdaExecutionTimes?: boolean;
enableHTTPCompression?: boolean;
regenerationQueueName?: string;
};

export type OriginRequestImageHandlerManifest = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ describe("triggerStaticRegeneration()", () => {
headers: { etag: [{ key: "Etag", value: "123" }] },
status: "200",
statusDescription: "ok"
} as AWSLambda.CloudFrontResponse
} as AWSLambda.CloudFrontResponse,
queueName: "my-bucket.fifo",
pagePath: "/"
};

class RequestThrottledException extends Error {
Expand Down Expand Up @@ -112,7 +114,8 @@ describe("triggerStaticRegeneration()", () => {
region: "us-east-1",
bucketName: "my-bucket",
cloudFrontEventRequest: options.request,
basePath: ""
basePath: "",
pagePath: "/"
}),
MessageDeduplicationId: expected,
MessageGroupId: "index.html"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ class NextjsComponent extends Component {
authentication: inputs.authentication ?? undefined,
baseDir: buildConfig.baseDir,
cleanupDotNext: buildConfig.cleanupDotNext,
assetIgnorePatterns: buildConfig.assetIgnorePatterns
assetIgnorePatterns: buildConfig.assetIgnorePatterns,
regenerationQueueName: inputs.sqs?.name
},
nextStaticPath
);
Expand Down

0 comments on commit 396b8dc

Please sign in to comment.