diff --git a/OriginRequestLambda/index.js b/OriginRequestLambda/index.js new file mode 100644 index 0000000..cc9899b --- /dev/null +++ b/OriginRequestLambda/index.js @@ -0,0 +1,78 @@ +/* eslint-disable no-useless-escape */ +'use strict'; + + +function parseCookies(headers) { + const parsedCookie = {}; + if (headers.cookie) { + headers.cookie[0].value.split(';').forEach((cookie) => { + if (cookie) { + const parts = cookie.split('='); + parsedCookie[parts[0].trim()] = parts[1].trim(); + } + }); + } + return parsedCookie; +} + +const pointsToFile = uri => /\/[^/]+\.[^/]+$/.test(uri); + +export const handler = (event, context, callback) => { + // Cookie name that will be used to determine the if dynamic routing needed + const sourceHash = 'X-source'; + // path for develop environment --- This value should be unique for each environment + const developPath = 'develop'; + const entryPoint = '/develop/index.html' + + var request = event.Records[0].cf.request; + const headers = request.headers; + + + // Extract the URI from the request and validate route to file or folder + var oldUri = request.uri; + var newUri; + + // Spa App only needs to route to the base path in s3, browser routing will handle in app routes + // Can later distinguish between microfrontends by checking the path here + const paths = oldUri.split('/'); + if (paths.length > 1) { + oldUri = '/' + paths[paths.length - 1]; + } + // If the URI points to an html file that is not index.html, set it as index.html + // else return base app (spa entry point is index.html) + if (pointsToFile(oldUri)) { + if(oldUri.includes('html')) { + newUri = entryPoint + } else { + newUri = '/develop/assets' + oldUri + } + + } else { // doesn't point to a file, so set at entry point + newUri = entryPoint + } + + + // Log the URI as received by CloudFront and the new URI to be used to fetch from origin + console.log("Old URI: " + oldUri); + console.log("New URI: " + newUri); + + // Replace the received URI with the URI that includes the index page + request.uri = newUri; + + + if (headers.cookie) { + for (let i = 0; i < headers.cookie.length; i++) { + if (headers.cookie[i].value.indexOf(sourceHash) >= 0) { + console.log('source has cookie found'); + const parsedCookies = parseCookies(headers) + request.uri = request.uri.replace(developPath, parsedCookies[sourceHash]); + break; + } + } + } + + + + console.log(`Request uri set to "${request.uri}"`); + callback(null, request); +}; diff --git a/src/App.tsx b/src/App.tsx index 7e05ada..6db7459 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,6 +1,6 @@ import { useState } from 'react' import reactLogo from './assets/react.svg' -import viteLogo from '/vite.svg' +import viteLogo from './assets/vite.svg' import './App.css' function App() { diff --git a/public/vite.svg b/src/assets/vite.svg similarity index 100% rename from public/vite.svg rename to src/assets/vite.svg