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

Task: Add browser storage for resources #2373

Merged
merged 30 commits into from
Apr 5, 2023

Conversation

MaryannGitonga
Copy link
Contributor

@MaryannGitonga MaryannGitonga commented Feb 1, 2023

Overview

Closes #2369

Demo

  • When the first fetch is unsuccessful, and the storage instance is empty, this is displayed to the user:
    image

  • Otherwise, the resources are displayed (served from DevX API or the browser storage when DevX API is down).

Notes

Optional. Ancillary topics, caveats, alternative strategies that didn't work out, anything else.

Testing Instructions

  • Checkout to this branch.
  • Run the local instance of GE.
  • Navigate to the Resources tab.
  • Observe changes.

@MaryannGitonga MaryannGitonga changed the title Task: Add in-browser cashing for resources Task: Add browser cashing for resources Feb 1, 2023
@thewahome thewahome changed the title Task: Add browser cashing for resources Task: Add browser caching for resources Feb 1, 2023
@thewahome
Copy link
Collaborator

We should remember to remove the local json files

Copy link
Member

@gavinbarron gavinbarron left a comment

Choose a reason for hiding this comment

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

Using a cached data ONLY as a fallback is a missed opportunity to reduce COGS and improve speed.

const response = await fetch(resourcesUrl, options);
if (response.ok) {
const resources = await response.json() as IResource;
return dispatch(fetchResourcesSuccess(resources));
}
throw response;
} catch (error) {
const cachedResources = await resourcesCache.readResources();
Copy link
Member

Choose a reason for hiding this comment

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

IMO we should be using the cached data in preference to hitting the DevX API every time, it will be much quicker.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We only call the api once when the page loads. And there are no use cases where for the app to work a reload is required.
I'm not sure that there'd be a use case for the preference

Copy link
Member

Choose a reason for hiding this comment

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

Our users never hit F5? or visit multiple times a day?

Copy link
Collaborator

Choose a reason for hiding this comment

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

that could happen.
To prefer cache, we'd need to make it able to expire so that we can sometimes get the latest.

Copy link
Member

@gavinbarron gavinbarron Feb 8, 2023

Choose a reason for hiding this comment

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

Yeah, we would need to also store a refresh interval, note, this not an expiry as we wouldn't remove anything from the cache, just have a point in time that we'd try to update the content of the cache after.

That said, I've been thinking on this a bit and chatting with @darrelmiller
We should be looking to ensure that the DevX API provides appropriate cache control headers and not worrying about adding a complex caching mechanism here.

Honestly the less code we write the better in terms of cost of ownership, and it allows us to focus on other areas.

Do we know how frequently the data in the DevX API is updated?

Copy link
Collaborator

Choose a reason for hiding this comment

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

We had a chat with @darrelmiller and we uncovered something I consider a large spanner in the works. The cache control headers do not work in instances where the client and the api are not in the same domain.
An alternative exists that we use service workers to try this. However I am not sure whether the code changes will be any less than the minimal work we have done now.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@gavinbarron, @darrelmiller I am more inclined to :

  • following your suggesion to prefer cache.
  • coming up with a mechanism to expire cache. For resources, we could take advantage of knowledge that the openapi document is refreshed every week. Or simply set cache to expire after 7 days

Tell me what you think.

Copy link
Member

Choose a reason for hiding this comment

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

The cache control headers do not work in instances where the client and the api are not in the same domain.

O_o

I'm really surprised to hear this because this is literally the situation for resources served from a CDN, those are always cross domain and cache in the browser based on cache control headers.

Is there a way I can take a look at what you're seeing to validate this?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@darrelmiller found this when searching. It was a shared screen where we were able to see the results of the query together.

I've been looking this up and it looks like it could be possible even when the client and the API are not in the same domain as long as the client origin is allowed in the API.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, we should be able to set the allowed headers in the response to the options calls from the server:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers

@@ -183,7 +184,7 @@ const UnstyledResourceExplorer = (props: any) => {

return (
<section style={{ marginTop: '8px' }}>
{!isolated && <>
{!isolated && items[0].links.length > 0 && <>
Copy link
Member

Choose a reason for hiding this comment

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

Is items always populated?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image
This is what the condition is trying to preempt from rendering: the search box and the switch to beta toggle button.

Co-authored-by: Gavin Barron <[email protected]>
@MaryannGitonga MaryannGitonga changed the title Task: Add browser caching for resources Task: Add browser storage for resources Feb 2, 2023
@sonarqubecloud
Copy link

sonarqubecloud bot commented Feb 8, 2023

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

37.8% 37.8% Coverage
0.0% 0.0% Duplication

@github-actions
Copy link
Contributor

github-actions bot commented Feb 8, 2023

Azure Static Web Apps: Your stage site is ready! Visit it here: https://jolly-sand-0ac78c710-2373.centralus.azurestaticapps.net

Onokaev
Onokaev previously approved these changes Mar 31, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Apr 4, 2023

Azure Static Web Apps: Your stage site is ready! Visit it here: https://jolly-sand-0ac78c710-2373.centralus.azurestaticapps.net

@github-actions
Copy link
Contributor

github-actions bot commented Apr 5, 2023

Azure Static Web Apps: Your stage site is ready! Visit it here: https://jolly-sand-0ac78c710-2373.centralus.azurestaticapps.net

@ElinorW ElinorW requested a review from Onokaev April 5, 2023 12:08
@github-actions
Copy link
Contributor

github-actions bot commented Apr 5, 2023

Azure Static Web Apps: Your stage site is ready! Visit it here: https://jolly-sand-0ac78c710-2373.centralus.azurestaticapps.net

@thewahome thewahome merged commit 75d2f5b into dev Apr 5, 2023
@sonarqubecloud
Copy link

sonarqubecloud bot commented Apr 5, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@thewahome thewahome deleted the fix/in-browser-caching-resources branch May 3, 2023 07:35
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.

Add in-browser caching for resources
5 participants