Skip to content

Conversation

drdreo
Copy link
Contributor

@drdreo drdreo commented Jun 16, 2025

PR Checklist

  • The commit message follows our guidelines: CONTRIBUTING.md#commit
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[x] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[x] Documentation content changes
[x] Other... Please describe: Ran `prettier . --write` to format the project

What is the current behavior?

The library required a provider for local storage strategy, and consumers had to use it exclusively. Storage strategy was not configurable per request.

Issue Number: 105

What is the new behavior?

Consumers can now specify the storage strategy (e.g., localStorage, sessionStorage, memory) per request using the withCache context. The old provider is deprecated but still available for backward compatibility.

Does this PR introduce a breaking change?

Angular 20 migration included.

[x] Yes
[] No

Other Info

  • The provideHttpCacheLocalStorageStrategy is no longer needed and was deprecated.
    Documentation and tests have been updated to reflect the new usage.
  • Migrated to Angular 20 while at it.
  • sessionStorage added
  • cache manager has basic tests again

Copy link

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@netanel-utila
Copy link

Hi, do you think you can upgrade to Angular 20?

@drdreo
Copy link
Contributor Author

drdreo commented Jun 16, 2025

Sure thing, right on it. Will then be a breaking release tho.

private _getCacheServices(storageStrategy?: CacheStorageStrategy): ResolvedCacheServices {
if (storageStrategy === 'localStorage') {
const lsStorage = this.injector.get(LocalStorageHttpCacheStorage, null, { optional: true });
const lsTtl = this.injector.get(LocalStorageTTLManager, null, { optional: true });
Copy link
Member

Choose a reason for hiding this comment

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

if you're importing the services, it's still make it not tree-shakable :)

but never mind, it's not a lot of a code, don't bother

Copy link
Contributor Author

@drdreo drdreo Jun 19, 2025

Choose a reason for hiding this comment

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

Refactored it to use injection tokens instead and a generic browser storage class

ttlManager.clear();
versionsManager.clear();
} else {
// Clear all storages (memory, localStorage and sessionStorage if available)
Copy link
Member

Choose a reason for hiding this comment

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

why we need to clear all? there is one global strategy

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since we now have multiple storages at the same time, i thought this could be a way to provide granular control of what a user wants to clear.

Clear all caches (memory, local storage, etc.)

 clearCache() {
    this.manager.clear();
  }

Clear just 1 specific cache storage, e.g.: localStorage

 clearCache() {
    this.manager.clear('localStorage');
  }

}

@Injectable()
export class SessionStorageTTLManager extends TTLManager {
Copy link
Member

Choose a reason for hiding this comment

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

all session and local storage can be use the same base class just pass super(sessionStorage/localStorage)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@NetanelBasal
Copy link
Member

The commit should include the breaking changes for the release

@drdreo
Copy link
Contributor Author

drdreo commented Jun 23, 2025

The commit should include the breaking changes for the release

The whole PR or which one do you refer to? Since the Angular upgrade was already a breaking one i reckon.

@NetanelBasal
Copy link
Member

Any breaking change that was introduced in this PR

@drdreo
Copy link
Contributor Author

drdreo commented Jun 24, 2025

Yeah I marked the commit for the angular migration to v20 as breaking. But otherwise it should not break the current local storage implementation.

@NetanelBasal
Copy link
Member

The build needs a fix

@drdreo
Copy link
Contributor Author

drdreo commented Jun 24, 2025

Updated CI workflow to use latest action versions and from node 18 --> 22.
Also aligned the ts lib to be es2022 like the other tsconfigs

@NetanelBasal NetanelBasal merged commit 2057210 into ngneat:master Jun 24, 2025
1 check passed
@drdreo drdreo deleted the add-storage-strategy branch June 24, 2025 15:02
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.

3 participants