Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(router): add cookies example #775

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions apps/docs-app/docs/features/api/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,48 @@ export default defineEventHandler((event) => {
});
```

## Accessing Cookies

Analog allows setting cookies reading them in your server side calls.
brandonroberts marked this conversation as resolved.
Show resolved Hide resolved

### Setting cookies

```ts
//(home).server.ts
import { setCookie } from 'h3';
import { PageServerLoad } from '@analogjs/router';

import { Product } from '../products';

export const load = async ({ fetch, event }: PageServerLoad) => {
setCookie(event, 'test', 'test'); // setting the cookie
santoshyadavdev marked this conversation as resolved.
Show resolved Hide resolved
const products = await fetch<Product[]>('/api/v1/products');

return {
products: products,
};
};
```

### Reading cookies

```ts
//index.server.ts
import { parseCookies } from 'h3';
import { PageServerLoad } from '@analogjs/router';

export const load = async ({ event }: PageServerLoad) => {
console.log('shipping');
santoshyadavdev marked this conversation as resolved.
Show resolved Hide resolved
const cookies = parseCookies(event);

console.log('test cookie', cookies['test']);
santoshyadavdev marked this conversation as resolved.
Show resolved Hide resolved

return {
shipping: true,
};
};
```

## More Info

API routes are powered by [Nitro](https://nitro.unjs.io). See the Nitro docs for more examples around building API routes.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Accessing the data fetched on the server can be done using the `injectLoad` func
// src/app/pages/index.page.ts
import { Component } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { LoadResult, injectLoad } from '@analogjs/router';
import { injectLoad } from '@analogjs/router';

import { load } from './index.server'; // not included in client build

Expand Down Expand Up @@ -85,7 +85,7 @@ Now to get the data in the component add an input called `load`.
```ts
// src/app/pages/index.page.ts
import { Component } from '@angular/core';
import { LoadResult, injectLoad } from '@analogjs/router';
import { LoadResult } from '@analogjs/router';

import { load } from './index.server'; // not included in client build

Expand Down