Skip to content

Commit

Permalink
added guide for query parameters and cookies
Browse files Browse the repository at this point in the history
Resolves #98
Resolves #99
  • Loading branch information
lukeshay committed Nov 27, 2023
1 parent 81ccee9 commit 6d2b1a6
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 11 deletions.
4 changes: 2 additions & 2 deletions apps/docs/src/content/docs/guides/01-getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ pnpm i @astro-aws/constructs
```ts
import { Stack } from "aws-cdk-lib/core"
import type { StackProps } from "aws-cdk-lib/core"
import { AstroAWSConstruct } from "@astro-aws/constructs"
import { AstroAWS } from "@astro-aws/constructs"

export interface HelloCdkStackProps extends StackProps {}

export class HelloCdkStack extends Stack {
public constructor(scope: Construct, id: string, props: HelloCdkStackProps) {
super(scope, id, props)

new AstroAWSConstruct(this, "AstroAWSConstruct", {
new AstroAWS(this, "AstroAWS", {
websiteDir: "../my-astro-project",
})
}
Expand Down
54 changes: 54 additions & 0 deletions apps/docs/src/content/docs/guides/02-query-parameters.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
title: Query Parameters
description: Describes how to access query parameters on the server.
---

## Setup

Follow the [getting started guide](./getting-started) to create a new Astro project with the Astro AWS adapter.

## Allowing Query Parameters

In order to allow query parameters to be passed to your application, you must create a custom `CachePolicy` for the CloudFront distribution. The following example based on the getting started guide will allow all query parameters to be passed to your application.

`lib/hello-cdk-stack.ts`:

```ts
import { Stack } from "aws-cdk-lib/core"
import type { StackProps } from "aws-cdk-lib/core"
import { AstroAWS } from "@astro-aws/constructs"
import {
CachePolicy,
CacheQueryStringBehavior,
} from "aws-cdk-lib/aws-cloudfront"

export interface HelloCdkStackProps extends StackProps {}

export class HelloCdkStack extends Stack {
public constructor(scope: Construct, id: string, props: HelloCdkStackProps) {
super(scope, id, props)

const cachePolicy = new CachePolicy(this, "CachePolicy", {
queryStringBehavior: CacheQueryStringBehavior.all(),
})

new AstroAWS(this, "AstroAWS", {
cdk: {
// This configures all subpaths of /api.
apiBehavior: {
cachePolicy,
},
// This configures everything excluding subpaths of /api.
cloudfrontDistribution: {
cachePolicy,
},
},
websiteDir: "../my-astro-project",
})
}
}
```

```
```
51 changes: 51 additions & 0 deletions apps/docs/src/content/docs/guides/03-cookies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
title: Cookies
description: Describes how to access cookies on the server.
---

## Setup

Follow the [getting started guide](./getting-started) to create a new Astro project with the Astro AWS adapter.

## Allowing Cookies

In order to allow cookies to be passed to your application, you must create a custom `CachePolicy` for the CloudFront distribution. The following example based on the getting started guide will allow all cookies to be passed to your application.

`lib/hello-cdk-stack.ts`:

```ts
import { Stack } from "aws-cdk-lib/core"
import type { StackProps } from "aws-cdk-lib/core"
import { AstroAWS } from "@astro-aws/constructs"
import { CachePolicy, CacheCookieBehavior } from "aws-cdk-lib/aws-cloudfront"

export interface HelloCdkStackProps extends StackProps {}

export class HelloCdkStack extends Stack {
public constructor(scope: Construct, id: string, props: HelloCdkStackProps) {
super(scope, id, props)

const cachePolicy = new CachePolicy(this, "CachePolicy", {
cookieBehavior: CacheCookieBehavior.all(),
})

new AstroAWS(this, "AstroAWS", {
cdk: {
// This configures all subpaths of /api.
apiBehavior: {
cachePolicy,
},
// This configures everything excluding subpaths of /api.
cloudfrontDistribution: {
cachePolicy,
},
},
websiteDir: "../my-astro-project",
})
}
}
```

```
```
9 changes: 0 additions & 9 deletions apps/infra/src/lib/constants/environments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,26 +111,17 @@ const ENVIRONMENT_PROPS: Record<Environment, EnvironmentProps> = {
{
mode: "static",
package: "@astro-aws/docs",
<<<<<<< HEAD
=======
runtime: "nodejs20",
>>>>>>> stream
},
{
mode: "ssr",
package: "@astro-aws/examples-base",
<<<<<<< HEAD
=======
runtime: "nodejs20",
>>>>>>> stream
},
{
mode: "ssr-stream",
package: "@astro-aws/examples-base",
<<<<<<< HEAD
=======
runtime: "nodejs20",
>>>>>>> stream
},
],
},
Expand Down
3 changes: 3 additions & 0 deletions apps/infra/src/lib/stacks/website-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ class WebsiteStack extends Stack {
const astroAwsConstruct = new AstroAWS(this, "AstroAWSConstruct", {
cdk: {
cloudfrontDistribution: {
apiBehavior: {
cachePolicy,
},
certificate,
comment: environment,
defaultBehavior: {
Expand Down

0 comments on commit 6d2b1a6

Please sign in to comment.