diff --git a/server.ts b/server.ts index 3e10677a8b1..bb8a0b34cf3 100644 --- a/server.ts +++ b/server.ts @@ -55,6 +55,7 @@ import { APP_CONFIG, AppConfig } from './src/config/app-config.interface'; import { extendEnvironmentWithAppConfig } from './src/config/config.util'; import { logStartupMessage } from './startup-message'; import { TOKENITEM } from 'src/app/core/auth/models/auth-token-info.model'; +import { SsrExcludePatterns } from './src/config/universal-config.interface'; /* @@ -228,7 +229,7 @@ export function app() { * The callback function to serve server side angular */ function ngApp(req, res) { - if (environment.universal.preboot) { + if (environment.universal.preboot && req.method === 'GET' && (req.path === '/' || !isExcludedFromSsr(req.path, environment.universal.excludePathPatterns))) { // Render the page to user via SSR (server side rendering) serverSideRender(req, res); } else { @@ -545,6 +546,21 @@ function start() { } } +/** + * Check if SSR should be skipped for path + * + * @param path + * @param excludePathPattern + */ +function isExcludedFromSsr(path: string, excludePathPattern: SsrExcludePatterns[]): boolean { + const patterns = excludePathPattern.map(p => + new RegExp(p.pattern, p.flag || '') + ); + return patterns.some((regex) => { + return regex.test(path) + }); +} + /* * The callback function to serve health check requests */ diff --git a/src/config/universal-config.interface.ts b/src/config/universal-config.interface.ts index c088dcd6579..90958e0c182 100644 --- a/src/config/universal-config.interface.ts +++ b/src/config/universal-config.interface.ts @@ -1,7 +1,17 @@ import { Config } from './config.interface'; +export interface SsrExcludePatterns { + pattern: string | RegExp; + flag?: string; +} + export interface UniversalConfig extends Config { preboot: boolean; async: boolean; time: boolean; + + /** + * Patterns to be used as regexes to match url's path and check if SSR is disabled for it. + */ + excludePathPatterns: SsrExcludePatterns[]; } diff --git a/src/environments/environment.production.ts b/src/environments/environment.production.ts index 09b5f19ade6..544066431cd 100644 --- a/src/environments/environment.production.ts +++ b/src/environments/environment.production.ts @@ -7,6 +7,25 @@ export const environment: Partial = { universal: { preboot: true, async: true, - time: false + time: false, + excludePathPatterns: [ + { + pattern: '^/communities/[a-f0-9-]{36}/browse(/.*)?$', + flag: 'i', + }, + { + pattern: '^/collections/[a-f0-9-]{36}/browse(/.*)?$', + flag: 'i', + }, + { pattern: '^/browse/' }, + { pattern: '^/search' }, + { pattern: '^/community-list$' }, + { pattern: '^/statistics/?' }, + { pattern: '^/admin/' }, + { pattern: '^/processes/?' }, + { pattern: '^/notifications/' }, + { pattern: '^/access-control/' }, + { pattern: '^/health$' }, + ], } }; diff --git a/src/environments/environment.test.ts b/src/environments/environment.test.ts index 0bb36da61f3..abc19b23410 100644 --- a/src/environments/environment.test.ts +++ b/src/environments/environment.test.ts @@ -10,7 +10,26 @@ export const environment: BuildConfig = { universal: { preboot: true, async: true, - time: false + time: false, + excludePathPatterns: [ + { + pattern: '^/communities/[a-f0-9-]{36}/browse(/.*)?$', + flag: 'i', + }, + { + pattern: '^/collections/[a-f0-9-]{36}/browse(/.*)?$', + flag: 'i', + }, + { pattern: '^/browse/' }, + { pattern: '^/search' }, + { pattern: '^/community-list$' }, + { pattern: '^/statistics/?' }, + { pattern: '^/admin/' }, + { pattern: '^/processes/?' }, + { pattern: '^/notifications/' }, + { pattern: '^/access-control/' }, + { pattern: '^/health$' }, + ], }, // Angular Universal server settings. diff --git a/src/environments/environment.ts b/src/environments/environment.ts index 67a586718de..6f3b8751e9c 100644 --- a/src/environments/environment.ts +++ b/src/environments/environment.ts @@ -12,7 +12,26 @@ export const environment: Partial = { universal: { preboot: false, async: true, - time: false + time: false, + excludePathPatterns: [ + { + pattern: '^/communities/[a-f0-9-]{36}/browse(/.*)?$', + flag: 'i', + }, + { + pattern: '^/collections/[a-f0-9-]{36}/browse(/.*)?$', + flag: 'i', + }, + { pattern: '^/browse/' }, + { pattern: '^/search' }, + { pattern: '^/community-list$' }, + { pattern: '^/statistics/?' }, + { pattern: '^/admin/' }, + { pattern: '^/processes/?' }, + { pattern: '^/notifications/' }, + { pattern: '^/access-control/' }, + { pattern: '^/health$' }, + ], }, // The REST API server settings.