Skip to content

Commit ce44a89

Browse files
authored
Merge pull request #4325 from DSpace/backport-4227-to-dspace-8_x
[Port dspace-8_x] Remove `ssr.paths` configuration and replace with `ssr.excludePathPatterns` which excludes specific paths from SSR
2 parents d6c8be6 + 5cbec37 commit ce44a89

File tree

6 files changed

+99
-10
lines changed

6 files changed

+99
-10
lines changed

config/config.example.yml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,24 @@ 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+
# 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
@@ -58,6 +58,7 @@ import {
5858
REQUEST,
5959
RESPONSE,
6060
} from './src/express.tokens';
61+
import { SsrExcludePatterns } from "./src/config/ssr-config.interface";
6162

6263
/*
6364
* Set path for the browser application's dist folder
@@ -221,7 +222,7 @@ export function app() {
221222
* The callback function to serve server side angular
222223
*/
223224
function ngApp(req, res, next) {
224-
if (environment.ssr.enabled && req.method === 'GET' && (req.path === '/' || environment.ssr.paths.some(pathPrefix => req.path.startsWith(pathPrefix)))) {
225+
if (environment.ssr.enabled && req.method === 'GET' && (req.path === '/' || !isExcludedFromSsr(req.path, environment.ssr.excludePathPatterns))) {
225226
// Render the page to user via SSR (server side rendering)
226227
serverSideRender(req, res, next);
227228
} else {
@@ -627,6 +628,21 @@ function start() {
627628
}
628629
}
629630

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

src/config/ssr-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 SSRConfig extends Config {
49
/**
510
* A boolean flag indicating whether the SSR configuration is enabled
@@ -39,9 +44,9 @@ export interface SSRConfig extends Config {
3944
replaceRestUrl: boolean;
4045

4146
/**
42-
* Paths to enable SSR for. Defaults to the home page and paths in the sitemap.
47+
* Patterns to be used as regexes to match url's path and check if SSR is disabled for it.
4348
*/
44-
paths: Array<string>;
49+
excludePathPatterns: SsrExcludePatterns[];
4550

4651
/**
4752
* 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
@@ -10,7 +10,25 @@ 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+
excludePathPatterns: [
14+
{
15+
pattern: '^/communities/[a-f0-9-]{36}/browse(/.*)?$',
16+
flag: 'i',
17+
},
18+
{
19+
pattern: '^/collections/[a-f0-9-]{36}/browse(/.*)?$',
20+
flag: 'i',
21+
},
22+
{ pattern: '^/browse/' },
23+
{ pattern: '^/search' },
24+
{ pattern: '^/community-list$' },
25+
{ pattern: '^/statistics/?' },
26+
{ pattern: '^/admin/' },
27+
{ pattern: '^/processes/?' },
28+
{ pattern: '^/notifications/' },
29+
{ pattern: '^/access-control/' },
30+
{ pattern: '^/health$' },
31+
],
1432
enableSearchComponent: false,
1533
enableBrowseComponent: false,
1634
},

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

0 commit comments

Comments
 (0)