Skip to content

Commit 387d84c

Browse files
[IIIF-188] refactor, set header also on redirect page
1 parent f9d8bcf commit 387d84c

6 files changed

Lines changed: 20 additions & 5 deletions

File tree

server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ const _window = domino.createWindow(indexHtml);
8888
// The REST server base URL
8989
const REST_BASE_URL = environment.rest.ssrBaseUrl || environment.rest.baseUrl;
9090

91-
const IIIF_ALLOWED_ORIGINS = environment.mirador.allowedOrigins;
91+
const IIIF_ALLOWED_ORIGINS = environment.rest.allowedOrigins || [];
9292

9393
// Assign the DOM window and document objects to the global object
9494
(_window as any).screen = {deviceXDPI: 0, logicalXDPI: 0};

src/app/core/services/server-hard-redirect.service.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens';
44
import { HardRedirectService } from './hard-redirect.service';
55
import { APP_CONFIG, AppConfig } from '../../../config/app-config.interface';
66
import { isNotEmpty } from '../../shared/empty.util';
7+
import { ServerResponseService } from './server-response.service';
78

89
/**
910
* Service for performing hard redirects within the server app module
@@ -15,6 +16,7 @@ export class ServerHardRedirectService extends HardRedirectService {
1516
@Inject(APP_CONFIG) protected appConfig: AppConfig,
1617
@Inject(REQUEST) protected req: Request,
1718
@Inject(RESPONSE) protected res: Response,
19+
private responseService: ServerResponseService,
1820
) {
1921
super();
2022
}
@@ -59,8 +61,10 @@ export class ServerHardRedirectService extends HardRedirectService {
5961

6062
console.info(`Redirecting from ${this.req.url} to ${redirectUrl} with ${status}`);
6163

64+
this.setCorsHeader();
65+
6266
this.res.redirect(status, redirectUrl);
63-
this.res.end();
67+
//this.res.end();
6468
// I haven't found a way to correctly stop Angular rendering.
6569
// So we just let it end its work, though we have already closed
6670
// the response.
@@ -83,4 +87,17 @@ export class ServerHardRedirectService extends HardRedirectService {
8387
getCurrentOrigin(): string {
8488
return this.req.protocol + '://' + this.req.headers.host;
8589
}
90+
91+
/**
92+
* Set CORS header to allow embedding of redirected content
93+
*/
94+
setCorsHeader() {
95+
const currentOrigin = this.getCurrentOrigin();
96+
const allowedOrigins = this.appConfig.rest.allowedOrigins;
97+
98+
if (currentOrigin && allowedOrigins?.length && allowedOrigins.includes(currentOrigin)) {
99+
console.info('Setting cors header for origin ', currentOrigin);
100+
this.responseService.setHeader('Access-Control-Allow-Origin', currentOrigin);
101+
}
102+
}
86103
}

src/config/default-app-config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,6 @@ export class DefaultAppConfig implements AppConfig {
816816

817817
mirador: MiradorConfig = {
818818
enableDownloadPlugin: true,
819-
allowedOrigins: []
820819
};
821820

822821
loader: LoaderConfig = {

src/config/mirador-config.interfaces.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ import { Config } from './config.interface';
22

33
export interface MiradorConfig extends Config {
44
enableDownloadPlugin: boolean;
5-
allowedOrigins: string[];
65
}

src/config/server-config.interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ export class ServerConfig implements Config {
1010
// This boolean will be automatically set on server startup based on whether "baseUrl" and "ssrBaseUrl"
1111
// have different values.
1212
public hasSsrBaseUrl?: boolean;
13+
public allowedOrigins?: string[];
1314
}

src/environments/environment.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,6 @@ export const environment: BuildConfig = {
603603

604604
mirador: {
605605
enableDownloadPlugin: true,
606-
allowedOrigins: []
607606
},
608607

609608
loader: {

0 commit comments

Comments
 (0)