Skip to content

Commit

Permalink
Merge pull request #60 from khuezy/fix/imageOpt
Browse files Browse the repository at this point in the history
fix: image optimization and absolute trace output path
  • Loading branch information
khuezy authored Dec 19, 2022
2 parents f2a6107 + be7e8c2 commit 46d3449
Show file tree
Hide file tree
Showing 8 changed files with 974 additions and 880 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .projen/deps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .projenrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const project = new awscdk.AwsCdkConstructLibrary({
'serverless-http',
'jszip',
'glob',
'node-fetch',
] /* Runtime dependencies of this module. */,
devDeps: ['aws-sdk', '[email protected]'] /* Build dependencies for this module. */,

Expand Down
9 changes: 7 additions & 2 deletions assets/lambda/ImageOptimization/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import { ImageConfigComplete, ImageConfig } from 'next/dist/shared/lib/image-con
import { Writable } from 'node:stream'
import https from 'node:https'
import { getNextServerConfig } from '../utils'
import fetch from 'node-fetch'

// TODO: Remove this once we use Node18.x
// imageOptimizer uses fetch in its preprocessing logic
global.fetch = fetch

const sourceBucket = process.env.S3_SOURCE_BUCKET ?? undefined

Expand Down Expand Up @@ -110,9 +115,9 @@ const optimizer: APIGatewayProxyHandlerV2 = async (event) => {
body: optimizedResult.buffer.toString('base64'),
isBase64Encoded: true,
headers: {
Vary: 'Accept',
Vary: 'Accept',
'Cache-Control': `public,max-age=${optimizedResult.maxAge},immutable`,
'Content-Type': optimizedResult.contentType
'Content-Type': optimizedResult.contentType,
},
}
} catch (error: any) {
Expand Down
2 changes: 2 additions & 0 deletions package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/ImageOptimizationLambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class ImageOptimizationLambda extends NodejsFunction {
},
minify: true,
target: 'node16',
externalModules: ['@aws-sdk/client-s3'],
externalModules: [],
},
layers: [props.nextLayer],
...lambdaOptions,
Expand Down
15 changes: 1 addition & 14 deletions src/NextjsBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class NextjsBuild extends Construct {
const buildEnv = {
...process.env,
[NEXTJS_BUILD_STANDALONE_ENV]: 'true',
[NEXTJS_BUILD_OUTPUTTRACEROOT_ENV]: getRootTracePath(nextjsPath),
[NEXTJS_BUILD_OUTPUTTRACEROOT_ENV]: path.resolve(),
...getBuildCmdEnvironment(this.props.environment),
...(this.props.nodeEnv ? { NODE_ENV: this.props.nodeEnv } : {}),
};
Expand Down Expand Up @@ -245,16 +245,3 @@ export const TOKEN_PLACEHOLDER_BEGIN = '{NEXT{! ';
export const TOKEN_PLACEHOLDER_END = ' !}}';
export const makeTokenPlaceholder = (value: string): string =>
TOKEN_PLACEHOLDER_BEGIN + value.toString() + TOKEN_PLACEHOLDER_END;

/**
* Replaces each path with "../"
* @param path
*/
function getRootTracePath(p: string): string {
p = p.replace(/^\.\/?/, '');
return p
.split('/')
.filter((v) => v && v !== '.')
.map(() => '..')
.join('/');
}
Loading

0 comments on commit 46d3449

Please sign in to comment.