Skip to content

Conversation

@ybnd
Copy link
Member

@ybnd ybnd commented Feb 14, 2025

References

Description

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.

Compression support is included in anticipation of a pending PR for #3961, where the initial size of config.json can become large enough where compressing it would be a plus.

Discussion

  • It's not clear how/if we can unit test this at the time of writing
  • Should we inlude a configuration flag to toggle this feature on/off? We may want to roll it out as opt-in so downstream users can wait it out until it has been battle-tested a bit.

Instructions for Reviewers

To test:

  1. Run DSpace Angular locally, in production mode
  2. Confirm that you see the following files:
    • [dist]/assets/config.<hash>.json → hashed configuration file
    • [dist]/dspace-theme.<hash>.css → hashed theme CSS file
    • [dist]/index.<hash>.html → index file with an extra script#hashed-file-mapping element containing the mapping of plain paths to hashed paths
  3. Confirm that config.<hash>.json and dspace-theme.<hash>.css are correctly served to the browser, and that the app runs/looks as before the PR
  4. Confirm that Angular runs with stale configuration when SSR is bypassed #3992 is resolved

Checklist

This checklist provides a reminder of what we are going to look for when reviewing your PR. You do not need to complete this checklist prior creating your PR (draft PRs are always welcome).
However, reviewers may request that you complete any actions in this list if you have not done so. If you are unsure about an item in the checklist, don't hesitate to ask. We're here to help!

  • 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.

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.
@ybnd ybnd self-assigned this Feb 14, 2025
@ybnd ybnd added bug performance / caching Related to performance, caching or embedded objects configuration affects: main Issue impacts "main" (latest release). affects: 8.x Issue impacts 8.x releases affects: 7.x Issue impacts 7.x releases claimed: Atmire Atmire team is working on this issue & will contribute back labels Feb 14, 2025
@github-actions
Copy link

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

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
@ybnd ybnd marked this pull request as ready for review February 17, 2025 12:47
@ybnd ybnd force-pushed the cache-bust-dynamic-configuration-7.6 branch from 6e6570e to 921ef18 Compare February 17, 2025 16:17
@ybnd ybnd changed the title Cache-bust dynamic configuration files Cache-bust dynamic configuration files and theme CSS Feb 17, 2025
@ybnd ybnd force-pushed the cache-bust-dynamic-configuration-7.6 branch from 921ef18 to aa7ef08 Compare February 18, 2025 09:00
@tdonohue tdonohue added port to dspace-7_x This PR needs to be ported to `dspace-7_x` branch for next bug-fix release port to dspace-8_x This PR needs to be ported to `dspace-8_x` branch for next bug-fix release labels Feb 18, 2025
@ybnd ybnd added port to main This PR needs to be ported to `main` branch for the next major release and removed port to dspace-7_x This PR needs to be ported to `dspace-7_x` branch for next bug-fix release labels Feb 19, 2025
@ybnd
Copy link
Member Author

ybnd commented Feb 19, 2025

@tdonohue adjusted the labels since this PR was opened against 7.x

@ybnd
Copy link
Member Author

ybnd commented Mar 11, 2025

Note: when running multiple DSpace Angular instances in PM2 cluster mode this would cause the same filesystem operations in parallel, could cause problems.

Could be addressed with Redis similar to #3110 (comment)

@AAwouters AAwouters mentioned this pull request Mar 14, 2025
12 tasks
@ybnd ybnd force-pushed the cache-bust-dynamic-configuration-7.6 branch from 2831efd to 4176ac9 Compare March 17, 2025 14:39
@ybnd
Copy link
Member Author

ybnd commented Mar 17, 2025

Update: while debugging #4081 I realised this PR also gives us the opportunity to prefetch assets.

Angular does similar for global CSS, but doesn't know about our themes or JSON configuration.
If we're already making changes to the index HTML it's easy to include the appropriate links

Before:

After:

@ybnd ybnd force-pushed the cache-bust-dynamic-configuration-7.6 branch from cbdaa7a to 4a276d5 Compare March 19, 2025 07:57
@ybnd ybnd marked this pull request as draft March 19, 2025 11:04
ybnd added 4 commits March 21, 2025 15:37
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
@ybnd ybnd force-pushed the cache-bust-dynamic-configuration-7.6 branch from 533c71c to 600b468 Compare March 21, 2025 15:00
@ybnd ybnd marked this pull request as ready for review March 25, 2025 08:26
Copy link
Contributor

@pcg-kk pcg-kk left a comment

Choose a reason for hiding this comment

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

Work for me fine, tested with SSR & without SSR

@ybnd ybnd force-pushed the cache-bust-dynamic-configuration-7.6 branch from 12b51de to bc9a33e Compare April 16, 2025 13:31
@kshepherd
Copy link
Member

@ybnd could you rebase + repush to force the build tasks to re-run ? the unit test was so long ago that the logs aren't available so it's not clear why it failed

- 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)
….6_CLEAN' into cache-bust-dynamic-configuration-7.6
@ybnd
Copy link
Member Author

ybnd commented Sep 19, 2025

@kshepherd if possible I'd like avoid rebasing to keep the history consistent with the "clean 7.6" version of the branch (already using that elsewhere)

Looking back now I did find a few small improvements to make (mainly with the theme prefetching part), so that will revive the CI just as well.

@github-actions
Copy link

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

@alexandrevryghem alexandrevryghem removed the port to dspace-8_x This PR needs to be ported to `dspace-8_x` branch for next bug-fix release label Dec 8, 2025
@alexandrevryghem alexandrevryghem force-pushed the cache-bust-dynamic-configuration-7.6 branch from aa47ce7 to 5f27668 Compare December 18, 2025 22:15
@alexandrevryghem alexandrevryghem removed claimed: Atmire Atmire team is working on this issue & will contribute back port to main This PR needs to be ported to `main` branch for the next major release affects: main Issue impacts "main" (latest release). affects: 8.x Issue impacts 8.x releases affects: 7.x Issue impacts 7.x releases labels Dec 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug configuration performance / caching Related to performance, caching or embedded objects

Projects

Status: 🙋 Needs Reviewers Assigned

Development

Successfully merging this pull request may close these issues.

5 participants