[DURACOM-344] Adapt SSR page filtering mechanism to a not allowed list

This commit is contained in:
FrancescoMolinaro
2025-04-22 16:16:27 +02:00
parent fbbf16f387
commit ce0f6153b9
6 changed files with 48 additions and 10 deletions

View File

@@ -23,10 +23,17 @@ universal:
# Determining which styles are critical is a relatively expensive operation; this option is # Determining which styles are critical is a relatively expensive operation; this option is
# disabled (false) by default to boost server performance at the expense of loading smoothness. # disabled (false) by default to boost server performance at the expense of loading smoothness.
inlineCriticalCss: false inlineCriticalCss: false
# Path prefixes to enable SSR for. By default these are limited to paths of primary DSpace objects. # Regexes to be run against the path of the page to check if SSR is allowed.
# NOTE: The "/handle/" path ensures Handle redirects work via SSR. The "/reload/" path ensures # If the path match any of the regexes it will be served directly in CSR.
# hard refreshes (e.g. after login) trigger SSR while fully reloading the page. # By default, excludes community and collection browse, global browse, global search, community list, and statistics.
paths: [ '/home', '/items/', '/entities/', '/collections/', '/communities/', '/bitstream/', '/bitstreams/', '/handle/', '/reload/' ] excludePathRegexes: [
/^\/communities\/[ 0-9a-f ]{ 8 }-[ 0-9a-f ]{ 4 }-[ 0-9a-f ]{ 4 }-[ 0-9a-f ]{ 4 }-[ 0-9a-f ]{ 12 }\/browse(\/.*)?$/i,
/^\/collections\/[ 0-9a-f ]{ 8 }-[ 0-9a-f ]{ 4 }-[ 0-9a-f ]{ 4 }-[ 0-9a-f ]{ 4 }-[ 0-9a-f ]{ 12 }\/browse(\/.*)?$/i,
/^\/browse\//,
/^\/search$/,
/^\/community-list$/,
/^\/statistics$/,
]
# Whether to enable rendering of Search component on SSR. # Whether to enable rendering of Search component on SSR.
# If set to true the component will be included in the HTML returned from the server side rendering. # If set to true the component will be included in the HTML returned from the server side rendering.
# If set to false the component will not be included in the HTML returned from the server side rendering. # If set to false the component will not be included in the HTML returned from the server side rendering.

View File

@@ -241,7 +241,7 @@ export function app() {
* The callback function to serve server side angular * The callback function to serve server side angular
*/ */
function ngApp(req, res) { function ngApp(req, res) {
if (environment.universal.preboot && req.method === 'GET' && (req.path === '/' || environment.universal.paths.some(pathPrefix => req.path.startsWith(pathPrefix)))) { if (environment.universal.preboot && req.method === 'GET' && (req.path === '/' || !isExcludedFromSsr(req.path, environment.universal.excludePathRegexes))) {
// Render the page to user via SSR (server side rendering) // Render the page to user via SSR (server side rendering)
serverSideRender(req, res); serverSideRender(req, res);
} else { } else {
@@ -625,6 +625,16 @@ function start() {
} }
} }
/**
* Check if SSR should be skipped for path
*
* @param path
* @param excludePathRegexes
*/
function isExcludedFromSsr(path: string, excludePathRegexes: RegExp[]): boolean {
return excludePathRegexes.some((regex) => regex.test(path));
}
/* /*
* The callback function to serve health check requests * The callback function to serve health check requests
*/ */

View File

@@ -32,9 +32,9 @@ export interface UniversalConfig extends Config {
replaceRestUrl: boolean; replaceRestUrl: boolean;
/** /**
* Paths to enable SSR for. Defaults to the home page and paths in the sitemap. * Regexes to match url's path and check if SSR is disabled for it.
*/ */
paths: Array<string>; excludePathRegexes: RegExp[];
/** /**
* Whether to enable rendering of search component on SSR * Whether to enable rendering of search component on SSR

View File

@@ -11,7 +11,14 @@ export const environment: Partial<BuildConfig> = {
inlineCriticalCss: false, inlineCriticalCss: false,
transferState: true, transferState: true,
replaceRestUrl: true, replaceRestUrl: true,
paths: [ '/home', '/items/', '/entities/', '/collections/', '/communities/', '/bitstream/', '/bitstreams/', '/handle/', '/reload/' ], excludePathRegexes: [
/^\/communities\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\/browse(\/.*)?$/i,
/^\/collections\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\/browse(\/.*)?$/i,
/^\/browse\//,
/^\/search$/,
/^\/community-list$/,
/^\/statistics$/,
],
enableSearchComponent: false, enableSearchComponent: false,
enableBrowseComponent: false, enableBrowseComponent: false,
}, },

View File

@@ -14,7 +14,14 @@ export const environment: BuildConfig = {
inlineCriticalCss: false, inlineCriticalCss: false,
transferState: true, transferState: true,
replaceRestUrl: false, replaceRestUrl: false,
paths: [ '/home', '/items/', '/entities/', '/collections/', '/communities/', '/bitstream/', '/bitstreams/', '/handle/', '/reload/' ], excludePathRegexes: [
/^\/communities\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\/browse(\/.*)?$/i,
/^\/collections\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\/browse(\/.*)?$/i,
/^\/browse\//,
/^\/search$/,
/^\/community-list$/,
/^\/statistics$/,
],
enableSearchComponent: false, enableSearchComponent: false,
enableBrowseComponent: false, enableBrowseComponent: false,
}, },

View File

@@ -16,7 +16,14 @@ export const environment: Partial<BuildConfig> = {
inlineCriticalCss: false, inlineCriticalCss: false,
transferState: true, transferState: true,
replaceRestUrl: false, replaceRestUrl: false,
paths: [ '/home', '/items/', '/entities/', '/collections/', '/communities/', '/bitstream/', '/bitstreams/', '/handle/', '/reload/' ], excludePathRegexes: [
/^\/communities\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\/browse(\/.*)?$/i,
/^\/collections\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\/browse(\/.*)?$/i,
/^\/browse\//,
/^\/search$/,
/^\/community-list$/,
/^\/statistics$/,
],
enableSearchComponent: false, enableSearchComponent: false,
enableBrowseComponent: false, enableBrowseComponent: false,
}, },