Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache-bust dynamic configuration files and theme CSS #3993

Open
wants to merge 5 commits into
base: dspace-7_x
Choose a base branch
from

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects: main Issue impacts "main" (latest release). affects: 7.x Issue impacts 7.x releases affects: 8.x Issue impacts 8.x releases bug claimed: Atmire Atmire team is working on this issue & will contribute back configuration performance / caching Related to performance, caching or embedded objects port to dspace-8_x This PR needs to be ported to `dspace-8_x` branch for next bug-fix release port to main This PR needs to be ported to `main` branch for the next major release
Projects
Status: 🙋 Needs Reviewers Assigned
Development

Successfully merging this pull request may close these issues.

2 participants