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

feat(ssr): adds server cookie output via response #315

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
40 changes: 19 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,35 +94,33 @@ Only install `ngx-cookie-service-ssr` library (and skip `ngx-cookie-service`) fo

2. By default, browser cookies are not
available in SSR because `document` object is not available. To overcome this, navigate to `server.ts` file in your
SSR
project, and replace the following code

SSR project, and add the provided exported tokens to the following code's providers array (also applicable to `commonEngine.render` providers):

```typescript
server.get('*', (req, res) => {
res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
});
```

with this
like so:

```typescript
import { REQUEST as SSR_REQUEST } from "ngx-cookie-service-ssr";

server.get('*', (req, res) => {
res.render(indexHtml, {
req,
providers: [
{ provide: APP_BASE_HREF, useValue: req.baseUrl },
{ provide: SSR_REQUEST, useValue: req },
{ provide: 'RESPONSE', useValue: res },
],
});
});
```
```typescript
import { REQUEST as COOKIE_SERVICE_REQ, RESPONSE as COOKIE_SERVICE_RES } from '@ngx-cookie-service-ssr';
// ...
server.get('*', (req, res) => {
res.render(indexHtml, {
req,
providers: [
{ provide: APP_BASE_HREF, useValue: req.baseUrl },
{ provide: COOKIE_SERVICE_REQ, useValue: req },
{ provide: COOKIE_SERVICE_RES, useValue: res },
],
});
});
```

3. This will make sure the cookies are available in `REQUEST` object, and the `ngx-cookie-service-ssr` can
use `REQUEST.cookies` to access the
cookies in SSR. Then proceed to use `ngx-cookie-service` as usual.
3. This will ensure cookies are available in the `REQUEST` and `RESPONSE` objects for `ngx-cookie-service-ssr` to access
via `REQUEST.cookies`/`RESPONSE.cookies` in SSR. Then proceed to use `ngx-cookie-service` as usual.
4. See the [sample repo](https://github.com/pavankjadda/angular-ssr-docker) for more details.

## Supported Versions
Expand Down
Loading