Skip to content

Commit df583c9

Browse files
[DURACOM-344] switch to blacklist approach for SSR
1 parent 08745f6 commit df583c9

6 files changed

Lines changed: 41 additions & 10 deletions

File tree

config/config.example.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ ssr:
2323
# Determining which styles are critical is a relatively expensive operation; this option is
2424
# disabled (false) by default to boost server performance at the expense of loading smoothness.
2525
inlineCriticalCss: false
26-
# Path prefixes to enable SSR for. By default these are limited to paths of primary DSpace objects.
27-
# NOTE: The "/handle/" path ensures Handle redirects work via SSR. The "/reload/" path ensures
28-
# hard refreshes (e.g. after login) trigger SSR while fully reloading the page.
29-
paths: [ '/home', '/items/', '/entities/', '/collections/', '/communities/', '/bitstream/', '/bitstreams/', '/handle/', '/reload/' ]
26+
# Path prefixes to disable SSR for. If the path the user is visiting starts with one of the excluded paths, it will be served directly in CSR.
27+
# These should be limited to paths that might cause high consumption in resources (e.g. crawler on pages with many items to follow).
28+
excludePaths: ['/browse/', '/community-list', '/search/', '/statistics' ]
29+
# Regexes to be run against the URl of the page to check if SSR is allowed.
30+
# This configuration offers more flexibility and precision than excludePaths as we can match more complex URLs.
31+
# If the entire URL match any of the regexes it will be served directly in CSR.
32+
ssrBlacklistRegexes: [ /^\/communities\/[0-9a-fA-F-]{36}(\/.*)?$/, /^\/collections\/[0-9a-fA-F-]{36}(\/.*)?$/]
3033
# Whether to enable rendering of Search component on SSR.
3134
# If set to true the component will be included in the HTML returned from the server side rendering.
3235
# If set to false the component will not be included in the HTML returned from the server side rendering.

server.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ export function app() {
221221
* The callback function to serve server side angular
222222
*/
223223
function ngApp(req, res, next) {
224-
if (environment.ssr.enabled && req.method === 'GET' && (req.path === '/' || environment.ssr.paths.some(pathPrefix => req.path.startsWith(pathPrefix)))) {
224+
if (environment.ssr.enabled && req.method === 'GET' && (req.path === '/' || !isBlacklistedPathForSsr(req.url, req.path, environment.ssr.ssrBlacklistRegexes))) {
225225
// Render the page to user via SSR (server side rendering)
226226
serverSideRender(req, res, next);
227227
} else {
@@ -627,6 +627,17 @@ function start() {
627627
}
628628
}
629629

630+
/**
631+
* Check if SSR should be skipped for url or path
632+
*
633+
* @param url
634+
* @param path
635+
* @param blacklist
636+
*/
637+
function isBlacklistedPathForSsr(url: string, path: string, blacklist: RegExp[]): boolean {
638+
return blacklist.some((regex) => regex.test(url)) || environment.ssr.excludePaths.some(blacklistedPath => path.startsWith(blacklistedPath));
639+
}
640+
630641
/*
631642
* The callback function to serve health check requests
632643
*/

src/config/ssr-config.interface.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@ export interface SSRConfig extends Config {
3939
replaceRestUrl: boolean;
4040

4141
/**
42-
* Paths to enable SSR for. Defaults to the home page and paths in the sitemap.
42+
* Paths to disable SSR for, to avoid some possible performance issue for specific pages.
4343
*/
44-
paths: Array<string>;
44+
excludePaths: Array<string>;
45+
46+
/**
47+
* Regexes to match url and check if SSR is enabled for it.
48+
*/
49+
ssrBlacklistRegexes: RegExp[];
4550

4651
/**
4752
* Whether to enable rendering of search component on SSR

src/environments/environment.production.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ export const environment: Partial<BuildConfig> = {
1010
inlineCriticalCss: false,
1111
transferState: true,
1212
replaceRestUrl: true,
13-
paths: [ '/home', '/items/', '/entities/', '/collections/', '/communities/', '/bitstream/', '/bitstreams/', '/handle/', '/reload/', '/500', '/404', '/403' ],
13+
excludePaths: [ '/browse/', '/community-list', '/search/', '/statistics' ],
14+
ssrBlacklistRegexes: [
15+
/^\/communities\/[0-9a-fA-F-]{36}(\/.*)?$/,
16+
/^\/collections\/[0-9a-fA-F-]{36}(\/.*)?$/,
17+
],
1418
enableSearchComponent: false,
1519
enableBrowseComponent: false,
1620
},

src/environments/environment.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ export const environment: BuildConfig = {
1414
inlineCriticalCss: false,
1515
transferState: true,
1616
replaceRestUrl: false,
17-
paths: [ '/home', '/items/', '/entities/', '/collections/', '/communities/', '/bitstream/', '/bitstreams/', '/handle/', '/reload/' ],
17+
excludePaths: [ '/browse/', '/community-list', '/search/', '/statistics' ],
18+
ssrBlacklistRegexes: [
19+
/^\/communities\/[0-9a-fA-F-]{36}(\/.*)?$/,
20+
/^\/collections\/[0-9a-fA-F-]{36}(\/.*)?$/,
21+
],
1822
enableSearchComponent: false,
1923
enableBrowseComponent: false,
2024
},

src/environments/environment.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ export const environment: Partial<BuildConfig> = {
1515
inlineCriticalCss: false,
1616
transferState: true,
1717
replaceRestUrl: false,
18-
paths: [ '/home', '/items/', '/entities/', '/collections/', '/communities/', '/bitstream/', '/bitstreams/', '/handle/', '/reload/' ],
18+
excludePaths: [ '/browse/', '/community-list', '/search/', '/statistics' ],
19+
ssrBlacklistRegexes: [
20+
/^\/communities\/[0-9a-fA-F-]{36}(\/.*)?$/,
21+
/^\/collections\/[0-9a-fA-F-]{36}(\/.*)?$/,
22+
],
1923
enableSearchComponent: false,
2024
enableBrowseComponent: false,
2125
},

0 commit comments

Comments
 (0)