Skip to content

Cache-bust dynamic configuration files and theme CSS - #4936

Merged
tdonohue merged 12 commits into
DSpace:mainfrom
atmire:cache-bust-dynamic-configuration_contribute-main
Apr 17, 2026
Merged

Cache-bust dynamic configuration files and theme CSS #4936
tdonohue merged 12 commits into
DSpace:mainfrom
atmire:cache-bust-dynamic-configuration_contribute-main

Conversation

@AbhinavS96

@AbhinavS96 AbhinavS96 commented Dec 12, 2025

Copy link
Copy Markdown
Contributor

References

Fixes #1961
Fixes #3992
DSpace 10 port of #3993

Description

See original PR

Instructions for Reviewers

To test:

Run DSpace Angular locally, in production mode

  1. Confirm that you see the following files:

  2. [dist]/assets/config..json → hashed configuration file

    • [dist]/dspace-theme..css → hashed theme CSS file
    • [dist]/index..html → index file with an extra script#hashed-file-mapping element containing the mapping of plain paths to hashed paths
    • Confirm that config..json and dspace-theme..css are correctly served to the browser, and that the app runs/looks as before the PR
  3. Confirm that Angular runs with stale configuration when SSR is bypassed #3992 is resolved

Checklist

  • My PR is created against the main branch of code (unless it is a backport or is fixing an issue specific to an older branch).
  • My PR is small in size (e.g. less than 1,000 lines of code, not including comments & specs/tests), or I have provided reasons as to why that's not possible.
  • My PR passes ESLint validation using npm run lint
  • My PR doesn't introduce circular dependencies (verified via npm run check-circ-deps)
  • My PR includes TypeDoc comments for all new (or modified) public methods and classes. It also includes TypeDoc for large or complex private methods.
  • My PR passes all specs/tests and includes new/updated specs or tests based on the Code Testing Guide.
  • My PR aligns with Accessibility guidelines if it makes changes to the user interface.
  • My PR uses i18n (internationalization) keys instead of hardcoded English text, to allow for translations.
  • My PR includes details on how to test it. I've provided clear instructions to reviewers on how to successfully test this fix or feature.
  • If my PR includes new libraries/dependencies (in package.json), I've made sure their licenses align with the DSpace BSD License based on the Licensing of Contributions documentation.
  • If my PR includes new features or configurations, I've provided basic technical documentation in the PR itself.
  • If my PR fixes an issue ticket, I've linked them together.

ybnd added 8 commits February 14, 2025 00:11
DSpace Angular's production server can modify the configuration it serves to CSR-only clients through YAML or environment variables.
However, these files can remain cached in the browser and leave users with out-of-date configuration until the TTL runs out or the user does a "hard refresh".
On build time this sort of problem is solved by saving the content hash as part of the path of each file (JS, CSS, ...)

We introduce HashedFileMapping to bridge the same gap for dynamic content generated _after_ the server is built:
- Files added to this mapping on the server are copied to a hashed path
- A copy is injected into index.html, where clients can read it out and resolve the hashed paths
- If a given path is not found in the mapping, the client will fall back to the original version (to prevent errors in development mode)

With this mechanism we can ensure updates to config.json and similar files take immediate effect without losing the performance benefit of client-side caching.
The JSON mapping needs to be declared as a data block, otherwise browers may complain (nothing seems to really break though, except for Cypress)
See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#any_other_value
Mainly for 8.x and beyond, where ThemeService is injected more often ~ standalone components
Both the configuration and theme CSS are known to be needed for every render.
We can save some time by retrieving them as soon as the HTML is read.
This didn't work well in one specific case: requests without a path (e.g. https://demo.dspace.org)
Such requests would result in a 304 redirect directly to index.html, losing the hashed file mapping and causing it to get cached.
Then it could remain in the cache across rebuilds.

Solutions:
- Don't try to hash index.html, but modify it in place
- Introduce configuration to disable caching for specific static files and apply this to index.html to prevent similar problems
- Don't let browsers cache index.html when it's served for CSR under another path
- Track head links in a Map, because Set doesn't deduplicate them properly
- Make ServerHashedFileMapping independent of ThemeConfig
- Make theme pre-fetching configurable (e.g. only prefetch the main theme CSS, but leave route-based theme CSS lazy)
@AbhinavS96
AbhinavS96 force-pushed the cache-bust-dynamic-configuration_contribute-main branch 6 times, most recently from f994389 to ebb4c0e Compare December 18, 2025 09:45
@alexandrevryghem alexandrevryghem added bug performance / caching Related to performance, caching or embedded objects configuration labels Dec 18, 2025
@tdonohue tdonohue moved this to 🙋 Needs Reviewers Assigned in DSpace 10.0 Release Jan 6, 2026
@AbhinavS96
AbhinavS96 marked this pull request as ready for review February 5, 2026 09:45
@pzlakowski

pzlakowski commented Feb 17, 2026

Copy link
Copy Markdown

The only thing which I cannot reproduce to work are the CSS changes due to wrongly resolved link to stylesheet. I hope somebody could double check it. This might have something to do with ThemeService.

Reproduction steps:

I use docker for development, Dockerfile.dist builds for production and run with PM2 supervisor(I run 1 instance like previous PR suggests).

  • Pull PR and merge the newest main
  • Build image using, flag --no-cache is important to generate different hash each time since docker may cache when you change the same things over and over
$ docker compose -f docker/docker-compose-dist.yml build --no-cache
  • Start server
$ docker compose -p d9 -f docker/docker-compose.yml -f docker/docker-compose-rest.yml -f docker/docker-compose-dist.yml up -d

Environment:

  • Chrome Version 144.0.7559.133 (Official Build) (arm64)
  • Firefox 147.0.3

Checklist:

Tests:

Shared:

  • I have checked [dist]/dpsace-theme.css with hash
  • I have checked [dist]/custom.css with hash
  • I have checked [dist]/index.html has reflected changes about resources with hash
  • Checked the theme CSS changes

It took me a bit to understand mechanism.

Case 1. Swap from theme X to theme Y

This works just fine.

Case 2. Modify CSS for the same theme

In my case I changed in _custom_variables.scss the --ds-footer-bg variable between $success and $danger. The changes will not be visible till I do not reset cache manually. I think this is because ThemeService will never resolve correctly to use hashed css since it fallbacks to cached custom-theme.css.

<link rel="prefetch" as="style" href="custom-theme.c96c2175297734122eafc46bb0ffbcf3.css" class="hfm">
<link rel="stylesheet" type="text/css" class="theme-css" href="custom-theme.css">

The same behaviour is with default theme.

SSR:

  • Tested with custom theme(CSS changes)
  • Tested with default theme(config change & css changes after swap from custom)

I have checked with network tab and differences between index.html builds. The index.html and config.json changes occurs just fine.

CSR:

  • Tested with custom theme(CSS changes)
  • Tested with default theme(config change & css changes after swap from custom)

I have checked with network tab and differences between index.html builds. The index.html and config.json changes occurs just fine.

@tdonohue tdonohue moved this from 🙋 Needs Reviewers Assigned to 👀 Under Review in DSpace 10.0 Release Feb 26, 2026
…bust-dynamic-configuration_contribute-main
@AbhinavS96
AbhinavS96 force-pushed the cache-bust-dynamic-configuration_contribute-main branch from ebb4c0e to c74daef Compare February 27, 2026 10:33

@MW3000 MW3000 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did test the functionality but not review the code:

  • config.<hash>.json file is correctly generated ✅
  • config.<hash>.json is correctly referenced ✅
  • <xxx>-theme.<hash>.css files are correctly generated ✅
  • <xxx>-theme.<hash>.css files are correctly referenced ✅
  • I tried with one and two custom themes, works both ✅
  • index.html contains the correct hashed-file-mappings and an additional <script id="hashed-file-mapping” section. ✅
  • New versions of the hashed files are generated when I make changes ✅
  • Everything is correctly served to the browser, and that the app runs/looks as before the PR ✅
  • I am not sure how to test “when SSR is bypassed” - what exactly does that mean? How do I make sure SSR is bypassed? ❓

@github-actions

github-actions Bot commented Apr 9, 2026

Copy link
Copy Markdown

Hi @AbhinavS96,
Conflicts have been detected against the base branch.
Please resolve these conflicts as soon as you can. Thanks!

@tdonohue

Copy link
Copy Markdown
Member

@AbhinavS96 or @ybnd : Could one of you rebase this on latest main? This is something I still want to get into 10.0. But, it's not in a mergeable/testable state. Thanks!

@ybnd

ybnd commented Apr 17, 2026

Copy link
Copy Markdown
Member

@tdonohue that was a quick one, just some dependencies shifting in the lock file

@tdonohue tdonohue left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Thanks @AbhinavS96 and @ybnd ! I tested and reviewed this today.

I've verified that hashed files are being created & loaded properly (so #1961 is fixed). I couldn't find any differences in UI behavior.

I also verified that #3992 is fixed by running the app in Production, stopping it, changing a config in my config.prod.yml, restarting it, and clicking the reload button on the page. In that scenario the updates to my config.prod.yml resulted in a newly hashed config.json and the new settings loaded immediately. (Previously I'd have to do a hard reload for settings to refresh properly in the browser, so this is fixed!)

One final note is that I'm adding the "needs documentation" label because the two new configs in this PR (noCacheFiles and prefetch) need to be added to the documentation at https://wiki.lyrasis.org/display/DSDOC10x/User+Interface+Configuration Please let me know once you've made those minor updates and we'll remove that label.

@tdonohue tdonohue added needs documentation PR is missing documentation. All new features and config changes require documentation. port to dspace-9_x This PR needs to be ported to `dspace-9_x` branch for next bug-fix release labels Apr 17, 2026
@tdonohue tdonohue added this to the 10.0 milestone Apr 17, 2026
@tdonohue
tdonohue merged commit 51a37cc into DSpace:main Apr 17, 2026
17 of 19 checks passed
@github-project-automation github-project-automation Bot moved this from 👍 Reviewer Approved to ✅ Done in DSpace 10.0 Release Apr 17, 2026
@dspace-bot

Copy link
Copy Markdown
Contributor

Backport failed for dspace-9_x, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally and resolve any conflicts.

git fetch origin dspace-9_x
git worktree add -d .worktree/backport-4936-to-dspace-9_x origin/dspace-9_x
cd .worktree/backport-4936-to-dspace-9_x
git switch --create backport-4936-to-dspace-9_x
git cherry-pick -x d336d8336178dcd4a5d822df188f2d47aca73599 fdc6e8aaec25ab11f9cee3bcb93e65ce1415e907 2f235991057154531f6d244e4b5df59b17078dd6 ce4dd3aa3254be96a8e28b1d5090b2cdbce001b5 928e932b06eb1eb78364ebd940ae7d36430ac3aa c6bf2b7355c3c7ba493c3b2688709a9125740517 93da6ce34cded5b29c4e7dcb0aa1f50fbb2eb87f d2458e6f95daec73e0c617799c7c6f761ebd983e

@tdonohue

Copy link
Copy Markdown
Member

@tdonohue tdonohue added the port to dspace-8_x This PR needs to be ported to `dspace-8_x` branch for next bug-fix release label Apr 17, 2026
@tdonohue tdonohue changed the title Cache-bust dynamic configuration files and theme CSS (port to 10.0) Cache-bust dynamic configuration files and theme CSS Apr 17, 2026
@tdonohue

tdonohue commented May 4, 2026

Copy link
Copy Markdown
Member

@AbhinavS96 and @ybnd : Reminder, this PR still requires documentation.

One final note is that I'm adding the "needs documentation" label because the two new configs in this PR (noCacheFiles and prefetch) need to be added to the documentation at https://wiki.lyrasis.org/display/DSDOC10x/User+Interface+Configuration Please let me know once you've made those minor updates and we'll remove that label.

@ybnd

ybnd commented May 5, 2026

Copy link
Copy Markdown
Member

@tdonohue tdonohue removed port to dspace-8_x This PR needs to be ported to `dspace-8_x` branch for next bug-fix release port to dspace-9_x This PR needs to be ported to `dspace-9_x` branch for next bug-fix release labels May 5, 2026
@tdonohue

tdonohue commented May 5, 2026

Copy link
Copy Markdown
Member

Thanks @ybnd ! Docs look good

@tdonohue tdonohue removed the needs documentation PR is missing documentation. All new features and config changes require documentation. label May 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug configuration high priority improvement performance / caching Related to performance, caching or embedded objects

Projects

No open projects
Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

Angular runs with stale configuration when SSR is bypassed Add hash/fingerprint to the custom-theme CSS files

7 participants