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

feat(http): allow HttpClient to cache requests #49509

Closed
wants to merge 3 commits into from

Conversation

alan-agius4
Copy link
Contributor

@alan-agius4 alan-agius4 commented Mar 21, 2023

This commit adds a new option for provideHttpClient called withTransferCache(). When this option is passed, requests done on the server are cached and reused during the bootstrapping of the application in the browser thus avoiding duplicate requests and reducing load time.

This is the same as TransferHttpCacheModule in https://github.com/angular/universal/blob/main/modules/common/src/transfer_http.ts and is part of the effort to move "common" logic that can be used without "@nguniversal/common" into the framework.

@alan-agius4 alan-agius4 added the target: minor This PR is targeted for the next minor release label Mar 21, 2023
@alan-agius4 alan-agius4 added the action: review The PR is still awaiting reviews from at least one requested reviewer label Mar 21, 2023
@alan-agius4 alan-agius4 marked this pull request as ready for review March 21, 2023 10:22
Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

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

@alan-agius4 looks great! 👍

packages/common/http/src/provider.ts Outdated Show resolved Hide resolved
packages/common/http/src/provider.ts Outdated Show resolved Hide resolved
packages/common/http/src/provider.ts Outdated Show resolved Hide resolved
packages/common/http/src/transfer_cache.ts Outdated Show resolved Hide resolved
@pullapprove pullapprove bot requested a review from alxhub March 22, 2023 00:06
packages/common/http/src/transfer_cache.ts Outdated Show resolved Hide resolved
packages/common/http/src/transfer_cache.ts Show resolved Hide resolved
packages/common/http/src/transfer_cache.ts Outdated Show resolved Hide resolved
@alan-agius4 alan-agius4 force-pushed the transfer-http branch 5 times, most recently from ba53c71 to b1ff62a Compare March 22, 2023 12:25
bougwal added a commit to bougwal/universal that referenced this pull request Mar 22, 2023
As per the discussion and the nice point made by @alan-agius4 regarding the TransferHttpResponse shape under angular/angular#49509. I suggest to align the status of optional and required keys of TransferHttpResponse interface of universal interceptor to meet Angular's packages/common/http/src/transfer_cache.ts counterpart.
bougwal pushed a commit to bougwal/universal that referenced this pull request Mar 22, 2023
As per the discussion and the nice point made by @alan-agius4 regarding the TransferHttpResponse shape under angular/angular#49509. I suggest to align the status of optional and required keys of TransferHttpResponse interface of universal's interceptor to meet Angular's packages/common/http/src/transfer_cache.ts counterpart.
bougwal pushed a commit to bougwal/universal that referenced this pull request Mar 22, 2023
…and headers

As per the discussion and the nice point made by @alan-agius4 regarding the TransferHttpResponse shape under angular/angular#49509. I suggest to align the status of optional and required keys of TransferHttpResponse interface of universal's interceptor to meet Angular's packages/common/http/src/transfer_cache.ts counterpart.
bougwal pushed a commit to bougwal/universal that referenced this pull request Mar 23, 2023
…and headers

As per the discussion and the nice point made by @alan-agius4 regarding the TransferHttpResponse shape under angular/angular#49509. I suggest to align the status of optional and required keys of TransferHttpResponse interface of universal's interceptor to meet Angular's packages/common/http/src/transfer_cache.ts counterpart.
bougwal added a commit to bougwal/universal that referenced this pull request Mar 23, 2023
…and headers

As per the discussion and the nice point made by @alan-agius4 regarding the TransferHttpResponse shape under angular/angular#49509. I suggest to align the status of optional and required keys of TransferHttpResponse interface of universal's interceptor to meet Angular's packages/common/http/src/transfer_cache.ts counterpart.
bougwal added a commit to bougwal/universal that referenced this pull request Mar 23, 2023
…and headers

As per the discussion and the nice point made by @alan-agius4 regarding the TransferHttpResponse shape under angular/angular#49509. I suggest to align the status of optional and required keys of TransferHttpResponse interface of universal's interceptor to meet Angular's packages/common/http/src/transfer_cache.ts counterpart.
bougwal added a commit to bougwal/universal that referenced this pull request Mar 24, 2023
…and headers

As per the discussion and the nice point made by @alan-agius4 regarding the TransferHttpResponse shape under angular/angular#49509. I suggest to align the status of optional and required keys of TransferHttpResponse interface of universal's interceptor to meet Angular's packages/common/http/src/transfer_cache.ts counterpart.
Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

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

Looks great! 👍 Just left a few comments.

packages/common/http/src/provider.ts Outdated Show resolved Hide resolved
const appRef = inject(ApplicationRef);

let isCacheActive = true;
appRef.isStable
Copy link
Contributor

Choose a reason for hiding this comment

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

We can also take the InitialRenderPendingTasks status into account as well (similar to utils.ts in the platform-server).

Copy link
Contributor

Choose a reason for hiding this comment

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

It also looks like we'd need to create a helper method for this (as a followup), since we use this "signal" in multiple places now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We indeed do as there are several places use appRef.isStable were currently InitialRenderPendingTasks is not considered.

I did try to use integrate the usage of InitialRenderPendingTasks but promise was always resolved immediate as collection was empty as I was accessing whenAllTasksComplete in an ENVIRONMENT_INITIALIZER which makes accessing/checking InitialRenderPendingTasks redundant.

I think this is one of the problem that I highlighted previously, that we need to solve.

Copy link
Contributor

Choose a reason for hiding this comment

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

We can probably change ENVIRONMENT_INITIALIZER -> APP_BOOTSTRAP_LISTENER, which is used in the hydration code:
https://github.com/angular/angular/blob/main/packages/core/src/hydration/api.ts#L163

In this case, the InitialRenderPendingTasks state would reflect the state of an initial navigation. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah that should work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

packages/common/http/src/transfer_cache.ts Outdated Show resolved Hide resolved
packages/common/http/src/transfer_cache.ts Show resolved Hide resolved
/**
* A multi-provided token of `HttpInterceptorFn`s that are only set in root.
*/
export const HTTP_ROOT_INTERCEPTOR_FNS = new InjectionToken<HttpInterceptorFn[]>(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there a cleaner way?

Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of having a default factory, can you just inject it optionally below?

this.injector.get(HTTP_ROOT_INTERCEPTOR_FNS, [])

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call.

@alan-agius4 alan-agius4 added the action: review The PR is still awaiting reviews from at least one requested reviewer label Mar 31, 2023
@alan-agius4
Copy link
Contributor Author

Note: I did not include this as part of a private/public API intentionally until we we finalise the decision on how to consume it.

bougwal added a commit to bougwal/universal that referenced this pull request Apr 1, 2023
…and headers

As per the discussion and the nice point made by @alan-agius4 regarding the TransferHttpResponse shape under angular/angular#49509. I suggest to align the status of optional and required keys of TransferHttpResponse interface of universal's interceptor to meet Angular's packages/common/http/src/transfer_cache.ts counterpart.
This commit adds a new option for `provideHttpClient` called
`withHttpTransferCache()`. When this option is passed, requests done on the server are cached and reused during the  bootstrapping of the application in the browser thus avoiding duplicate requests and reducing load time.

This is the same as `TransferHttpCacheModule` in https://github.com/angular/universal/blob/main/modules/common/src/transfer_http.ts
Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

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

Looks great, thanks Alan! 👍

@AndrewKushnir AndrewKushnir removed the action: review The PR is still awaiting reviews from at least one requested reviewer label Apr 3, 2023
@alan-agius4 alan-agius4 added the action: merge The PR is ready for merge by the caretaker label Apr 3, 2023
@dylhunn
Copy link
Contributor

dylhunn commented Apr 4, 2023

This PR was merged into the repository by commit aff1512.

@dylhunn dylhunn closed this in aff1512 Apr 4, 2023
@alan-agius4 alan-agius4 deleted the transfer-http branch April 4, 2023 04:53
alan-agius4 pushed a commit to angular/universal that referenced this pull request Apr 12, 2023
…and headers

As per the discussion and the nice point made by @alan-agius4 regarding the TransferHttpResponse shape under angular/angular#49509. I suggest to align the status of optional and required keys of TransferHttpResponse interface of universal's interceptor to meet Angular's packages/common/http/src/transfer_cache.ts counterpart.
alan-agius4 pushed a commit to angular/universal that referenced this pull request Apr 12, 2023
…and headers

As per the discussion and the nice point made by @alan-agius4 regarding the TransferHttpResponse shape under angular/angular#49509. I suggest to align the status of optional and required keys of TransferHttpResponse interface of universal's interceptor to meet Angular's packages/common/http/src/transfer_cache.ts counterpart.

(cherry picked from commit 654ac18)
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators May 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: common/http detected: feature PR contains a feature commit target: minor This PR is targeted for the next minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants