From 307d7468f11177b60c02e03d7111c6b08a4a19f3 Mon Sep 17 00:00:00 2001 From: Thijs Daniels Date: Mon, 15 Jul 2024 12:19:34 +0200 Subject: [PATCH] feat(cdk-sanity-site): rewrite non-static to index --- .changeset/new-seahorses-appear.md | 5 +++++ packages/cdk-sanity-site/src/constructs/SanitySite.ts | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 .changeset/new-seahorses-appear.md diff --git a/.changeset/new-seahorses-appear.md b/.changeset/new-seahorses-appear.md new file mode 100644 index 00000000..29c3be8b --- /dev/null +++ b/.changeset/new-seahorses-appear.md @@ -0,0 +1,5 @@ +--- +"@codedazur/cdk-sanity-site": patch +--- + +Every request that is not to a static file is now rewritten to the index. diff --git a/packages/cdk-sanity-site/src/constructs/SanitySite.ts b/packages/cdk-sanity-site/src/constructs/SanitySite.ts index 25cc2f9a..c7db1958 100644 --- a/packages/cdk-sanity-site/src/constructs/SanitySite.ts +++ b/packages/cdk-sanity-site/src/constructs/SanitySite.ts @@ -1,3 +1,5 @@ +/* eslint-disable no-useless-escape */ + import { StaticSite, StaticSiteProps } from "@codedazur/cdk-static-site"; import { FunctionCode } from "aws-cdk-lib/aws-cloudfront"; import { Construct } from "constructs"; @@ -16,7 +18,9 @@ export class SanitySite extends StaticSite { ...(props.distribution?.functions?.viewerRequest ?? []), FunctionCode.fromInline(/* js */ ` function rewriteToIndex(event, next) { - if (!event.request.uri.match(/.*.[^?]*$/)) { + const isStatic = /^\/static\//.test(event.request.uri); + + if (!isStatic) { event.request.uri = "/index.html"; }