Skip to content

Commit ce0f615

Browse files
[DURACOM-344] Adapt SSR page filtering mechanism to a not allowed list
1 parent fbbf16f commit ce0f615

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 @@ universal:
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
@@ -241,7 +241,7 @@ export function app() {
241241
* The callback function to serve server side angular
242242
*/
243243
function ngApp(req, res) {
244-
if (environment.universal.preboot && req.method === 'GET' && (req.path === '/' || environment.universal.paths.some(pathPrefix => req.path.startsWith(pathPrefix)))) {
244+
if (environment.universal.preboot && req.method === 'GET' && (req.path === '/' || !isExcludedFromSsr(req.path, environment.universal.excludePathRegexes))) {
245245
// Render the page to user via SSR (server side rendering)
246246
serverSideRender(req, res);
247247
} else {
@@ -625,6 +625,16 @@ function start() {
625625
}
626626
}
627627

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

src/config/universal-config.interface.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ export interface UniversalConfig extends Config {
3232
replaceRestUrl: boolean;
3333

3434
/**
35-
* Paths to enable SSR for. Defaults to the home page and paths in the sitemap.
35+
* Regexes to match url's path and check if SSR is disabled for it.
3636
*/
37-
paths: Array<string>;
37+
excludePathRegexes: RegExp[];
3838

3939
/**
4040
* 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
@@ -11,7 +11,14 @@ export const environment: Partial<BuildConfig> = {
1111
inlineCriticalCss: false,
1212
transferState: true,
1313
replaceRestUrl: true,
14-
paths: [ '/home', '/items/', '/entities/', '/collections/', '/communities/', '/bitstream/', '/bitstreams/', '/handle/', '/reload/' ],
14+
excludePathRegexes: [
15+
/^\/communities\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\/browse(\/.*)?$/i,
16+
/^\/collections\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\/browse(\/.*)?$/i,
17+
/^\/browse\//,
18+
/^\/search$/,
19+
/^\/community-list$/,
20+
/^\/statistics$/,
21+
],
1522
enableSearchComponent: false,
1623
enableBrowseComponent: false,
1724
},

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
@@ -16,7 +16,14 @@ export const environment: Partial<BuildConfig> = {
1616
inlineCriticalCss: false,
1717
transferState: true,
1818
replaceRestUrl: false,
19-
paths: [ '/home', '/items/', '/entities/', '/collections/', '/communities/', '/bitstream/', '/bitstreams/', '/handle/', '/reload/' ],
19+
excludePathRegexes: [
20+
/^\/communities\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\/browse(\/.*)?$/i,
21+
/^\/collections\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\/browse(\/.*)?$/i,
22+
/^\/browse\//,
23+
/^\/search$/,
24+
/^\/community-list$/,
25+
/^\/statistics$/,
26+
],
2027
enableSearchComponent: false,
2128
enableBrowseComponent: false,
2229
},

0 commit comments

Comments
 (0)