Skip to content

Commit

Permalink
feat(cdk-sanity-site): rewrite non-static to index
Browse files Browse the repository at this point in the history
  • Loading branch information
thijsdaniels committed Jul 15, 2024
1 parent c8d5f0b commit 307d746
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/new-seahorses-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@codedazur/cdk-sanity-site": patch
---

Every request that is not to a static file is now rewritten to the index.
6 changes: 5 additions & 1 deletion packages/cdk-sanity-site/src/constructs/SanitySite.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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";
}
Expand Down

0 comments on commit 307d746

Please sign in to comment.