Path and domain
A cookie is only visible on pages under its path. The browser also treats cookies with the same name but a different path as separate cookies, so delete() must use the same path that set() used.
Try it:
- Click set() on
path: '/somewhere-else'. The cookie is written, but this page can’t see it. - Click set() on
path: '/', then delete() on the “no path” row. Nothing is deleted, because the paths don’t match. - Click delete() on
path: '/'.
import { ChangeDetectionStrategy, Component, inject, signal, WritableSignal } from '@angular/core';import { CookieService } from 'ngx-cookie-service';
interface PathCase { label: string; /** Cookie value, so the page can tell which of the cookies it sees. */ value: string; path: string | undefined;}
@Component({ selector: 'path-example', templateUrl: './path-example.component.html', changeDetection: ChangeDetectionStrategy.OnPush,})export class PathExampleComponent { private readonly cookieService: CookieService = inject(CookieService);
protected readonly cases: PathCase[] = [ { label: `path: '/'`, value: 'root', path: '/' }, { label: 'no path (browser default)', value: 'default', path: undefined }, { label: `path: '/somewhere-else'`, value: 'elsewhere', path: '/somewhere-else' }, ];
protected readonly log: WritableSignal<string[]> = signal([]); protected readonly cookies: WritableSignal<Record<string, string>> = signal(this.cookieService.getAll());
protected set(pathCase: PathCase): void { this.cookieService.set('docs-path', pathCase.value, { path: pathCase.path }); this.record(`set('docs-path', '${pathCase.value}', { path: ${this.literal(pathCase.path)} })`); }
protected delete(pathCase: PathCase): void { this.cookieService.delete('docs-path', { path: pathCase.path }); this.record(`delete('docs-path', { path: ${this.literal(pathCase.path)} })`); }
protected visibleValue(): string | undefined { return this.cookies()['docs-path']; }
private record(call: string): void { this.cookies.set(this.cookieService.getAll()); const value = this.visibleValue(); const outcome = value === undefined ? 'no docs-path cookie visible here' : `visible here: "${value}"`; this.log.update((lines) => [`${call} → ${outcome}`, ...lines].slice(0, 5)); }
private literal(path: string | undefined): string { return path === undefined ? 'undefined' : `'${path}'`; }}<table class="docs-table"> <thead> <tr> <th scope="col">Path option</th> <th scope="col"><span class="docs-visually-hidden">Actions</span></th> </tr> </thead> <tbody> @for (pathCase of cases; track pathCase.label) { <tr> <td> <code>{{ pathCase.label }}</code> </td> <td class="docs-cell-actions"> <button type="button" class="primary" (click)="set(pathCase)">set()</button> <button type="button" (click)="delete(pathCase)">delete()</button> </td> </tr> } </tbody></table>
<p aria-live="polite"> @if (visibleValue(); as value) { <code>get('docs-path')</code> on this page returns <code>"{{ value }}"</code>. } @else { <code>check('docs-path')</code> on this page returns <code>false</code>. }</p>
@if (log().length) { <pre class="docs-output">{{ log().join('\n') }}</pre>}Domain
Section titled “Domain”domain makes a cookie visible to subdomains. { domain: 'example.com' } set from app.example.com is also sent to api.example.com. Without domain, the cookie belongs to the current host only.
- If
pathordomaincontains;or a control character, the call is skipped with a console warning. That stops a value from injecting extra cookie attributes. - Set
path: '/'once for the whole app withprovideCookieOptions().