feat(Utility): improve addLink method#8118
Conversation
Reviewer's guide (collapsed on small PRs)Reviewer's GuideImproves the addLink helper to use string values for its loaded state, handle stylesheet load errors explicitly, and simplify the returned Promise by resolving on either success or failure without rejecting. Sequence diagram for improved addLink stylesheet loadingsequenceDiagram
participant Caller
participant addLink
participant DocumentHead as document_head
participant CssLink as css_link
participant Console as console
Caller->>addLink: addLink(href, rel)
addLink->>DocumentHead: appendChild(css_link)
addLink->>CssLink: setAttribute(loaded, 'true') onload
addLink->>CssLink: setAttribute(loaded, 'error') onerror
addLink->>addLink: setInterval(handler, 20)
addLink->>CssLink: getAttribute(loaded)
alt state === 'true'
addLink->>addLink: clearInterval(handler)
addLink-->>Caller: Promise resolved
else state === 'error'
addLink->>addLink: clearInterval(handler)
addLink->>Console: console.error(href)
addLink-->>Caller: Promise resolved
end
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- Consider rejecting the promise on
onerrorinstead of resolving, so callers can distinguish between success and failure rather than relying on a console message. - If
addLinkis called multiple times for the samehrefwhere the link already exists but has noloadedattribute (e.g., added outside this helper), the polling loop will never resolve; you may want to handle pre-existing links more defensively (e.g., resolve immediately or infer loaded state fromsheet/readyState).
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider rejecting the promise on `onerror` instead of resolving, so callers can distinguish between success and failure rather than relying on a console message.
- If `addLink` is called multiple times for the same `href` where the link already exists but has no `loaded` attribute (e.g., added outside this helper), the polling loop will never resolve; you may want to handle pre-existing links more defensively (e.g., resolve immediately or infer loaded state from `sheet`/`readyState`).Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Pull request overview
This PR improves the addLink helper in wwwroot/modules/utility.js to better track the load state of dynamically added stylesheets, aligning its behavior more closely with the existing addScript helper and preventing callers from hanging on load errors.
Changes:
- Set the
loadedattribute using explicit string values ('true'/'error') instead of a boolean. - Add
onerrorhandling for dynamically added<link>elements to mark failure state. - Simplify the promise to always resolve, and log an error when stylesheet loading fails.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #8118 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 766 766
Lines 34204 34204
Branches 4695 4695
=========================================
Hits 34204 34204
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Link issues
fixes #8117
Summary By Copilot
Regression?
Risk
Verification
Packaging changes reviewed?
☑️ Self Check before Merge
Summary by Sourcery
Improve the utility helper for dynamically adding stylesheet links to handle load state more robustly.
Bug Fixes:
Enhancements: