-
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor main js bundle load sequence
Signed-off-by: weru <fromweru@gmail.com>
- Loading branch information
Showing
4 changed files
with
156 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
function prefersColor(mode){ | ||
return `(prefers-color-scheme: ${mode})`; | ||
} | ||
|
||
function systemMode() { | ||
if (window.matchMedia) { | ||
const prefers = prefersColor(dark); | ||
return window.matchMedia(prefers).matches ? dark : light; | ||
} | ||
return light; | ||
} | ||
|
||
function currentMode() { | ||
let acceptable_chars = light + dark; | ||
acceptable_chars = [...acceptable_chars]; | ||
let mode = getComputedStyle(doc).getPropertyValue(key).replace(/\"/g, '').trim(); | ||
|
||
mode = [...mode].filter(function(letter){ | ||
return acceptable_chars.includes(letter); | ||
}); | ||
|
||
return mode.join(''); | ||
} | ||
|
||
function changeMode(is_dark_mode) { | ||
if(is_dark_mode) { | ||
bank.setItem(storageKey, light) | ||
elemAttribute(doc, mode_data, light); | ||
} else { | ||
bank.setItem(storageKey, dark); | ||
elemAttribute(doc, mode_data, dark); | ||
} | ||
} | ||
|
||
|
||
function pickModePicture(mode) { | ||
elems('picture').forEach(function(picture){ | ||
let source = picture.firstElementChild; | ||
const picture_data = picture.dataset; | ||
const images = [picture_data.lit, picture_data.dark]; | ||
source.src = mode == 'dark' ? images[1] : images[0]; | ||
}); | ||
} | ||
|
||
function setUserColorMode(mode = false) { | ||
const is_dark_mode = currentMode() == dark; | ||
const stored_mode = bank.getItem(storageKey); | ||
const sys_mode = systemMode(); | ||
if(stored_mode) { | ||
mode ? changeMode(is_dark_mode) : elemAttribute(doc, mode_data, stored_mode); | ||
} else { | ||
mode === true ? changeMode(is_dark_mode) : changeMode(sys_mode!==dark); | ||
} | ||
const user_mode = doc.dataset.mode; | ||
doc.dataset.systemmode = sys_mode; | ||
user_mode ? pickModePicture(user_mode) : false; | ||
} | ||
|
||
setUserColorMode(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8496fdf
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.
It seems that this way,
mode.js
is still run at the very end of the document, albeit earlier within the bundle. I don't think this will properly prevent actual flickering on all devices. I'm afraid the only way would be to run$variables
and$mode
(don't know if$functions
or$code
are needed for this) from the<head>
of the page.8496fdf
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.
@ltguillaume, I'm trying not to load any javascript at all in the head. This change needs one additional css change to work. I'll add it soon.
8496fdf
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.
There's a good chance it might be inadequate, in which case I will have no option but to follow your lead.
8496fdf
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.
Is there any specific reason (I am evidently unaware of 😅) as to why this is best practice?
8496fdf
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.
@ltguillaume, scripts can be render blocking. Best if they're in the footer. I inlined the main script in the footer, 3230e2b. Not sure if it made the right dent. CSS changes not in yet, so not to worry.
8496fdf
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.
Sorry, I can still see a clear white flash before every page load when in dark mode on Chromium-based browsers. Somehow Firefox-based browsers don't show a flash anymore.