Skip to content

Commit 203b777

Browse files
committed
Merge branch 'master' into add-ssr
# Conflicts: # package-lock.json # package.json
2 parents 067cb34 + 9e3914e commit 203b777

File tree

3 files changed

+105
-54
lines changed

3 files changed

+105
-54
lines changed

README.md

+72-24
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ yarn add ngx-cookie-service
2828
```
2929

3030
## Usage
31-
3231
Add the cookie service to your `app.module.ts` as a provider:
3332

3433
```typescript
@@ -58,37 +57,86 @@ cookieService: CookieService
5857
```
5958

6059
That's it!
61-
62-
## Server Side Rendering (Coming Soon)
63-
64-
`ngx-cookie-service` supports Server Side Rendering (SSR) through Angular Universal. By default, browser cookies are not
60+
### Angular 14+
61+
1. Angular 14 introduced support for standalone components.
62+
If you are using just standalone components, you can import the service directly into the component
63+
```typescript
64+
import {CookieService} from 'ngx-cookie-service';
65+
import {Component} from '@angular/core';
66+
67+
@Component({
68+
selector: 'my-component',
69+
template: `<h1>Hello World</h1>`,
70+
providers: [CookieService]
71+
})
72+
73+
export class HelloComponent {
74+
constructor(private cookieService: CookieService) {
75+
this.cookieService.set('Test', 'Hello World');
76+
this.cookieValue = this.cookieService.get('Test');
77+
}
78+
}
79+
```
80+
2. You can also use `inject()` method in v14+ to inject the service into the component
81+
```typescript
82+
import {CookieService} from 'ngx-cookie-service';
83+
import {Component,inject} from '@angular/core';
84+
85+
@Component({
86+
selector: 'my-component',
87+
template: `<h1>Hello World</h1>`,
88+
providers: [CookieService]
89+
})
90+
91+
export class HelloComponent {
92+
cookieService=inject(CookieService);
93+
94+
constructor() {
95+
this.cookieService.set('Test', 'Hello World');
96+
this.cookieValue = this.cookieService.get('Test');
97+
}
98+
}
99+
```
100+
## Server Side Rendering (Beta)
101+
Ngx Cookie Service supports Server Side Rendering (SSR) via dedicated library [ngx-cookie-service-ssr](https://www.npmjs.com/package/ngx-cookie-service-ssr).
102+
103+
> Note: SSR support is in beta via dedicated library [ngx-cookie-service-ssr](https://www.npmjs.com/package/ngx-cookie-service-ssr). Please test and let us know if you find any issues.
104+
105+
1. Install the library using below command
106+
```shell
107+
npm install ngx-cookie-service-ssr --save
108+
109+
# or
110+
111+
yarn add ngx-cookie-service-ssr
112+
```
113+
2. By default, browser cookies are not
65114
available in SSR because `document` object is not available. To overcome this, navigate to `server.ts` file in your SSR
66115
project, and replace the following code
67-
68-
```typescript
69-
server.get('*', (req, res) => {
70-
res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
71-
});
72-
```
73-
116+
117+
```typescript
118+
server.get('*', (req, res) => {
119+
res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
120+
});
121+
```
74122
with this
75123

76124
```typescript
77125
server.get('*', (req, res) => {
78-
res.render(indexHtml, {
79-
req,
80-
providers: [
81-
{ provide: APP_BASE_HREF, useValue: req.baseUrl },
82-
{ provide: 'REQUEST', useValue: req },
83-
{ provide: 'RESPONSE', useValue: res },
84-
],
85-
});
86-
});
126+
res.render(indexHtml, {
127+
req,
128+
providers: [
129+
{ provide: APP_BASE_HREF, useValue: req.baseUrl },
130+
{ provide: 'REQUEST', useValue: req },
131+
{ provide: 'RESPONSE', useValue: res },
132+
],
133+
});
134+
});
87135
```
88136

89-
This will make sure the cookies are available in `REQUEST` object, and the `ngx-cookie-service` can use `REQUEST.cookies` to access the
90-
cookies in SSR. Then proceed to use `ngx-cookie-service` as usual. See
91-
the [sample repo](https://github.com/pavankjadda/angular-ssr-docker) for more details.
137+
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
138+
cookies in SSR. Then proceed to use `ngx-cookie-service` as usual.
139+
4. See the [sample repo](https://github.com/pavankjadda/angular-ssr-docker) for more details.
92140

93141
## Demo
94142

angular.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@
3939
"lint": {
4040
"builder": "@angular-eslint/builder:lint",
4141
"options": {
42-
"lintFilePatterns": ["projects/ngx-cookie-service/**/*.ts", "projects/ngx-cookie-service/**/*.html"]
42+
"lintFilePatterns": [
43+
"projects/ngx-cookie-service/**/*.ts",
44+
"projects/ngx-cookie-service/**/*.html"
45+
]
4346
}
4447
}
4548
}

package.json

+29-29
Original file line numberDiff line numberDiff line change
@@ -19,49 +19,49 @@
1919
},
2020
"private": true,
2121
"dependencies": {
22-
"@angular/animations": "^14.0.0",
23-
"@angular/common": "^14.0.0",
24-
"@angular/compiler": "^14.0.0",
25-
"@angular/core": "^14.0.0",
26-
"@angular/forms": "^14.0.0",
27-
"@angular/platform-browser": "^14.0.0",
28-
"@angular/platform-browser-dynamic": "^14.0.0",
29-
"@angular/router": "^14.0.0",
22+
"@angular/animations": "^14.0.3",
23+
"@angular/common": "^14.0.3",
24+
"@angular/compiler": "^14.0.3",
25+
"@angular/core": "^14.0.3",
26+
"@angular/forms": "^14.0.3",
27+
"@angular/platform-browser": "^14.0.3",
28+
"@angular/platform-browser-dynamic": "^14.0.3",
29+
"@angular/router": "^14.0.3",
3030
"@nguniversal/express-engine": "^14.0.0",
3131
"rxjs": "^7.5.5",
3232
"tslib": "^2.4.0",
3333
"zone.js": "^0.11.5"
3434
},
3535
"devDependencies": {
36-
"@angular-devkit/build-angular": "^14.0.0",
37-
"@angular-eslint/builder": "13.2.1",
38-
"@angular-eslint/eslint-plugin": "13.2.1",
39-
"@angular-eslint/eslint-plugin-template": "13.2.1",
40-
"@angular-eslint/schematics": "13.2.1",
41-
"@angular-eslint/template-parser": "13.2.1",
42-
"@angular/cli": "^14.0.0",
43-
"@angular/compiler-cli": "^14.0.0",
44-
"@angular/language-service": "^14.0.0",
45-
"prettier": "^2.6.2",
36+
"@angular-devkit/build-angular": "^14.0.3",
37+
"@angular-eslint/builder": "14.0.0",
38+
"@angular-eslint/eslint-plugin": "14.0.0",
39+
"@angular-eslint/eslint-plugin-template": "14.0.0",
40+
"@angular-eslint/schematics": "14.0.0",
41+
"@angular-eslint/template-parser": "14.0.0",
42+
"@angular/cli": "^14.0.3",
43+
"@angular/compiler-cli": "^14.0.3",
44+
"@angular/language-service": "^14.0.3",
45+
"prettier": "^2.7.1",
4646
"@types/express": "^4.17.13",
4747
"@types/jasmine": "^4.0.3",
4848
"@types/jasminewd2": "^2.0.10",
49-
"@types/node": "^17.0.33",
50-
"@typescript-eslint/eslint-plugin": "5.23.0",
51-
"@typescript-eslint/parser": "5.23.0",
52-
"eslint": "^8.15.0",
49+
"@types/node": "^18.0.0",
50+
"@typescript-eslint/eslint-plugin": "^5.29.0",
51+
"@typescript-eslint/parser": "^5.29.0",
52+
"eslint": "^8.18.0",
5353
"eslint-plugin-import": "2.26.0",
5454
"eslint-plugin-jsdoc": "39.2.9",
5555
"eslint-plugin-prefer-arrow": "1.2.3",
56-
"jasmine-core": "^4.1.1",
56+
"jasmine-core": "^4.2.0",
5757
"jasmine-spec-reporter": "^7.0.0",
58-
"karma": "^6.3.20",
58+
"karma": "^6.4.0",
5959
"karma-chrome-launcher": "^3.1.1",
6060
"karma-coverage": "^2.2.0",
61-
"karma-jasmine": "^5.0.1",
62-
"karma-jasmine-html-reporter": "^1.7.0",
63-
"ng-packagr": "^14.0.0",
64-
"ts-node": "^10.7.0",
65-
"typescript": "^4.7.3"
61+
"karma-jasmine": "^5.1.0",
62+
"karma-jasmine-html-reporter": "^2.0.0",
63+
"ng-packagr": "^14.0.2",
64+
"ts-node": "^10.8.1",
65+
"typescript": "^4.7.4"
6666
}
6767
}

0 commit comments

Comments
 (0)