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

chore: Prefetch consolidated api using service worker #33306

Merged
merged 36 commits into from May 16, 2024

Conversation

dvj1988
Copy link
Contributor

@dvj1988 dvj1988 commented May 9, 2024

Description

Prefetch the consolidated API using the service worker. This api is prefetched for the view and edit urls.

Fixes Issue URL

Warning

If no issue exists, please create an issue first, and check with the maintainers if the issue is valid.

Automation

/ok-to-test tags="@tag.All"

🔍 Cypress test results

Tip

🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
Workflow run: https://github.com/appsmithorg/appsmith/actions/runs/9108829019
Commit: 30e2db9
Cypress dashboard url: Click here!

Communication

Should the DevRel and Marketing teams inform users about this change?

  • Yes
  • No

@dvj1988
Copy link
Contributor Author

dvj1988 commented May 9, 2024

/build-deploy-preview skip-tests=true

Copy link

github-actions bot commented May 9, 2024

Deploying Your Preview: https://github.com/appsmithorg/appsmith/actions/runs/9012824234.
Workflow: On demand build Docker image and deploy preview.
skip-tests: true.
env: ``.
PR: 33306.
recreate: .

@dvj1988 dvj1988 changed the title WIP chore: Prefetch consolidated api using service worker May 10, 2024
@github-actions github-actions bot added the skip-changelog Adding this label to a PR prevents it from being listed in the changelog label May 10, 2024
@dvj1988
Copy link
Contributor Author

dvj1988 commented May 10, 2024

/build-deploy-preview skip-tests=true

Copy link

Deploying Your Preview: https://github.com/appsmithorg/appsmith/actions/runs/9027698556.
Workflow: On demand build Docker image and deploy preview.
skip-tests: true.
env: ``.
PR: 33306.
recreate: .

@@ -21,4 +21,32 @@ module.exports = merge(common, {
experiments: {
cacheUnaffected: true,
},
webpack: {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Revert this. This change is required only for testing

Copy link
Contributor

Choose a reason for hiding this comment

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

Does this change need to be reverted ?

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 can leave it as is and enable it for dev mode.

Just FYI there are some warnings that popup in the terminal as result of enabling WorkboxPlugin in dev mode. This is a known issue for webpack watch mode (InjectManifest has been called multiple times, perhaps due to running webpack in --watch mode.)

Copy link
Collaborator

Choose a reason for hiding this comment

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

@dvj1988 Does it affect the build time somehow? Did you check it?

We have some issues with building time, if it is not necessary, then I would prefer not to add anything here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have not seen any issues with the dev server build time or hot reloads. Preferably we want to keep this so that we have the same behaviour of the service worker in the dev and prod modes.
Earlier we were using service worker to cache the static assets but this PR is prefetching and caching one of the apis. If there are any main bundle changes that would affect the behaviour of the service worker or vice versa we want to start catching them in the dev mode itself.
After the merge I will keep a track of whether this is affecting any devs local setup and can take a measure accordingly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@KelvinOm Do you have any specific scenario in mind to test?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@dvj1988 I am most interested in the start time (with and without cache) and the recompilation time. We also have problems OOM issues, this should not make the situation worse.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@KelvinOm By cache do you mean lint cache?
How do you propose I measure these?

Copy link
Collaborator

@KelvinOm KelvinOm May 16, 2024

Choose a reason for hiding this comment

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

By cache I mean babel-loader cache it stored at app/client/node_modules/.cache/babel-loader. It is created when the webpack is running.

How do you propose I measure these?

I do not know a good way to do this. I'm measuring the time in terminal. We can use time like time yarn start. I couldn't run the webpack speed measure plugin because it doesn't work with craco. So we have to measure it manually.

Copy link
Contributor Author

@dvj1988 dvj1988 May 16, 2024

Choose a reason for hiding this comment

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

@KelvinOm I used the package called gnomon. And the time taken looks the same.

Start time without cache was replicated by following the steps

  • Checkout the branch
  • delete node_modules
  • install deps
  • comment out the cache settings in craco.dev.config
  • start the server
yarn start | gnomon --type=elapsed-total

Tests done in: Apple M1 Pro, RAM 32 GB

Branch Start time without cache Start time with cache Recompilation Time
release 190-192s 12-13s 4-6s
chore/prefetch-consolidated-api 190-192s 12-13s 4-6s

Start time without cache - release
Screenshot 2024-05-16 at 4 44 52 PM
Start time with cache - release
Screenshot 2024-05-16 at 4 47 10 PM
Start time without cache - chore/prefetch-consolidated-api
Screenshot 2024-05-16 at 5 39 47 PM
Start time with cache - chore/prefetch-consolidated-api
Screenshot 2024-05-16 at 5 12 39 PM

Copy link

Deploy-Preview-URL: https://ce-33306.dp.appsmith.com

@dvj1988
Copy link
Contributor Author

dvj1988 commented May 10, 2024

/build-deploy-preview skip-tests=true

Copy link

Deploying Your Preview: https://github.com/appsmithorg/appsmith/actions/runs/9030519052.
Workflow: On demand build Docker image and deploy preview.
skip-tests: true.
env: ``.
PR: 33306.
recreate: .

Copy link

Deploy-Preview-URL: https://ce-33306.dp.appsmith.com

@dvj1988
Copy link
Contributor Author

dvj1988 commented May 12, 2024

/build-deploy-preview skip-tests=true

Copy link

Deploying Your Preview: https://github.com/appsmithorg/appsmith/actions/runs/9053293027.
Workflow: On demand build Docker image and deploy preview.
skip-tests: true.
env: ``.
PR: 33306.
recreate: .

Copy link

Deploy-Preview-URL: https://ce-33306.dp.appsmith.com

app/client/src/App.css Outdated Show resolved Hide resolved
app/client/src/ce/AppRouter.tsx Outdated Show resolved Hide resolved
@dvj1988
Copy link
Contributor Author

dvj1988 commented May 13, 2024

/build-deploy-preview skip-tests=true

Copy link

Deploying Your Preview: https://github.com/appsmithorg/appsmith/actions/runs/9056956178.
Workflow: On demand build Docker image and deploy preview.
skip-tests: true.
env: ``.
PR: 33306.
recreate: .

Copy link

Deploy-Preview-URL: https://ce-33306.dp.appsmith.com

@dvj1988 dvj1988 added the ok-to-test Required label for CI label May 13, 2024
Copy link

Deploying Your Preview: https://github.com/appsmithorg/appsmith/actions/runs/9101177210.
Workflow: On demand build Docker image and deploy preview.
skip-tests: true.
env: ``.
PR: 33306.
recreate: .

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Copy link

Deploy-Preview-URL: https://ce-33306.dp.appsmith.com

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

app/client/src/utils/serviceWorkerUtils.js Outdated Show resolved Hide resolved
@dvj1988 dvj1988 added ok-to-test Required label for CI and removed ok-to-test Required label for CI labels May 15, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

app/client/src/utils/serviceWorkerUtils.js Show resolved Hide resolved
Comment on lines 137 to 157
async cacheConsolidatedApi(request) {
// Acquire the lock
await this.consolidatedApiFetchmutex.acquire();

try {
const response = await fetch(request);

if (response.ok) {
// Clone the response as the response can be consumed only once
const clonedResponse = response.clone();
// Put the response in the cache
await this.cache.put(request, clonedResponse);
}
} catch (error) {
// Delete the existing cache if the fetch fails
await this.cache.delete(request);
} finally {
// Release the lock
this.consolidatedApiFetchmutex.release();
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a return statement to indicate success or failure in cacheConsolidatedApi.

To improve clarity, add a return statement that indicates whether the caching was successful.

  async cacheConsolidatedApi(request) {
    // Acquire the lock
    await this.consolidatedApiFetchmutex.acquire();

    try {
      const response = await fetch(request);

      if (response.ok) {
        // Clone the response as the response can be consumed only once
        const clonedResponse = response.clone();
        // Put the response in the cache
        await this.cache.put(request, clonedResponse);
+       return true;
      }
    } catch (error) {
      // Delete the existing cache if the fetch fails
      await this.cache.delete(request);
+     return false;
    } finally {
      // Release the lock
      this.consolidatedApiFetchmutex.release();
    }
+   return false;
  }

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
async cacheConsolidatedApi(request) {
// Acquire the lock
await this.consolidatedApiFetchmutex.acquire();
try {
const response = await fetch(request);
if (response.ok) {
// Clone the response as the response can be consumed only once
const clonedResponse = response.clone();
// Put the response in the cache
await this.cache.put(request, clonedResponse);
}
} catch (error) {
// Delete the existing cache if the fetch fails
await this.cache.delete(request);
} finally {
// Release the lock
this.consolidatedApiFetchmutex.release();
}
}
async cacheConsolidatedApi(request) {
// Acquire the lock
await this.consolidatedApiFetchmutex.acquire();
try {
const response = await fetch(request);
if (response.ok) {
// Clone the response as the response can be consumed only once
const clonedResponse = response.clone();
// Put the response in the cache
await this.cache.put(request, clonedResponse);
return true;
}
} catch (error) {
// Delete the existing cache if the fetch fails
await this.cache.delete(request);
return false;
} finally {
// Release the lock
this.consolidatedApiFetchmutex.release();
}
return false;
}

@rajatagrawal
Copy link
Contributor

Suggested refactor to comply with single responsibility principle and some suggested naming changes to make code more generic. The code logic is fine and untouched. Just a suggestion on an iteration of code organization. I have suggested the changes in a patch file. The patch can be applied locally to see the suggested changes.

gitdiffpatch.zip

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

app/client/src/serviceWorker.js Outdated Show resolved Hide resolved
app/client/src/serviceWorker.js Show resolved Hide resolved
@dvj1988 dvj1988 added ok-to-test Required label for CI and removed ok-to-test Required label for CI labels May 16, 2024
@dvj1988
Copy link
Contributor Author

dvj1988 commented May 16, 2024

/build-deploy-preview skip-tests=true

Copy link

Deploying Your Preview: https://github.com/appsmithorg/appsmith/actions/runs/9108873127.
Workflow: On demand build Docker image and deploy preview.
skip-tests: true.
env: ``.
PR: 33306.
recreate: .

Copy link

Deploy-Preview-URL: https://ce-33306.dp.appsmith.com

@@ -21,4 +21,32 @@ module.exports = merge(common, {
experiments: {
cacheUnaffected: true,
},
webpack: {
Copy link
Collaborator

Choose a reason for hiding this comment

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

@dvj1988 Does it affect the build time somehow? Did you check it?

We have some issues with building time, if it is not necessary, then I would prefer not to add anything here.

@dvj1988 dvj1988 requested a review from rajatagrawal May 16, 2024 09:14
@dvj1988 dvj1988 requested a review from KelvinOm May 16, 2024 12:22
@dvj1988 dvj1988 merged commit 992790d into release May 16, 2024
82 checks passed
@dvj1988 dvj1988 deleted the chore/prefetch-consolidated-api branch May 16, 2024 12:33
dvj1988 added a commit that referenced this pull request May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ok-to-test Required label for CI skip-changelog Adding this label to a PR prevents it from being listed in the changelog Task A simple Todo
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Task]: Prefetch consolidated api using the service worker
4 participants