Skip to content

Commit 4bb7b54

Browse files
[DURACOM-344] Adapt SSR page filtering mechanism to a not allowed list
(cherry picked from commit b1c5460)
1 parent d6c8be6 commit 4bb7b54

6 files changed

Lines changed: 48 additions & 10 deletions

File tree

config/config.example.yml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,17 @@ 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+
# Regexes to be run against the path of the page to check if SSR is allowed.
27+
# If the path match any of the regexes it will be served directly in CSR.
28+
# By default, excludes community and collection browse, global browse, global search, community list, and statistics.
29+
excludePathRegexes: [
30+
/^\/communities\/[ 0-9a-f ]{ 8 }-[ 0-9a-f ]{ 4 }-[ 0-9a-f ]{ 4 }-[ 0-9a-f ]{ 4 }-[ 0-9a-f ]{ 12 }\/browse(\/.*)?$/i,
31+
/^\/collections\/[ 0-9a-f ]{ 8 }-[ 0-9a-f ]{ 4 }-[ 0-9a-f ]{ 4 }-[ 0-9a-f ]{ 4 }-[ 0-9a-f ]{ 12 }\/browse(\/.*)?$/i,
32+
/^\/browse\//,
33+
/^\/search$/,
34+
/^\/community-list$/,
35+
/^\/statistics$/,
36+
]
3037
# Whether to enable rendering of Search component on SSR.
3138
# If set to true the component will be included in the HTML returned from the server side rendering.
3239
# If set to false the component will not be included in the HTML returned from the server side rendering.

server.ts

Lines changed: 11 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 === '/' || !isExcludedFromSsr(req.path, environment.ssr.excludePathRegexes))) {
225225
// Render the page to user via SSR (server side rendering)
226226
serverSideRender(req, res, next);
227227
} else {
@@ -627,6 +627,16 @@ function start() {
627627
}
628628
}
629629

630+
/**
631+
* Check if SSR should be skipped for path
632+
*
633+
* @param path
634+
* @param excludePathRegexes
635+
*/
636+
function isExcludedFromSsr(path: string, excludePathRegexes: RegExp[]): boolean {
637+
return excludePathRegexes.some((regex) => regex.test(path));
638+
}
639+
630640
/*
631641
* The callback function to serve health check requests
632642
*/

src/config/ssr-config.interface.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ 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+
* Regexes to match url's path and check if SSR is disabled for it.
4343
*/
44-
paths: Array<string>;
44+
excludePathRegexes: RegExp[];
4545

4646
/**
4747
* Whether to enable rendering of search component on SSR

src/environments/environment.production.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ 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/' ],
13+
excludePathRegexes: [
14+
/^\/communities\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\/browse(\/.*)?$/i,
15+
/^\/collections\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\/browse(\/.*)?$/i,
16+
/^\/browse\//,
17+
/^\/search$/,
18+
/^\/community-list$/,
19+
/^\/statistics$/,
20+
],
1421
enableSearchComponent: false,
1522
enableBrowseComponent: false,
1623
},

src/environments/environment.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ 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+
excludePathRegexes: [
18+
/^\/communities\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\/browse(\/.*)?$/i,
19+
/^\/collections\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\/browse(\/.*)?$/i,
20+
/^\/browse\//,
21+
/^\/search$/,
22+
/^\/community-list$/,
23+
/^\/statistics$/,
24+
],
1825
enableSearchComponent: false,
1926
enableBrowseComponent: false,
2027
},

src/environments/environment.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ 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+
excludePathRegexes: [
19+
/^\/communities\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\/browse(\/.*)?$/i,
20+
/^\/collections\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\/browse(\/.*)?$/i,
21+
/^\/browse\//,
22+
/^\/search$/,
23+
/^\/community-list$/,
24+
/^\/statistics$/,
25+
],
1926
enableSearchComponent: false,
2027
enableBrowseComponent: false,
2128
},

0 commit comments

Comments
 (0)