Skip to content

Commit

Permalink
Merge pull request #201 from stevermeister/13.1.x
Browse files Browse the repository at this point in the history
merge angular universal docs to master
  • Loading branch information
pavankjadda authored Dec 9, 2021
2 parents 1db4cd1 + d447418 commit 2478749
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 8 deletions.
38 changes: 36 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Angular service to read, set and delete browser cookies. Originally based on the

> Note: `ViewEngine` support has been removed on 13.x.x. See [compatability matrix](https://github.com/stevermeister/ngx-cookie-service#supported-versions) for details
# Installation
## Installation

```bash
npm install ngx-cookie-service --save
Expand All @@ -27,6 +27,8 @@ npm install ngx-cookie-service --save
yarn add ngx-cookie-service
```

## Usage

Add the cookie service to your `app.module.ts` as a provider:

```typescript
Expand Down Expand Up @@ -57,13 +59,45 @@ cookieService: CookieService

That's it!

## Server Side Rendering

`ngx-cookie-service` supports Server Side Rendering (SSR) through Angular Universal. 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

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

with this

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

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

## Demo

https://stackblitz.com/edit/angular-ivy-1lrgdt?file=src%2Fapp%2Fapp.component.ts

## Supported Versions

`ViewEngine` support has been removed on 13.x.x. For Angular versions 13.x.x or later use the latest version of the library. For versions <=12.x.x, use 12.0.3 version
`ViewEngine` support has been removed on 13.x.x. For Angular versions 13.x.x or later use the latest version of the
library. For versions <=12.x.x, use 12.0.3 version

| Angular Version | Supported Version |
| ---------------------- | ----------------- |
Expand Down
42 changes: 37 additions & 5 deletions projects/ngx-cookie-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@

</p>

Angular service to read, set and delete browser cookies. The experienced team
behind [Studytube](https://www.studytube.nl/) will take care of our cookie service from now on.
Angular service to read, set and delete browser cookies. Originally based on the [ng2-cookies](https://www.npmjs.com/package/ng2-cookies) library. The experienced team behind [Studytube](https://www.studytube.nl/) will take care of our cookie service from now on.

> Note: `ViewEngine` support has been removed on 13.x.x. See [compatability matrix](https://github.com/stevermeister/ngx-cookie-service#supported-versions) for details
# Installation
## Installation

```bash
npm install ngx-cookie-service --save
Expand All @@ -28,6 +27,8 @@ npm install ngx-cookie-service --save
yarn add ngx-cookie-service
```

## Usage

Add the cookie service to your `app.module.ts` as a provider:

```typescript
Expand Down Expand Up @@ -58,6 +59,35 @@ cookieService: CookieService

That's it!

## Server Side Rendering

`ngx-cookie-service` supports Server Side Rendering (SSR) through Angular Universal. 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

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

with this

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

This will make sure the cookies are available in `REQUEST` object and you can then use `REQUEST.cookies` to access the cookies.
Then proceed to use `ngx-cookie-service` as usual.

## Demo

https://stackblitz.com/edit/angular-ivy-1lrgdt?file=src%2Fapp%2Fapp.component.ts
Expand Down Expand Up @@ -181,7 +211,9 @@ at [StackOverflow](https://stackoverflow.com/) for help.

## Do you support Angular Universal?

We are working on it. If you are interested in helping us out, see [PR](https://github.com/stevermeister/ngx-cookie-service/pull/177)
There is an [issue](https://github.com/7leads/ngx-cookie-service/issues/1) for that. Check
out [this comment](https://github.com/7leads/ngx-cookie-service/issues/1#issuecomment-361150174) for more information
about future support.

# Opening issues

Expand All @@ -206,7 +238,6 @@ the other cookie packages we found were either not designed "the Angular way" or

Thanks to all contributors:

- [pavankjadda](https://github.com/pavankjadda)
- [paroe](https://github.com/paroe)
- [CunningFatalist](https://github.com/CunningFatalist)
- [kthy](https://github.com/kthy)
Expand All @@ -218,6 +249,7 @@ Thanks to all contributors:
- [IceBreakerG](https://github.com/IceBreakerG)
- [rojedalopez](https://github.com/rojedalopez)
- [Nikel163](https://github.com/Nikel163)
- [pavankjadda](https://github.com/pavankjadda)

# License

Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-cookie-service/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ngx-cookie-service",
"description": "Angular cookie service",
"version": "13.0.1",
"version": "13.1.0-rc.1",
"license": "MIT",
"author": "Stepan Suvorov <[email protected]>",
"keywords": [
Expand Down

0 comments on commit 2478749

Please sign in to comment.