Skip to content

Commit f9c2f3c

Browse files
authored
Merge pull request #4773 from DSpace/backport-4638-to-dspace-8_x
[Port dspace-8_x] Client side rendering (CSR) ignores `nameSpace` configuration causing broken resource loading
2 parents 5120009 + ec32cd4 commit f9c2f3c

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

server.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,24 @@ function serverSideRender(req, res, next, sendToUser: boolean = true) {
310310
});
311311
}
312312

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

322333

0 commit comments

Comments
 (0)