Skip to content

Commit f9aa721

Browse files
authored
Merge pull request #4332 from 4Science/task/dspace-7_x/DURACOM-344
[Port dspace-7_x] Remove `ssr.paths` configuration and replace with `ssr.excludePathPatterns` which excludes specific paths from SSR
2 parents fbbf16f + d47baab commit f9aa721

6 files changed

Lines changed: 99 additions & 10 deletions

File tree

config/config.example.yml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,24 @@ 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+
# Patterns to be run as regexes 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, statistics and various administrative tools.
29+
excludePathPatterns:
30+
- pattern: "^/communities/[a-f0-9-]{36}/browse(/.*)?$",
31+
flag: "i"
32+
- pattern: "^/collections/[a-f0-9-]{36}/browse(/.*)?$"
33+
flag: "i"
34+
- pattern: "^/browse/"
35+
- pattern: "^/search$"
36+
- pattern: "^/community-list$"
37+
- pattern: "^/admin/"
38+
- pattern: "^/processes/?"
39+
- pattern: "^/notifications/"
40+
- pattern: "^/statistics/?"
41+
- pattern: "^/access-control/"
42+
- pattern: "^/health$"
43+
3044
# Whether to enable rendering of Search component on SSR.
3145
# If set to true the component will be included in the HTML returned from the server side rendering.
3246
# If set to false the component will not be included in the HTML returned from the server side rendering.

server.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import { APP_CONFIG, AppConfig } from './src/config/app-config.interface';
5555
import { extendEnvironmentWithAppConfig } from './src/config/config.util';
5656
import { logStartupMessage } from './startup-message';
5757
import { TOKENITEM } from './src/app/core/auth/models/auth-token-info.model';
58+
import { SsrExcludePatterns } from './src/config/universal-config.interface';
5859

5960

6061
/*
@@ -241,7 +242,7 @@ export function app() {
241242
* The callback function to serve server side angular
242243
*/
243244
function ngApp(req, res) {
244-
if (environment.universal.preboot && req.method === 'GET' && (req.path === '/' || environment.universal.paths.some(pathPrefix => req.path.startsWith(pathPrefix)))) {
245+
if (environment.universal.preboot && req.method === 'GET' && (req.path === '/' || !isExcludedFromSsr(req.path, environment.universal.excludePathPatterns))) {
245246
// Render the page to user via SSR (server side rendering)
246247
serverSideRender(req, res);
247248
} else {
@@ -625,6 +626,21 @@ function start() {
625626
}
626627
}
627628

629+
/**
630+
* Check if SSR should be skipped for path
631+
*
632+
* @param path
633+
* @param excludePathPattern
634+
*/
635+
function isExcludedFromSsr(path: string, excludePathPattern: SsrExcludePatterns[]): boolean {
636+
const patterns = excludePathPattern.map(p =>
637+
new RegExp(p.pattern, p.flag || '')
638+
);
639+
return patterns.some((regex) => {
640+
return regex.test(path)
641+
});
642+
}
643+
628644
/*
629645
* The callback function to serve health check requests
630646
*/

src/config/universal-config.interface.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { Config } from './config.interface';
22

3+
export interface SsrExcludePatterns {
4+
pattern: string | RegExp;
5+
flag?: string;
6+
}
7+
38
export interface UniversalConfig extends Config {
49
preboot: boolean;
510
async: boolean;
@@ -32,9 +37,9 @@ export interface UniversalConfig extends Config {
3237
replaceRestUrl: boolean;
3338

3439
/**
35-
* Paths to enable SSR for. Defaults to the home page and paths in the sitemap.
40+
* Patterns to be used as regexes to match url's path and check if SSR is disabled for it.
3641
*/
37-
paths: Array<string>;
42+
excludePathPatterns: SsrExcludePatterns[];
3843

3944
/**
4045
* Whether to enable rendering of search component on SSR

src/environments/environment.production.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,25 @@ 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+
excludePathPatterns: [
15+
{
16+
pattern: '^/communities/[a-f0-9-]{36}/browse(/.*)?$',
17+
flag: 'i',
18+
},
19+
{
20+
pattern: '^/collections/[a-f0-9-]{36}/browse(/.*)?$',
21+
flag: 'i',
22+
},
23+
{ pattern: '^/browse/' },
24+
{ pattern: '^/search' },
25+
{ pattern: '^/community-list$' },
26+
{ pattern: '^/statistics/?' },
27+
{ pattern: '^/admin/' },
28+
{ pattern: '^/processes/?' },
29+
{ pattern: '^/notifications/' },
30+
{ pattern: '^/access-control/' },
31+
{ pattern: '^/health$' },
32+
],
1533
enableSearchComponent: false,
1634
enableBrowseComponent: false,
1735
},

src/environments/environment.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,25 @@ 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+
excludePathPatterns: [
18+
{
19+
pattern: '^/communities/[a-f0-9-]{36}/browse(/.*)?$',
20+
flag: 'i',
21+
},
22+
{
23+
pattern: '^/collections/[a-f0-9-]{36}/browse(/.*)?$',
24+
flag: 'i',
25+
},
26+
{ pattern: '^/browse/' },
27+
{ pattern: '^/search' },
28+
{ pattern: '^/community-list$' },
29+
{ pattern: '^/statistics/?' },
30+
{ pattern: '^/admin/' },
31+
{ pattern: '^/processes/?' },
32+
{ pattern: '^/notifications/' },
33+
{ pattern: '^/access-control/' },
34+
{ pattern: '^/health$' },
35+
],
1836
enableSearchComponent: false,
1937
enableBrowseComponent: false,
2038
},

src/environments/environment.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,25 @@ 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+
excludePathPatterns: [
20+
{
21+
pattern: '^/communities/[a-f0-9-]{36}/browse(/.*)?$',
22+
flag: 'i',
23+
},
24+
{
25+
pattern: '^/collections/[a-f0-9-]{36}/browse(/.*)?$',
26+
flag: 'i',
27+
},
28+
{ pattern: '^/browse/' },
29+
{ pattern: '^/search' },
30+
{ pattern: '^/community-list$' },
31+
{ pattern: '^/statistics/?' },
32+
{ pattern: '^/admin/' },
33+
{ pattern: '^/processes/?' },
34+
{ pattern: '^/notifications/' },
35+
{ pattern: '^/access-control/' },
36+
{ pattern: '^/health$' },
37+
],
2038
enableSearchComponent: false,
2139
enableBrowseComponent: false,
2240
},

0 commit comments

Comments
 (0)