forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(docs-infra): add canonical link to each adev page (angular#56540)
PR Close angular#56540
- Loading branch information
1 parent
49b2e65
commit e81abdb
Showing
4 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/*! | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.dev/license | ||
*/ | ||
|
||
import {TestBed} from '@angular/core/testing'; | ||
|
||
import {HeaderService} from './header.service'; | ||
import {link} from 'fs'; | ||
|
||
describe('HeaderService', () => { | ||
let service: HeaderService; | ||
|
||
beforeEach(() => { | ||
service = TestBed.inject(HeaderService); | ||
}); | ||
|
||
it('setCanonical', () => { | ||
// setCanonical assumes there is a preexisting element | ||
const linkEl = document.createElement('link'); | ||
linkEl.setAttribute('rel', 'canonical'); | ||
document.querySelector('head')?.appendChild(linkEl); | ||
|
||
service.setCanonical('/some/link'); | ||
expect(document.querySelector('link[rel=canonical]')!.getAttribute('href')).toBe( | ||
'https://angular.dev/some/link', | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import {DOCUMENT} from '@angular/common'; | ||
import {Injectable, inject} from '@angular/core'; | ||
|
||
const ANGULAR_DEV = 'https://angular.dev'; | ||
|
||
/** | ||
* Information about the deployment of this application. | ||
*/ | ||
@Injectable({providedIn: 'root'}) | ||
export class HeaderService { | ||
private readonly document = inject(DOCUMENT); | ||
|
||
/** | ||
* Sets the canonical link in the header. | ||
* It supposes the header link is already present in the index.html | ||
* | ||
* The function behave invariably and will always point to angular.dev, | ||
* no matter if it's a specific version build | ||
*/ | ||
setCanonical(absolutePath: string): void { | ||
const pathWithoutFragment = this.normalizePath(absolutePath).split('#')[0]; | ||
const fullPath = `${ANGULAR_DEV}/${pathWithoutFragment}`; | ||
this.document.querySelector('link[rel=canonical]')?.setAttribute('href', fullPath); | ||
} | ||
|
||
private normalizePath(path: string): string { | ||
if (path[0] === '/') { | ||
return path.substring(1); | ||
} | ||
return path; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters