Skip to content

FOUR-20933: Implement ETag Caching for Screens Data #7771

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

Merged
merged 6 commits into from
Dec 2, 2024

Conversation

eiresendez
Copy link
Contributor

@eiresendez eiresendez commented Nov 26, 2024

This PR introduces foundational support for ETag-based caching in the application. The implementation includes a middleware to handle HTTP conditional requests and an extensible manager to generate ETags based on different strategies.

Solution

  1. ETag Middleware (HandleEtag):
  • Handles If-None-Match headers for caching.
  • Supports safe HTTP methods (GET, HEAD).
  • Responds with 304 Not Modified when ETag matches.
  • Restores the original HTTP method after processing (HEAD to GET).
  1. ETag Manager:
  • Generates default ETags using an MD5 hash of the response content.
  • Supports custom algorithms (e.g., SHA-256) through callbacks.
  1. Tests:
  • Verify default ETag generation using MD5.
  • Ensure custom ETag generation works via callback.
  • Validate IfNoneMatch middleware behavior for matching and non-matching ETags.

How to Test

  1. Initial Request:
  • The first time the endpoint is called, a 200 status response is returned.
  • The response content is retrieved from the server and cached.
  1. Subsequent Requests (Page Refresh):
  • On subsequent page refreshes, since the response is already cached, a 304 status is returned.
  • This improves response time and reduces bandwidth usage.
  1. Content Updates:
  • If the response content changes (e.g., the screen is modified or updated) a 200 status is returned again and the new content is reloaded.
  • On subsequent refreshes, the updated content will remain cached and the server will respond with 304 status.

Endpoints:
Screen Responses in Core:
GET https://processmaker.test/api/1.1/tasks/{param_1}/screen?include=screen,nested&screen_version={param_2}

Screen Responses in Web Entry:
GET https://processmaker.test/api/1.0/webentry/{param_1}/{param_2}/screens/{param_3}?include=nested

Related Tickets & Packages

Code Review Checklist

  • I have pulled this code locally and tested it on my instance, along with any associated packages.
  • This code adheres to ProcessMaker Coding Guidelines.
  • This code includes a unit test or an E2E test that tests its functionality, or is covered by an existing test.
  • This solution fixes the bug reported in the original ticket.
  • This solution does not alter the expected output of a component in a way that would break existing Processes.
  • This solution does not implement any breaking changes that would invalidate documentation or cause existing Processes to fail.
  • This solution has been tested with enterprise packages that rely on its functionality and does not introduce bugs in those packages.
  • This code does not duplicate functionality that already exists in the framework or in ProcessMaker.
  • This ticket conforms to the PRD associated with this part of ProcessMaker.

- Introduced IfNoneMatch middleware to handle ETag-based caching:
  - Supports safe HTTP methods (GET, HEAD).
  - Returns "304 Not Modified" for matching ETags.
  - Handles multiple ETags and restores original request methods.

- Added EtagManager for flexible ETag generation:
  - Default ETag generation uses MD5 hash.
  - Supports custom hash algorithms like SHA-256 via callbacks.

- Included test cases:
  - Validate default ETag generation.
  - Test custom callback logic for ETag creation.

This commit adds foundational support for ETag-based caching in the application.
…ware

- Merged `etag.set` and `etag.if-none-match` middlewares into a single `etag.handle` middleware.
- Simplified logic to reduce redundancy and improve maintainability.
- Ensured ETag validation (`If-None-Match`) and generation are handled in the same flow.
- Preserved compatibility with HEAD requests for consistency.

This refactor improves clarity, reduces potential misconfigurations, and keeps the ETag logic centralized.
…d tests

- Created tests for the new `HandleEtag` middleware:
  - Validates ETag generation and correctness.
  - Tests responses for both matching and non-matching `If-None-Match` headers.
  - Ensures proper handling of weak ETags (`W/`).
- Removed old tests for `SetEtag` and `IfNoneMatch` middlewares as they are no longer needed.

This commit improves test clarity and ensures the new ETag middleware behaves as expected.
- Added a test to validate ETag generation for user-specific routes using `etag:user`.
  - Simulates an authenticated user and verifies the ETag includes the user ID.

These tests ensure the ETag middleware behaves correctly for both user-specific and common routes.
@eiresendez eiresendez marked this pull request as ready for review November 28, 2024 20:09
Copy link
Contributor

@estebangallego estebangallego left a comment

Choose a reason for hiding this comment

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

Works as expected!

@eiresendez eiresendez merged commit c283de6 into epic/FOUR-20929 Dec 2, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants