66 * http://www.dspace.org/license/
77 */
88/* eslint-disable max-classes-per-file */
9- import { Injectable } from '@angular/core' ;
9+ import { Inject , Injectable , Optional , PLATFORM_ID } from '@angular/core' ;
1010import { Router } from '@angular/router' ;
1111import { Observable } from 'rxjs' ;
1212import { tap } from 'rxjs/operators' ;
@@ -21,6 +21,8 @@ import { getFirstCompletedRemoteData } from '../shared/operators';
2121import { DSpaceObject } from '../shared/dspace-object.model' ;
2222import { IdentifiableDataService } from './base/identifiable-data.service' ;
2323import { getDSORoute } from '../../app-routing-paths' ;
24+ import { RESPONSE } from '@nguniversal/express-engine/tokens' ;
25+ import { isPlatformServer } from '@angular/common' ;
2426
2527const ID_ENDPOINT = 'pid' ;
2628const UUID_ENDPOINT = 'dso' ;
@@ -75,12 +77,20 @@ export class DsoRedirectService {
7577 protected objectCache : ObjectCacheService ,
7678 protected halService : HALEndpointService ,
7779 private router : Router ,
80+ @Optional ( ) @Inject ( RESPONSE ) private response : any ,
81+ @Inject ( PLATFORM_ID ) private platformId : any
7882 ) {
7983 this . dataService = new DsoByIdOrUUIDDataService ( requestService , rdbService , objectCache , halService ) ;
8084 }
8185
8286 /**
83- * Retrieve a DSpaceObject by
87+ * Redirect to a DSpaceObject's path using the given identifier type and ID.
88+ * This is used to redirect paths like "/handle/[prefix]/[suffix]" to the object's path (e.g. /items/[uuid]).
89+ * See LookupGuard for more examples.
90+ *
91+ * If this is called server side (via SSR), it performs a 301 Redirect.
92+ * If this is called client side (via CSR), it simply uses the Angular router to do the redirect.
93+ *
8494 * @param id the identifier of the object to retrieve
8595 * @param identifierType the type of the given identifier (defaults to UUID)
8696 */
@@ -94,7 +104,12 @@ export class DsoRedirectService {
94104 if ( hasValue ( dso . uuid ) ) {
95105 let newRoute = getDSORoute ( dso ) ;
96106 if ( hasValue ( newRoute ) ) {
97- this . router . navigate ( [ newRoute ] ) ;
107+ // If running via SSR, perform a "301 Moved Permanently" redirect for SEO purposes.
108+ if ( isPlatformServer ( this . platformId ) ) {
109+ this . response . redirect ( 301 , newRoute ) ;
110+ } else {
111+ this . router . navigate ( [ newRoute ] ) ;
112+ }
98113 }
99114 }
100115 }
0 commit comments