Skip to content

[v1.38.x] Send less requests to scratch.mit.edu/session etc. - #7666

Merged
WorldLanguages merged 15 commits into
ScratchAddons:v1.38.xfrom
WorldLanguages:send-less-requests
Jul 23, 2024
Merged

[v1.38.x] Send less requests to scratch.mit.edu/session etc.#7666
WorldLanguages merged 15 commits into
ScratchAddons:v1.38.xfrom
WorldLanguages:send-less-requests

Conversation

@WorldLanguages

@WorldLanguages WorldLanguages commented Jul 20, 2024

Copy link
Copy Markdown
Member

Resolves comment #7651 (comment)
Related: #7648

When viewing the diff, I recommend you enable "hide whitespace".

Changes

This PR intends to make very small changes/optimizations to reduce the load on Scratch servers. It should also slightly help with battery and data usage.

These are small code changes. Big restructurings are intentionally not made.
The changes are made in such a way that anyone can clearly see that, after the changes, it's impossible that the extension sends more than requests than before.

Background process (service worker / event page)

The amount of requests to /session will decrease significantly. This is strongly related to the Manifest V3 lifecycle and using chrome.storage.session to cache data.

  • Previously, /session would be fetched each time the service worker wakes up.
  • Now, the response to /session is cached through chrome.storage.session and the amount of background requests to that endpoint should be similar to our old Manifest V2 versions.
  • The function that previously caused an infinite loop (addToQueue) remains intentionally unchanged. It may still request /session if Scratch cookies changed.

Non-addon code running in Scratch tabs (content scripts in both worlds)

The amount of requests to /session will decrease significantly.

  • In scratch-www pages, such as the front page, projects, and studios, we don't need to fetch /session ourselves on page load, as Scratch will do this automatically. We can obtain the response data either by polluting XHR or by looking at Redux. (In this PR I fetch XHR as it's the easiest and more reliable option).
  • If no addon calls addon.auth.fetchX (X can be "Username", "IsLoggedIn", "UserId" or "XToken") there is no need to fetch /session. In fact, in non-www pages (that is, "scratchr2" pages) no requests to /session will be sent at all, until one of the running userscripts calls one of these methods.
  • In non-www pages (that is, "scratchr2" pages) there is no need to fetch /session more than once. Only in some www pages the user can log in or out without a reload. This may mean data gets out of sync if the user logs in to a different account, but that's what already happens on vanilla Scratch, and what tricks like "follow yourself" rely on.

Addons

  • The forum-post-countdown addon reuses the session data fetched by the isLoggedIn() call, so overall 1 request is sent instead of two.
  • exact-count: Do not fetch exact project count if it does not exceed 100. Do not fetch exact favorites and studio counts. (May revert this last decision if there's user feedback about it, see issue exact-count: rethink exact counts for profiles #7623)

Extension popup

No changes

Tests

Have to test

@WorldLanguages WorldLanguages added type: enhancement New feature for the project scope: addon api Related to the addon.* JS APIs or other ways for addons to provide features priority: 3 Medium priority. Includes bugs and useful features scope: addon Related to one or multiple addons scope: core Related to the core script/extension workings labels Jul 20, 2024
@WorldLanguages WorldLanguages self-assigned this Jul 20, 2024
Comment thread background/handle-auth.js Outdated

@Samq64 Samq64 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't have a new Scratcher to test the 120 second rule.

@Samq64 Samq64 added the status: needs review PR needs 1 more approval label Jul 21, 2024
@WorldLanguages WorldLanguages removed the status: needs review PR needs 1 more approval label Jul 23, 2024
@WorldLanguages
WorldLanguages merged commit e939152 into ScratchAddons:v1.38.x Jul 23, 2024
WorldLanguages added a commit to WorldLanguages/ScratchAddons that referenced this pull request Jul 28, 2024
…chAddons#7666)

* Initial commit

* Format code

* Fix TODO: handle chrome.storage.session undefined

* Remove invalid TODO

* Remove console.log

* Background: only use cache on first time

* Revert canUseCachedSession

* exact-count addon changes (profiles)

* Change animated-thumb later

* Only call refreshFn once

* Use underscore for _requestFetchFn attr

* Remove outdated comment

* Do not reference isScratchGui variable

* Add code comment

* Fix execution order

---------

Co-authored-by: WorldLanguages <WorldLanguages@users.noreply.github.com>
WorldLanguages added a commit that referenced this pull request Jul 31, 2024
…7650)

* [v1.38.x] Stop fetching scratch.mit.edu/csrf_token and more optimizations (#7647)

* Use getAllCookieStores() instead of fetching hack

* Call background alarms less often (`msg-count-badge` affected) (#7505)

* Remove unnecessary `cleanCsInfoCache` alarm

* Fetch messages less often if user inactive

* Format code

---------

Co-authored-by: WorldLanguages <WorldLanguages@users.noreply.github.com>

* Popup doesn't really need to refetch cookies

---------

Co-authored-by: WorldLanguages <WorldLanguages@users.noreply.github.com>

* Set value of `scratchAddons.cookieStoreId`

* Format code

* [v1.38.x] Send less requests to `scratch.mit.edu/session` etc. (#7666)

* Initial commit

* Format code

* Fix TODO: handle chrome.storage.session undefined

* Remove invalid TODO

* Remove console.log

* Background: only use cache on first time

* Revert canUseCachedSession

* exact-count addon changes (profiles)

* Change animated-thumb later

* Only call refreshFn once

* Use underscore for _requestFetchFn attr

* Remove outdated comment

* Do not reference isScratchGui variable

* Add code comment

* Fix execution order

---------

Co-authored-by: WorldLanguages <WorldLanguages@users.noreply.github.com>

---------

Co-authored-by: WorldLanguages <WorldLanguages@users.noreply.github.com>
@WorldLanguages

Copy link
Copy Markdown
Member Author

Regression: #7744

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority: 3 Medium priority. Includes bugs and useful features scope: addon api Related to the addon.* JS APIs or other ways for addons to provide features scope: addon Related to one or multiple addons scope: core Related to the core script/extension workings type: enhancement New feature for the project

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants