This repository was archived by the owner on Oct 14, 2024. It is now read-only.
feat(transfer-state): add an interceptor & a service to cache HTTP requests #509
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Cache all the HTTP requests on the server by using
TransferState
API, and return the HTTP response from the cache if exist and it's on the browser and only if it's for the first time.How does this work
It uses a new interceptor (
transfer-state.interceptor
) and a service (transfer-state.service
) to set, get and manage the cache.Why is this needed
The first thing user needs it the same ability to just run the app and see the result as he/she expect, and they would think something is broken when they see the flicker caused by the second HTTP request (which is not necessary!) on the browser.
There are lots of different issued filled on this repository, Universal, Angular, Angular-cli, and StackOverflow that developers are looking for an answer, and using a cache mechanism that does this for them seems to be a good idea.
Here are just some of the issues filled for the second HTTP request on the browser:
angular/angular-cli#7477 angular/universal#338 angular/universal#845 angular/angular#20638 #497 #62 #61 #139 #439 #429 #139 angular/universal#436 #7 #57 ...
Thanks to @vikerman this problem can be easily fixed by using
TransferState
, but it needs some efforts to implement it, and since this repository is a starter-kit for Universal, it's good to have the best practice way to show how to implementTransferState
in an Angular app.@vikerman @Toxicable and @MarkPieszak Can you please let us know your idea of having such a feature in universal-starter?
@vikerman I experienced some strange behavior from
TransferState
which might be my wrong. But if you think we can have this in universal-starter, we can fix the way I usedTransferState