Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow functionAssociations overrides #237

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

coryasilva
Copy link

@coryasilva coryasilva commented Dec 10, 2024

Fixes #229 Allows functionAssociations to be overriden by exposing an optional overrides.nextjsDistribution.viewerRequestFunctionProps.code prop. Sample usage:

const cmsRedirectKvStore = new cloudfront.KeyValueStore(this, "CmsRedirectKeyValueStore");

const viewerRequestCode = `
import cf from 'cloudfront';

const kvsId = 'KEY_VALUE_STORE_ID_PLACEHOLDER';
const kvs = cf.kvs(kvsId);

async function handler(event) {
  // My custom code to do canonical redirects here...
  // Or my code to use kv store here...

  // INJECT_CLOUDFRONT_FUNCTION_HOST_HEADER

  // INJECT_CLOUDFRONT_FUNCTION_CACHE_HEADER_KEY
}
`

viewerRequestCode.replace(
  'KEY_VALUE_STORE_ID_PLACEHOLDER',
  cmsRedirectKvStore.keyValueStoreId,
);

const nextjs = new Nextjs(this, 'Nextjs', {
  nextjsPath: './web',
  overrides: {
    nextjsDistribution: {
      viewerRequestFunctionProps: {
        code: cloudfront.FunctionCode.fromInline(viewerRequestCode)
        keyValueStore: cmsRedirectKvStore,
      },
    },
  },
});

Also enable better cache strategy stolen from SST impl

This does not require but compliments the efforts for #234 and works towards resolving part of discussion #222

@coryasilva coryasilva force-pushed the 229-override-cf-func-v2 branch 4 times, most recently from 8fdf67c to 6c9c62d Compare December 17, 2024 17:00
@coryasilva coryasilva force-pushed the 229-override-cf-func-v2 branch from 6c9c62d to e293b4b Compare December 18, 2024 23:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Need a way to override.distributionProps.defaultBehavior.functionAssociations
1 participant