Skip to content

Commit 3bf183c

Browse files
committed
fix: add CloudFront Function JS file to git (excluded by *.js in .gitignore)
1 parent 53b2463 commit 3bf183c

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

cdk/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
*.js
22
!jest.config.js
3+
!lib/constructs/cf-lambda-furl-service/cf-function/*.js
34
*.d.ts
45
node_modules
56

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// CloudFront Functions JS 2.0
2+
// Combines Next.js RSC-related headers into a single hashed cache key header
3+
// to prevent cache poisoning between HTML and RSC flight responses.
4+
//
5+
// Next.js App Router sets Vary: rsc, next-router-state-tree, next-router-prefetch,
6+
// next-router-segment-prefetch (and next-url for interception routes).
7+
// CloudFront ignores Vary and requires explicit cache key configuration,
8+
// but its Cache Policy has a 10-header limit. This function hashes all
9+
// RSC headers into one header to stay within the limit.
10+
async function handler(event) {
11+
var h = event.request.headers;
12+
var parts = [
13+
'rsc',
14+
'next-router-prefetch',
15+
'next-router-state-tree',
16+
'next-router-segment-prefetch',
17+
'next-url',
18+
];
19+
var key = '';
20+
for (var i = 0; i < parts.length; i++) {
21+
if (h[parts[i]]) {
22+
key += parts[i] + '=' + h[parts[i]].value + ';';
23+
}
24+
}
25+
if (key) {
26+
// FNV-1a hash. Cryptographic strength is unnecessary;
27+
// we only need distinct cache keys for distinct header combinations.
28+
var hash = 2166136261;
29+
for (var j = 0; j < key.length; j++) {
30+
hash ^= key.charCodeAt(j);
31+
hash = (hash * 16777619) | 0;
32+
}
33+
event.request.headers['x-nextjs-cache-key'] = { value: String(hash >>> 0) };
34+
}
35+
return event.request;
36+
}

0 commit comments

Comments
 (0)