Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,38 @@ rest:
port: 8080
nameSpace: /server

universal:
# Whether to tell Angular to inline "critical" styles into the server-side rendered HTML.
# Determining which styles are critical is a relatively expensive operation; this option is
# disabled (false) by default to boost server performance at the expense of loading smoothness.
inlineCriticalCss: false
# Patterns to be run as regexes against the path of the page to check if SSR is allowed.
# If the path match any of the regexes it will be served directly in CSR.
# By default, excludes community and collection browse, global browse, global search, community list, statistics and various administrative tools.
excludePathPatterns:
- pattern: "^/communities/[a-f0-9-]{36}/browse(/.*)?$"
flag: "i"
- pattern: "^/collections/[a-f0-9-]{36}/browse(/.*)?$"
flag: "i"
- pattern: "^/browse/"
- pattern: "^/search$"
- pattern: "^/community-list$"
- pattern: "^/admin/"
- pattern: "^/processes/?"
- pattern: "^/notifications/"
- pattern: "^/statistics/?"
- pattern: "^/access-control/"
- pattern: "^/health$"

# Whether to enable rendering of Search component on SSR.
# If set to true the component will be included in the HTML returned from the server side rendering.
# If set to false the component will not be included in the HTML returned from the server side rendering.
enableSearchComponent: false
# Whether to enable rendering of Browse component on SSR.
# If set to true the component will be included in the HTML returned from the server side rendering.
# If set to false the component will not be included in the HTML returned from the server side rendering.
enableBrowseComponent: false

# Caching settings
cache:
# NOTE: how long should objects be cached for by default
Expand Down
27 changes: 17 additions & 10 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,6 @@ function serverSideRender(req, res, sendToUser: boolean = true) {
requestUrl: req.originalUrl,
}, (err, data) => {
if (hasNoValue(err) && hasValue(data)) {
// Fix missing base href in SSR output
const baseHref = `${environment.ui.nameSpace}${environment.ui.nameSpace.endsWith('/') ? '' : '/'}`;
data = data.replace(/<base href=".*?">/, `<base href="${baseHref}">`);

// Replace REST URL with UI URL
if (environment.universal.replaceRestUrl && REST_BASE_URL !== environment.rest.baseUrl) {
data = data.replace(new RegExp(REST_BASE_URL, 'g'), environment.rest.baseUrl);
Expand Down Expand Up @@ -307,13 +303,24 @@ function serverSideRender(req, res, sendToUser: boolean = true) {
});
}

/**
* Send back response to user to trigger direct client-side rendering (CSR)
* @param req current request
* @param res current response
*/
// Read file once at startup
const indexHtmlContent = readFileSync(indexHtml, 'utf8');

function clientSideRender(req, res) {
res.sendFile(indexHtml);
const namespace = environment.ui.nameSpace || '/';
let html = indexHtmlContent;
// Replace base href dynamically
html = html.replace(
/<base href="[^"]*">/,
`<base href="${namespace.endsWith('/') ? namespace : namespace + '/'}">`
);

// Replace REST URL with UI URL
if (environment.universal.replaceRestUrl && REST_BASE_URL !== environment.rest.baseUrl) {
html = html.replace(new RegExp(REST_BASE_URL, 'g'), environment.rest.baseUrl);
}

res.send(html);
}


Expand Down
Loading