serverless-nextjs / serverless-next.js

⚡ Deploy your Next.js apps on AWS Lambda@Edge via Serverless Components
MIT License
4.43k stars 451 forks source link

cdk-construct: the updated files under `public/` not updated in deployment (cloudfront) #2473

Open azhong-git opened 2 years ago

azhong-git commented 2 years ago

Issue Summary

Hello, I am using the following script to deploy a NextJS site to CloudFront. However I came across the following two issues regarding statically served files under /public:

Is any of the two issues expected? I have included my script (pretty standard) below:


import { Stack, StackProps } from "aws-cdk-lib";
import { Construct } from "constructs";
import { NextJSLambdaEdge } from "@sls-next/cdk-construct";

// Followed the examples below to run deploy NextJS with CDK
// https://github.com/serverless-nextjs/serverless-next.js/blob/master/documentation/docs/cdkconstruct.md
// https://github.com/serverless-nextjs/serverless-next.js/tree/master/packages/serverless-components/nextjs-cdk-construct/examples/basic
export class MyStack extends Stack {
  constructor(scope: Construct, id: string, props: StackProps) {
    super(scope, id, props);
    new NextJSLambdaEdge(this, "NextJSApp", {
      serverlessBuildOutDir: "./build",
    });
  }
}
#!/usr/bin/env node
import { App } from "aws-cdk-lib";
import { Builder } from "@sls-next/lambda-at-edge";
import { MyStack } from "../stack";

const builder = new Builder(".", "./build", { args: ["build"] });

builder
  .build(true)
  .then(() => {
    const app = new App();
    new MyStack(app, "AppStack", {
      env: {
        // Only us-east-1 supports lambda edge
        region: "us-east-1",
      },
      analyticsReporting: true,
    });
  })
  .catch((e) => {
    console.error(e);
    process.exit(1);
  });

Expected behavior

Local changes in /public gets updated in the deployed site right away after npm run cdk deploy.

Versions

"@sls-next/cdk-construct": "^3.7.0", "@sls-next/lambda-at-edge": "^3.7.0",

Checklist

azhong-git commented 2 years ago

No longer a blocking issue for me and it seems that the behavior is expected according to CloudFront docs included in the clarification of issue 1 above.

However I am still looking for advice on best practice updating static files:

andrewdavidcostello commented 1 year ago

I just came looking for this exact issue. I think the 10 minute times you are experiencing could be considered lucky as we are seeing anything from 10 minutes to a few days for everything to clear out depending on how cloudfront handles things.

I have not tried this yet but you may just be able to set invalidationPaths: ["/*"] as per the docs or define this manually.