2018-11-14 11:38:53 +01:00
|
|
|
import { Injectable, Inject } from '@angular/core';
|
|
|
|
import { DOCUMENT } from '@angular/common';
|
2019-02-11 16:03:38 +01:00
|
|
|
import { Optional, RendererFactory2, ViewEncapsulation } from '@angular/core';
|
2018-11-14 11:38:53 +01:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class SEOService {
|
2019-02-11 16:03:38 +01:00
|
|
|
constructor( @Inject(DOCUMENT) private doc,
|
|
|
|
private rendererFactory: RendererFactory2,
|
|
|
|
@Inject(DOCUMENT) private document) {
|
2018-11-14 11:38:53 +01:00
|
|
|
}
|
|
|
|
|
2019-02-11 16:03:38 +01:00
|
|
|
createLinkForCanonicalURL(url:string, addParameters:boolean=true) {
|
|
|
|
if(this.doc && (typeof this.doc.getElementById === "function" || typeof this.doc.createElement === "function") ){
|
2019-06-03 10:53:57 +02:00
|
|
|
if(!addParameters && url && url.indexOf("?") != -1){
|
|
|
|
url = url.substring(0,url.indexOf("?"));
|
2018-11-14 11:38:53 +01:00
|
|
|
}
|
|
|
|
if (typeof this.doc.getElementById === "function") {
|
|
|
|
let currentLink: HTMLLinkElement = this.doc.getElementById("relcan");
|
|
|
|
if(currentLink ){
|
|
|
|
currentLink.setAttribute('href', url);
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (typeof this.doc.createElement === "function") {
|
|
|
|
let link: HTMLLinkElement = this.doc.createElement('link');
|
|
|
|
link.setAttribute('id', 'relcan');
|
|
|
|
link.setAttribute('rel', 'canonical');
|
|
|
|
this.doc.head.appendChild(link);
|
|
|
|
link.setAttribute('href', url);
|
|
|
|
}
|
|
|
|
|
2019-02-11 16:03:38 +01:00
|
|
|
}else{
|
|
|
|
try {
|
|
|
|
const renderer = this.rendererFactory.createRenderer(this.document, {
|
|
|
|
id: '-1',
|
|
|
|
encapsulation: ViewEncapsulation.None,
|
|
|
|
styles: [],
|
|
|
|
data: {}
|
|
|
|
});
|
|
|
|
|
|
|
|
const link = renderer.createElement('link');
|
|
|
|
const head = this.document.head;
|
|
|
|
|
|
|
|
if (head === null) {
|
|
|
|
throw new Error('<head> not found within DOCUMENT.');
|
|
|
|
}
|
|
|
|
renderer.setAttribute(link, "rel", "canonical");
|
2019-02-15 16:03:48 +01:00
|
|
|
renderer.setAttribute(link, "href", (addParameters)?url:url.split("?")[0]);
|
2019-02-11 16:03:38 +01:00
|
|
|
renderer.setAttribute(link, "id", "relcan");
|
|
|
|
// [TODO]: get them to update the existing one (if it exists) ?
|
|
|
|
renderer.appendChild(head, link);
|
|
|
|
} catch (e) {
|
|
|
|
console.error('Error within linkService : ', e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 11:38:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-02-11 16:03:38 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* -- LinkService -- [Temporary]
|
|
|
|
* @MarkPieszak
|
|
|
|
*
|
|
|
|
* Similar to Meta service but made to handle <link> creation for SEO purposes
|
|
|
|
* -- NOTE: Soon there will be an overall DocumentService within Angular that handles Meta/Link everything
|
|
|
|
*/
|
|
|
|
//
|
|
|
|
// import { Injectable, Optional, RendererFactory2, ViewEncapsulation, Inject } from '@angular/core';
|
|
|
|
// import { DOCUMENT } from '@angular/platform-browser';
|
|
|
|
//
|
|
|
|
// @Injectable()
|
|
|
|
// export class LinkService {
|
|
|
|
//
|
|
|
|
// constructor(
|
|
|
|
// private rendererFactory: RendererFactory2,
|
|
|
|
// @Inject(DOCUMENT) private document
|
|
|
|
// ) {
|
|
|
|
// }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inject the State into the bottom of the <head>
|
|
|
|
*/
|
|
|
|
// addTag(tag: LinkDefinition, forceCreation?: boolean) {
|
|
|
|
//
|
|
|
|
// try {
|
|
|
|
// const renderer = this.rendererFactory.createRenderer(this.document, {
|
|
|
|
// id: '-1',
|
|
|
|
// encapsulation: ViewEncapsulation.None,
|
|
|
|
// styles: [],
|
|
|
|
// data: {}
|
|
|
|
// });
|
|
|
|
//
|
|
|
|
// const link = renderer.createElement('link');
|
|
|
|
// const head = this.document.head;
|
|
|
|
// const selector = this._parseSelector(tag);
|
|
|
|
//
|
|
|
|
// if (head === null) {
|
|
|
|
// throw new Error('<head> not found within DOCUMENT.');
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Object.keys(tag).forEach((prop: string) => {
|
|
|
|
// return renderer.setAttribute(link, prop, tag[prop]);
|
|
|
|
// });
|
|
|
|
//
|
|
|
|
// // [TODO]: get them to update the existing one (if it exists) ?
|
|
|
|
// renderer.appendChild(head, link);
|
|
|
|
//
|
|
|
|
// } catch (e) {
|
|
|
|
// console.error('Error within linkService : ', e);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// private _parseSelector(tag: LinkDefinition): string {
|
|
|
|
// // Possibly re-work this
|
|
|
|
// const attr: string = tag.rel ? 'rel' : 'hreflang';
|
|
|
|
// return `${attr}="${tag[attr]}"`;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
export declare type LinkDefinition = {
|
|
|
|
charset?: string;
|
|
|
|
crossorigin?: string;
|
|
|
|
href?: string;
|
|
|
|
hreflang?: string;
|
|
|
|
media?: string;
|
|
|
|
rel?: string;
|
|
|
|
rev?: string;
|
|
|
|
sizes?: string;
|
|
|
|
target?: string;
|
|
|
|
type?: string;
|
|
|
|
} & {
|
|
|
|
[prop: string]: string;
|
|
|
|
};
|