Skip to content

feat: wait load js for lms#1

Merged
johanseto merged 3 commits intoopen-release/redwood.nelpfrom
jlc/wait-load-js
Jul 14, 2025
Merged

feat: wait load js for lms#1
johanseto merged 3 commits intoopen-release/redwood.nelpfrom
jlc/wait-load-js

Conversation

@johanseto
Copy link
Collaborator

@johanseto johanseto commented Jul 5, 2025

What's this PR do?

Fix cloudfront compatibility with missing js files loads.

This ensure to load the extra js files before xblock is called.

Before

2025-07-04_10-58

Stage:

2025-07-04_20-44

After

image

Stage:

image

Jira story

https://edunext.atlassian.net/browse/FUTUREX-1215

@johanseto johanseto requested a review from andrey-canon July 7, 2025 15:25
@johanseto
Copy link
Collaborator Author

@shadinaif @OmarIthawi Could you also check this PR to help fix SGA.

Issues -related

mitodl#336

PRs related

mitodl#319
mitodl#318

Copy link
Collaborator

@andrey-canon andrey-canon left a comment

Choose a reason for hiding this comment

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

I couldn't tests the CDN issue locally however the standard functionality worked as expected and the code LGTM

@shadinaif
Copy link

shadinaif commented Jul 8, 2025

Thank you @johanseto.

I believe this resolves the issue. I guess the root cause is the fact that cloudfront is slow in many regions! all requests are almost guaranteed to finish before the cloudfront request. The only problem that I expect is that it'll make the page stuck in loading state for up to 10 seconds (sometimes). It's a problem with cloudfront itself, the file takes a lot of time to load unless it's cached by the browser

Copy link

@OmarIthawi OmarIthawi left a comment

Choose a reason for hiding this comment

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

Thanks a lot @johanseto.

I've suggested few refactorings because the code might work well in most cases but fail unexpectedly due to race conditions.

Making the code more solid and more handled is good and should probably be contributed upstream.

That being said, if cloudfront is an issue in the middleeast and your region, let's use a better CDN or no CDN at all that stays an issue. @shadinaif do you have any source about the issues you've mentioned regarding cloudflare or that's an experience you've faced?

function loadjs(url) {
$('<script>')
.attr('type', 'text/javascript')
.attr('src', window.baseUrl + url)

Choose a reason for hiding this comment

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

If async resolves the issue, then let's keep the change to its minimum amount.

      .attr('async', true)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That doesn't resolve the problem; we need to wait until those scripts are loaded. So we need to use the onload, I tried with jquery approach, but it doesn't work

@@ -1,5 +1,5 @@
/* Javascript for StaffGradedAssignmentXBlock. */
function StaffGradedAssignmentXBlock(runtime, element) {
async function StaffGradedAssignmentXBlock(runtime, element) {

Choose a reason for hiding this comment

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

Suggested change
async function StaffGradedAssignmentXBlock(runtime, element) {
function StaffGradedAssignmentXBlock(runtime, element) {

We have two asyncs here, this async function and the scripts are async.

Are we sure we need both?

The LMS XBlock runtime (which calls this funtion) may not support promises and this might crash to unhandled promise error.

If async is a must, we must encapsulate the async logic inside StaffGradedAssignmentXBlock to ensure the promise is handled properly, instead of making the StaffGradedAssignmentXBlock itself async.

Copy link
Collaborator Author

@johanseto johanseto Jul 9, 2025

Choose a reason for hiding this comment

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

Knowing your concerns now, I prefer to apply this change to remove the general async declaration. b776991

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@johanseto
Copy link
Collaborator Author

Thank you @johanseto.

I believe this resolves the issue. I guess the root cause is the fact that cloudfront is slow in many regions! all requests are almost guaranteed to finish before the cloudfront request. The only problem that I expect is that it'll make the page stuck in loading state for up to 10 seconds (sometimes). It's a problem with cloudfront itself, the file takes a lot of time to load unless it's cached by the browser

Even cached in the browser the error persists, when you reload test_url. Also CDN idea is to load fast with Cloudfront without thinking about the region. If you check here, the timing is good, I mean 70ms.

2025-07-08_10-50

Anyway, the problem exists maybe because the script URL is using and URL different than the window URL. (
https://dg4ogka6-test.cloudfront.net/static/js/vendor/jQuery-File-Upload/js/jquery.fileupload.js) and the js block code continues calling xblock without loading the libraries...

@johanseto johanseto changed the base branch from open-release/redwood.nelp to jlc/fix-tests July 9, 2025 20:45
@johanseto johanseto force-pushed the jlc/fix-tests branch 4 times, most recently from b316822 to 65d54f6 Compare July 9, 2025 21:37
@johanseto johanseto requested a review from OmarIthawi July 9, 2025 22:19
With pr suggestion the async behaviour is only used to load the promises of loading scripts.
Then xblock is called if all js files load as expected.
Also StaffGradedAssignmentXBlock is preserved not async, so now we wait for loadJs promises.
@johanseto johanseto changed the base branch from jlc/fix-tests to open-release/redwood.nelp July 11, 2025 15:29
Copy link

@OmarIthawi OmarIthawi left a comment

Choose a reason for hiding this comment

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

Thanks @johanseto, no objections from my end. Thanks!!

@johanseto johanseto merged commit 38897b3 into open-release/redwood.nelp Jul 14, 2025
6 checks passed
@johanseto johanseto temporarily deployed to open-release/redwood.nelp July 14, 2025 14:06 — with GitHub Actions Inactive
@johanseto johanseto deployed to open-release/redwood.nelp July 14, 2025 14:06 — with GitHub Actions Active
johanseto added a commit that referenced this pull request Jul 15, 2025
* feat: wait load js for lms

* chore: append script to the element

* refactor: move async behaviour for main function

With pr suggestion the async behaviour is only used to load the promises of loading scripts.
Then xblock is called if all js files load as expected.
Also StaffGradedAssignmentXBlock is preserved not async, so now we wait for loadJs promises.

(cherry picked from commit 38897b3)
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.

4 participants