Skip to content
Merged
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
15 changes: 12 additions & 3 deletions docs/src/app/shared/header-tag-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,25 @@ export class HeaderTagManager {
private readonly _document = inject(DOCUMENT);

/**
* Sets the canonical link in the header.
* It supposes the header link is already present in the index.html
* Sets the canonical link in the header. If the link already exists,
* it will be updated. Otherwise, a new link will be created and inserted.
*
* 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 = `${MAT_ANGULAR_DEV}/${pathWithoutFragment}`;
this._document.querySelector('link[rel=canonical]')?.setAttribute('href', fullPath);
let canonicalLink = this._document.querySelector<HTMLLinkElement>('link[rel=canonical]');

if (canonicalLink) {
canonicalLink.setAttribute('href', fullPath);
} else {
canonicalLink = this._document.createElement('link');
canonicalLink.setAttribute('rel', 'canonical');
canonicalLink.setAttribute('href', fullPath);
this._document.head.appendChild(canonicalLink);
}
}

private _normalizePath(path: string): string {
Expand Down
1 change: 0 additions & 1 deletion docs/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<head>
<meta charset="utf-8">
<title>Angular Material UI Component Library</title>
<link rel="canonical" href="https://material.angular.dev">
<base href="/">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Expand Down
Loading