Skip to content

Commit

Permalink
disable prune
Browse files Browse the repository at this point in the history
  • Loading branch information
revmischa committed Oct 11, 2022
1 parent f83ef45 commit 1afcec1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
24 changes: 15 additions & 9 deletions src/NextjsAssetsDeployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import { Construct } from 'constructs';
import * as fs from 'fs-extra';
import * as micromatch from 'micromatch';
import { NextjsBaseProps } from './NextjsBase';
import { createArchive, NextjsBuild, replaceTokenGlobs } from './NextjsBuild';
import {
createArchive,
makeTokenPlaceholder,
NextjsBuild,
replaceTokenGlobs,
TOKEN_PLACEHOLDER_BEGIN,
} from './NextjsBuild';

export interface NextjsAssetsDeploymentProps extends NextjsBaseProps {
/**
Expand All @@ -33,7 +39,7 @@ export interface NextjsAssetsDeploymentProps extends NextjsBaseProps {
* Set to true to delete old assets (defaults to false).
* Recommended to only set to true if you don't need the ability to roll back deployments.
*/
readonly prune?: boolean;
// readonly prune?: boolean;
}

// interface EnvReplaceValues {
Expand Down Expand Up @@ -86,7 +92,7 @@ export class NextJsAssetsDeployment extends Construct {
destinationKeyPrefix: '_next/static',
sources: [Source.asset(staticDir)],
distribution: this.props.distribution, // invalidate Cloudfront distribution caches
prune: this.props.prune,
prune: false,
})
);
}
Expand All @@ -108,7 +114,7 @@ export class NextJsAssetsDeployment extends Construct {
destinationKeyPrefix: this.props.isPlaceholder ? '/placeholder' : '/',
sources: [Source.asset(zipFilePath)],
distribution: this.props.distribution,
prune: this.props.prune,
prune: false,
})
);
}
Expand Down Expand Up @@ -139,9 +145,9 @@ async function tryGetObject(bucket, key, tries) {
try {
return await s3.getObject({ Bucket: bucket, Key: key }).promise();
} catch (err) {
console.error("Failed to retrieve object", key, "\\nCode:", err.code, err);
console.error("Failed to retrieve object", key, err);
// if access denied - wait a few seconds and try again
if (err.code === "AccessDenied" && tries < 3) {
if (err.code === "AccessDenied" && tries < 10) {
console.info("Retrying for object", key);
await new Promise((res) => setTimeout(res, 5000));
return tryGetObject(bucket, key, ++tries);
Expand Down Expand Up @@ -253,7 +259,7 @@ exports.handler = async (event) => {

/*
private createLambdaCodeReplacer(name: string, asset: s3Assets.Asset): CustomResource {
// Note: Source code for the Lambda functions have "{{ ENV_KEY }}" in them.
// Note: Source code for the Lambda functions have "{{! ENV_KEY !}}" in them.
// They need to be replaced with real values before the Lambda
// functions get deployed.
Expand Down Expand Up @@ -324,9 +330,9 @@ exports.handler = async (event) => {

Object.entries(this.props.environment || {})
.filter(([, value]) => Token.isUnresolved(value))
.filter(([key]) => key.startsWith('NEXT_PUBLIC_')) // don't replace server-only env vars
.filter(([key]) => key.startsWith(TOKEN_PLACEHOLDER_BEGIN + 'NEXT_PUBLIC_')) // don't replace server-only env vars
.forEach(([key, value]) => {
const token = `{{ ${key} }}`;
const token = makeTokenPlaceholder(key);
replacements[token] = value.toString();
});

Expand Down
9 changes: 8 additions & 1 deletion src/NextjsBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,15 @@ export function getBuildCmdEnvironment(siteEnvironment?: { [key: string]: string
//
const buildCmdEnvironment: Record<string, string> = {};
Object.entries(siteEnvironment || {}).forEach(([key, value]) => {
buildCmdEnvironment[key] = Token.isUnresolved(value) ? `{{ ${key} }}` : value;
buildCmdEnvironment[key] = Token.isUnresolved(value)
? TOKEN_PLACEHOLDER_BEGIN + key.toString() + TOKEN_PLACEHOLDER_END
: value;
});

return buildCmdEnvironment;
}

export const TOKEN_PLACEHOLDER_BEGIN = '{{! ';
export const TOKEN_PLACEHOLDER_END = ' !}}';
export const makeTokenPlaceholder = (value: string): string =>
TOKEN_PLACEHOLDER_BEGIN + value.toString() + TOKEN_PLACEHOLDER_END;
8 changes: 4 additions & 4 deletions src/NextjsLambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as fs from 'fs-extra';
import * as micromatch from 'micromatch';
import { listDirectory } from './NextjsAssetsDeployment';
import { NextjsBaseProps } from './NextjsBase';
import { createArchive, NextjsBuild, replaceTokenGlobs } from './NextjsBuild';
import { createArchive, makeTokenPlaceholder, NextjsBuild, replaceTokenGlobs } from './NextjsBuild';
import { NextjsLayer } from './NextjsLayer';

export type EnvironmentVars = Record<string, string>;
Expand Down Expand Up @@ -165,7 +165,7 @@ export function getNextPublicEnvReplaceValues(environment: EnvironmentVars): Env
return Object.fromEntries(
Object.entries(environment || {})
.filter(([key]) => key.startsWith('NEXT_PUBLIC_'))
.map(([key]) => [`"{{ ${key} }}"`, `process.env.${key}`]) // will need to replace with actual value for edge functions
.map(([key]) => [makeTokenPlaceholder(key), `process.env.${key}`]) // will need to replace with actual value for edge functions
);
}

Expand All @@ -182,7 +182,7 @@ export function getNextPublicEnvReplaceValues(environment: EnvironmentVars): Env
// // might not get resolved on `next build` if it is used in
// // server-side functions, ie. getServerSideProps().
// // Because Lambda@Edge does not support environment variables, we will
// // use the trick of replacing "{{ _SST_NEXTJS_SITE_ENVIRONMENT_ }}" with
// // use the trick of replacing "{{! _SST_NEXTJS_SITE_ENVIRONMENT_ !}}" with
// // a JSON encoded string of all environment key-value pairs. This string
// // will then get decoded at run time.
// const lambdaEnvs: { [key: string]: string } = {};
Expand All @@ -202,7 +202,7 @@ export function getNextPublicEnvReplaceValues(environment: EnvironmentVars): Env
// replaceValues.push(
// {
// files: '**/*.mjs',
// search: '"{{ _SST_NEXTJS_SITE_ENVIRONMENT_ }}"',
// search: '"{{! _SST_NEXTJS_SITE_ENVIRONMENT_ }}"',
// replace: JSON.stringify(lambdaEnvs),
// },
// {
Expand Down

0 comments on commit 1afcec1

Please sign in to comment.