Skip to content

Commit 70009df

Browse files
Ufal/Fix rendering client side namespace (#945)
* Added rendering namespace when client side * Added SSR excluded path into config file
1 parent 01371f1 commit 70009df

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

config/config.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,38 @@ rest:
66
port: 8080
77
nameSpace: /server
88

9+
universal:
10+
# Whether to tell Angular to inline "critical" styles into the server-side rendered HTML.
11+
# Determining which styles are critical is a relatively expensive operation; this option is
12+
# disabled (false) by default to boost server performance at the expense of loading smoothness.
13+
inlineCriticalCss: false
14+
# Patterns to be run as regexes against the path of the page to check if SSR is allowed.
15+
# If the path match any of the regexes it will be served directly in CSR.
16+
# By default, excludes community and collection browse, global browse, global search, community list, statistics and various administrative tools.
17+
excludePathPatterns:
18+
- pattern: "^/communities/[a-f0-9-]{36}/browse(/.*)?$"
19+
flag: "i"
20+
- pattern: "^/collections/[a-f0-9-]{36}/browse(/.*)?$"
21+
flag: "i"
22+
- pattern: "^/browse/"
23+
- pattern: "^/search$"
24+
- pattern: "^/community-list$"
25+
- pattern: "^/admin/"
26+
- pattern: "^/processes/?"
27+
- pattern: "^/notifications/"
28+
- pattern: "^/statistics/?"
29+
- pattern: "^/access-control/"
30+
- pattern: "^/health$"
31+
32+
# Whether to enable rendering of Search component on SSR.
33+
# If set to true the component will be included in the HTML returned from the server side rendering.
34+
# If set to false the component will not be included in the HTML returned from the server side rendering.
35+
enableSearchComponent: false
36+
# Whether to enable rendering of Browse component on SSR.
37+
# If set to true the component will be included in the HTML returned from the server side rendering.
38+
# If set to false the component will not be included in the HTML returned from the server side rendering.
39+
enableBrowseComponent: false
40+
941
# Caching settings
1042
cache:
1143
# NOTE: how long should objects be cached for by default

server.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,6 @@ function serverSideRender(req, res, sendToUser: boolean = true) {
273273
requestUrl: req.originalUrl,
274274
}, (err, data) => {
275275
if (hasNoValue(err) && hasValue(data)) {
276-
// Fix missing base href in SSR output
277-
const baseHref = `${environment.ui.nameSpace}${environment.ui.nameSpace.endsWith('/') ? '' : '/'}`;
278-
data = data.replace(/<base href=".*?">/, `<base href="${baseHref}">`);
279-
280276
// Replace REST URL with UI URL
281277
if (environment.universal.replaceRestUrl && REST_BASE_URL !== environment.rest.baseUrl) {
282278
data = data.replace(new RegExp(REST_BASE_URL, 'g'), environment.rest.baseUrl);
@@ -307,13 +303,24 @@ function serverSideRender(req, res, sendToUser: boolean = true) {
307303
});
308304
}
309305

310-
/**
311-
* Send back response to user to trigger direct client-side rendering (CSR)
312-
* @param req current request
313-
* @param res current response
314-
*/
306+
// Read file once at startup
307+
const indexHtmlContent = readFileSync(indexHtml, 'utf8');
308+
315309
function clientSideRender(req, res) {
316-
res.sendFile(indexHtml);
310+
const namespace = environment.ui.nameSpace || '/';
311+
let html = indexHtmlContent;
312+
// Replace base href dynamically
313+
html = html.replace(
314+
/<base href="[^"]*">/,
315+
`<base href="${namespace.endsWith('/') ? namespace : namespace + '/'}">`
316+
);
317+
318+
// Replace REST URL with UI URL
319+
if (environment.universal.replaceRestUrl && REST_BASE_URL !== environment.rest.baseUrl) {
320+
html = html.replace(new RegExp(REST_BASE_URL, 'g'), environment.rest.baseUrl);
321+
}
322+
323+
res.send(html);
317324
}
318325

319326

0 commit comments

Comments
 (0)