-
Notifications
You must be signed in to change notification settings - Fork 47
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: use existing policies #195
Conversation
passing nextjsDistribution: {
imageCachePolicy: getCachePolicy(
scope, //
`${prefix}-ImageCachePolicy`,
props.policies.imageCachePolicyId,
),
imageResponseHeadersPolicy: getResponseHeadersPolicy(
scope, //
`${prefix}-ImageResponseHeadersPolicy`,
props.policies.imageResponseHeadersPolicyId,
),
serverCachePolicy: getCachePolicy(
scope, //
`${prefix}-ServerCachePolicy`,
props.policies.serverCachePolicyId,
),
serverResponseHeadersPolicy: getResponseHeadersPolicy(
scope, //
`${prefix}-ServerResponseHeadersPolicy`,
props.policies.serverResponseHeadersPolicyId,
),
staticResponseHeadersPolicy: getResponseHeadersPolicy(
scope, //
`${prefix}-StaticResponseHeadersPolicy`,
props.policies.staticResponseHeadersPolicyId,
),
}, it deploys using the given policies |
Thanks for this feature, @onhate! |
well, now that you said, considering that the distribution is passed on the I'll adjust. |
@onhate, it seems like the code changes in Thinking through this more, I think a simpler way to achieve the goal you have is to use the already existing |
no, it's there. am I missing something?
but aren't those policies required for it to work? and what if I want custom policies but to reusing existing? |
Whoops, sorry, I see those changes now.
What I am suggesting I think would result in the same behavior this PR creates. If you were to pass in an override, then You'd use it something like: const cachePolicy = new CachePolicy(...);
new Nextjs(this, "MyNextjs", {
overrides: {
nextjsDistribution: {
serverBehaviorOptions: {
cachePolicy,
}
}
}
}); and then in construct: private createServerBehaviorOptions(): cloudfront.BehaviorOptions {
const fnUrl = this.props.serverFunction.addFunctionUrl({ authType: this.fnUrlAuthType });
const origin = new origins.HttpOrigin(Fn.parseDomainName(fnUrl.url), this.props.overrides?.serverHttpOriginProps);
let cachePolicy: cloudfront.CachePolicy | undefined;
if (!this.props.overrides?.serverBehaviorOptions?.cachePolicy) {
cachePolicy = new cloudfront.CachePolicy(this, 'ServerCachePolicy', {
queryStringBehavior: cloudfront.CacheQueryStringBehavior.all(),
headerBehavior: cloudfront.CacheHeaderBehavior.allowList(
'accept',
'rsc',
'next-router-prefetch',
'next-router-state-tree',
'next-url',
'x-prerender-revalidate'
),
cookieBehavior: cloudfront.CacheCookieBehavior.all(),
defaultTtl: Duration.seconds(0),
maxTtl: Duration.days(365),
minTtl: Duration.seconds(0),
enableAcceptEncodingBrotli: true,
enableAcceptEncodingGzip: true,
comment: 'Nextjs Server Cache Policy',
...this.props.overrides?.serverCachePolicyProps,
});
}
let responseHeadersPolicy: ResponseHeadersPolicy | undefined;
if (!this.props.overrides?.serverBehaviorOptions?.responseHeadersPolicy) {
responseHeadersPolicy = new ResponseHeadersPolicy(this, 'ServerResponseHeadersPolicy', {
customHeadersBehavior: {
customHeaders: [
{
header: 'cache-control',
override: false,
// MDN Cache-Control Use Case: Up-to-date contents always
// @see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#up-to-date_contents_always
value: `no-cache`,
},
],
},
securityHeadersBehavior: this.commonSecurityHeadersBehavior,
comment: 'Nextjs Server Response Headers Policy',
...this.props.overrides?.serverResponseHeadersPolicyProps,
});
}
return {
...this.commonBehaviorOptions,
origin,
allowedMethods: cloudfront.AllowedMethods.ALLOW_ALL,
originRequestPolicy: cloudfront.OriginRequestPolicy.ALL_VIEWER_EXCEPT_HOST_HEADER,
cachePolicy,
edgeLambdas: this.edgeLambdas.length ? this.edgeLambdas : undefined,
functionAssociations: this.createCloudFrontFnAssociations(),
responseHeadersPolicy,
...this.props.overrides?.serverBehaviorOptions,
};
} |
ooow, now I see, and in this case we wouldn't even need to add properties. Ok, I like it better. |
I'll start a fresh PR from scratch. |
closes #194