diff --git a/.gitignore b/.gitignore index a25b9365..16b3ae97 100644 --- a/.gitignore +++ b/.gitignore @@ -77,4 +77,3 @@ cdk.out # Used during the publish stage .npmignore -*.tgz \ No newline at end of file diff --git a/packages/geoip-redirect/README.md b/packages/geoip-redirect/README.md index 40b83a8d..812a2014 100644 --- a/packages/geoip-redirect/README.md +++ b/packages/geoip-redirect/README.md @@ -1,4 +1,53 @@ -# Geo-IP Redirect -This library provides a construct which creates a Lambda@Edge functions to perform GeoIP redirects. +# Geo-IP Redirect -These functions are intended to be added to an existing Cloudfront distribution +![TypeScript version](https://img.shields.io/github/package-json/dependency-version/aligent/cdk-constructs/dev/typescript?filename=packages/geoip-redirect/package.json&color=red) ![AWS CDK version](https://img.shields.io/github/package-json/dependency-version/aligent/cdk-constructs/dev/aws-cdk?filename=packages/geoip-redirect/package.json) ![NPM version](https://img.shields.io/npm/v/%40aligent%2Fcdk-geoip-redirect?color=green) + +## Overview + +This library provides a construct which creates a Lambda@Edge which is intended to be attached to the Origin Request in a CloudFront distribution. The construct allows a CloudFront website to perform GeoIP redirects to redirect users to a version of the website related to their location such as `.com` `.com.au` or `.co.nz` etc. + +The Lambda@Edge function will check if the viewer's country code matches any supported regions. The user's country code for each request is pulled from the `cloudfront-viewer-country`. The construct will match the code to the record with the corresponding regex lookup. + +## Usage and Default Geo-IP Redirect options + +### `redirectHost` (string) + +``` +interface RedirectFunctionOptions { + supportedRegions?: Record; + defaultRegionCode: string; + defaultDomain: string; +} +``` + +| Property | Definition | +| ------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------- | +| `supportedRegions` | A record with domain codes as a key (regex) and a domain to redirect to as a value | +| `defaultRegionCode` | The default region code(s) as regex. These are the regions supported by `defaultDomain`. When multiple codes are used the default will be the first code the default site eg. `AU | NZ`will treat`AU` as the default | +| `defaultDomain` | The website's main domain. This will act as a fallback for any unsupported regions | + +### Using this package + +The two main ways you can use this package are as follows: +First off your website has a basic domain let's say `www.iamawesome.com` and you serve all content for all regions of the world here such as `www.iamawesome.com/us` or `www.iamawesome.com/au`. For this approach you should use the below method + +``` +redirectBehaviourOptions: { + defaultDomain: "www.iamawesome.com", + defaultRegionCode: "AU|US", +} +``` + +Any region codes that are regexed to be `XX|YY` (note the pipe `|`) will automatically add the matching region as a path suffix to the url. + +However perhaps you also have `www.iamawesome.co.nz`. How to redirect to a website that is different from the base domain? To achieve this, you can "hardcode" a domain for a region to use by using the `supportedRegions` value. + +``` +redirectBehaviourOptions: { + defaultDomain: "www.example.com", + defaultRegionCode: "AU|US", + supportedRegions: { "NZ": "www.example.co.nz" } +} +``` + +_this package has not been tested with interplanetary domains_ diff --git a/packages/geoip-redirect/lib/handlers/redirect.ts b/packages/geoip-redirect/lib/handlers/redirect.ts index c3ccd374..8b4555cb 100644 --- a/packages/geoip-redirect/lib/handlers/redirect.ts +++ b/packages/geoip-redirect/lib/handlers/redirect.ts @@ -20,7 +20,7 @@ export const handler = async ( options.defaultRegionCode = process.env.DEFAULT_REGION_CODE ?? ""; const defaultRegion = options.defaultRegionCode.split("|")[0].toLowerCase(); options.supportedRegions = { - ...process.env.SUPPORTED_REGIONS as Record ?? "{}", + ...process.env.SUPPORTED_REGIONS as Record ?? "{}", ...{ [options.defaultRegionCode]: options.defaultDomain } }; diff --git a/packages/geoip-redirect/lib/redirect-construct.ts b/packages/geoip-redirect/lib/redirect-construct.ts index 8d9dbdcd..1c05668a 100644 --- a/packages/geoip-redirect/lib/redirect-construct.ts +++ b/packages/geoip-redirect/lib/redirect-construct.ts @@ -5,8 +5,6 @@ import { Construct } from "constructs"; import { join } from "path"; import { Esbuild } from "@aligent/cdk-esbuild"; -export type DomainOverwrite = string | null - /** * The default region, domain, and other supported regions for a website to redirect to. */ @@ -15,7 +13,7 @@ export interface RedirectFunctionOptions { * Regex formatted string to match region codes and redirect to the DomainOverwrite destination. * @default undefined */ - supportedRegions?: Record; + supportedRegions?: Record; /** * Regex for supported domain paths on the default domain eg .com/au */ @@ -48,10 +46,6 @@ export class RedirectFunction extends Construct { command, image: DockerImage.fromRegistry("busybox"), local: new Esbuild({ - - minify: false, - minifyWhitespace: false, - minifySyntax: false, entryPoints: [join(__dirname, "handlers/redirect.ts")], define: { "process.env.DEFAULT_DOMAIN": JSON.stringify(options.defaultDomain), @@ -68,7 +62,7 @@ export class RedirectFunction extends Construct { } public getFunctionVersion(): IVersion { - return Version.fromVersionArn( // SEE THE README ON YOUR DESKTOP (AND DELETE THESE COMMENTS) + return Version.fromVersionArn( this, "redirect-fn-version", this.edgeFunction.currentVersion.edgeArn diff --git a/smgb_stripped_prod_20241204.sql.gz b/smgb_stripped_prod_20241204.sql.gz deleted file mode 100644 index c30b16c7..00000000 Binary files a/smgb_stripped_prod_20241204.sql.gz and /dev/null differ