Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
ssr-sites: standardized Cloudfront function code
Browse files Browse the repository at this point in the history
  • Loading branch information
fwang committed Sep 11, 2024
1 parent 3dfd970 commit 01f2b0a
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 124 deletions.
10 changes: 5 additions & 5 deletions platform/src/components/aws/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -682,17 +682,17 @@ function useCloudFrontRoutingInjection(buildMetadata: BuildMetaConfig) {
var routeData = ${stringifiedTree};
var findFirstMatch = (matches) => Array.isArray(matches[0]) ? findFirstMatch(matches[0]) : matches;
var findMatches = (path, routeData) => routeData.map((route) => route[0].test(path) ? Array.isArray(route[1]) ? findMatches(path, route[1]) : route : null).filter(route => route !== null && route.length > 0);
var matchedRoute = findFirstMatch(findMatches(request.uri, routeData));
var matchedRoute = findFirstMatch(findMatches(event.request.uri, routeData));
if (matchedRoute[0]) {
if (!matchedRoute[1] && !/^.*\\.[^\\/]+$/.test(request.uri)) {
if (!matchedRoute[1] && !/^.*\\.[^\\/]+$/.test(event.request.uri)) {
${
buildMetadata.pageResolution === "file"
? `request.uri = request.uri === "/" ? "/index.html" : request.uri.replace(/\\/?$/, ".html");`
: `request.uri = request.uri.replace(/\\/?$/, "/index.html");`
? `event.request.uri = event.request.uri === "/" ? "/index.html" : event.request.uri.replace(/\\/?$/, ".html");`
: `event.request.uri = event.request.uri.replace(/\\/?$/, "/index.html");`
}
} else if (matchedRoute[1] === 2) {
var redirectPath = matchedRoute[2];
matchedRoute[0].exec(request.uri).forEach((match, index) => {
matchedRoute[0].exec(event.request.uri).forEach((match, index) => {
redirectPath = redirectPath.replace(\`\\\${\${index}}\`, match);
});
return {
Expand Down
32 changes: 16 additions & 16 deletions platform/src/components/aws/nextjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1279,7 +1279,7 @@ export class Nextjs extends Component implements Link.Linkable {
// accept header, and this header is not useful for the rest of the query
return `
function getHeader(key) {
var header = request.headers[key];
var header = event.request.headers[key];
if (header) {
if (header.multiValue) {
return header.multiValue.map((header) => header.value).join(",");
Expand All @@ -1291,7 +1291,7 @@ function getHeader(key) {
return "";
}
var cacheKey = "";
if (request.uri.startsWith("/_next/image")) {
if (event.request.uri.startsWith("/_next/image")) {
cacheKey = getHeader("accept");
} else {
cacheKey =
Expand All @@ -1301,36 +1301,36 @@ if (request.uri.startsWith("/_next/image")) {
getHeader("next-url") +
getHeader("x-prerender-revalidate");
}
if (request.cookies["__prerender_bypass"]) {
cacheKey += request.cookies["__prerender_bypass"]
? request.cookies["__prerender_bypass"].value
if (event.request.cookies["__prerender_bypass"]) {
cacheKey += event.request.cookies["__prerender_bypass"]
? event.request.cookies["__prerender_bypass"].value
: "";
}
var crypto = require("crypto");
var hashedKey = crypto.createHash("md5").update(cacheKey).digest("hex");
request.headers["x-open-next-cache-key"] = { value: hashedKey };
event.request.headers["x-open-next-cache-key"] = { value: hashedKey };
`;
}

function useCloudfrontGeoHeadersInjection() {
// Inject the CloudFront viewer country, region, latitude, and longitude headers into the request headers
// for OpenNext to use them
return `
if(request.headers["cloudfront-viewer-city"]) {
request.headers["x-open-next-city"] = request.headers["cloudfront-viewer-city"];
if(event.request.headers["cloudfront-viewer-city"]) {
event.request.headers["x-open-next-city"] = event.request.headers["cloudfront-viewer-city"];
}
if(request.headers["cloudfront-viewer-country"]) {
request.headers["x-open-next-country"] = request.headers["cloudfront-viewer-country"];
if(event.request.headers["cloudfront-viewer-country"]) {
event.request.headers["x-open-next-country"] = event.request.headers["cloudfront-viewer-country"];
}
if(request.headers["cloudfront-viewer-region"]) {
request.headers["x-open-next-region"] = request.headers["cloudfront-viewer-region"];
if(event.request.headers["cloudfront-viewer-region"]) {
event.request.headers["x-open-next-region"] = event.request.headers["cloudfront-viewer-region"];
}
if(request.headers["cloudfront-viewer-latitude"]) {
request.headers["x-open-next-latitude"] = request.headers["cloudfront-viewer-latitude"];
if(event.request.headers["cloudfront-viewer-latitude"]) {
event.request.headers["x-open-next-latitude"] = event.request.headers["cloudfront-viewer-latitude"];
}
if(request.headers["cloudfront-viewer-longitude"]) {
request.headers["x-open-next-longitude"] = request.headers["cloudfront-viewer-longitude"];
if(event.request.headers["cloudfront-viewer-longitude"]) {
event.request.headers["x-open-next-longitude"] = event.request.headers["cloudfront-viewer-longitude"];
}
`;
}
Expand Down
2 changes: 1 addition & 1 deletion platform/src/components/aws/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ export class React extends Component implements Link.Linkable {
injections: [
// Note: When using libraries like remix-flat-routes the file can
// contains special characters like "+". It needs to be encoded.
`request.uri = request.uri.split('/').map(encodeURIComponent).join('/');`,
`event.request.uri = event.request.uri.split('/').map(encodeURIComponent).join('/');`,
],
},
},
Expand Down
2 changes: 1 addition & 1 deletion platform/src/components/aws/remix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ export class Remix extends Component implements Link.Linkable {
injections: [
// Note: When using libraries like remix-flat-routes the file can
// contains special characters like "+". It needs to be encoded.
`request.uri = request.uri.split('/').map(encodeURIComponent).join('/');`,
`event.request.uri = event.request.uri.split('/').map(encodeURIComponent).join('/');`,
],
},
},
Expand Down
5 changes: 2 additions & 3 deletions platform/src/components/aws/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,8 @@ export class Router extends Component implements Link.Linkable {
runtime: "cloudfront-js-1.0",
code: [
`function handler(event) {`,
` var request = event.request;`,
` request.headers["x-forwarded-host"] = request.headers.host;`,
` return request;`,
` event.request.headers["x-forwarded-host"] = event.request.headers.host;`,
` return event.request;`,
`}`,
].join("\n"),
},
Expand Down
Loading

0 comments on commit 01f2b0a

Please sign in to comment.