-
-
Notifications
You must be signed in to change notification settings - Fork 6k
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
perf: reduce query selector #9437
Conversation
Related, though I didn't want to add it to this PR, why is this being awaited in the first place? I didn't try this, but it should be just: - return Promise.all(
- deps.map((dep) => {
- // @ts-ignore
- dep = assetsURL(dep, importerUrl)
- // @ts-ignore
- if (dep in seen) return
- // @ts-ignore
- seen[dep] = true
- const isCss = dep.endsWith('.css')
- // @ts-ignore check if the file is already preloaded by SSR markup
- if (documentLinkUrls.includes(dep)) {
- return
- }
- // @ts-ignore
- const link = document.createElement('link')
- // @ts-ignore
- link.rel = isCss ? 'stylesheet' : scriptRel
- if (!isCss) {
- link.as = 'script'
- link.crossOrigin = ''
- }
- link.href = dep
- // @ts-ignore
- document.head.appendChild(link)
- if (isCss) {
- return new Promise((res, rej) => {
- link.addEventListener('load', res)
- link.addEventListener('error', () =>
- rej(new Error(`Unable to preload CSS for ${dep}`))
- )
- })
- }
- })
- ).then(() => baseModule())
+ for (const dep of deps) {
+ // @ts-ignore
+ dep = assetsURL(dep, importerUrl)
+ // @ts-ignore
+ if (dep in seen) return
+ // @ts-ignore
+ seen[dep] = true
+ const isCss = dep.endsWith('.css')
+ // @ts-ignore check if the file is already preloaded by SSR markup
+ if (documentLinkUrls.includes(dep)) {
+ return
+ }
+ // @ts-ignore
+ const link = document.createElement('link')
+ // @ts-ignore
+ link.rel = isCss ? 'stylesheet' : scriptRel
+ if (!isCss) {
+ link.as = 'script'
+ link.crossOrigin = ''
+ }
+ link.href = dep
+ // @ts-ignore
+ document.head.appendChild(link)
+ }
+
+ return baseModule(); Waiting for scripts to be actually loaded is not needed for any logic that follows, as far as I can tell. |
Please re-run tests. It looks like it is a CI/CD failure. |
@1yyx990803 @patak-dev can someone re-run tests? |
@gajus A couple notes about your proposed code below:
|
None of the code that you reference is added by this PR. |
Yeah, I'm aware 😄 Otherwise, I would've noted it in a PR review. I was merely commenting. |
const cssSelector = isCss ? '[rel="stylesheet"]' : '' | ||
// @ts-ignore check if the file is already preloaded by SSR markup | ||
if (document.querySelector(`link[href="${dep}"]${cssSelector}`)) { | ||
if (documentLinkUrls.includes(dep)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This now skips checking [rel="stylesheet"]
if isCss
is true, which would regress #1734. We should probably check the href
and rel="stylsheet"
too for css.
It's likely to prevent FOUC if the preload links aren't properly loaded yet, and also to prevent runtime race conditions between preload deps and base module. |
Closing as this PR brings in breaking changes. |
Description
Reduces use of unnecessary querySelector use.
Additional context
It is faster to read DOM once.
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).