-
-
Notifications
You must be signed in to change notification settings - Fork 35.9k
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
Persist night mode on page load #13229
Persist night mode on page load #13229
Conversation
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.
@Bouncey Thanks! 😊
This works fine as long as you don't do a hard refresh, so I'd say this is ready!
Hmm I tested it with hard refreshes, clearing the cache shouldn't clear
localStorage
…On Tuesday, February 7, 2017, Samuel Plumppu ***@***.***> wrote:
***@***.**** approved this pull request.
@Bouncey <https://github.com/Bouncey> Thanks! 😊
This works fine as long as you don't do a hard refresh, so I'd say this is
ready!
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#13229 (review)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ARtk5iGf7UXZ4JAKKO3FUgRzDylvUiIGks5raOe7gaJpZM4L6EHO>
.
|
@Bouncey It might be caused by the re-downloading bundle? Or maybe the bundle is starting (although this flash doesn't occur on normal pageloads)? |
This post on SO confirmed what I thought. Could it just be an edge case? |
@Bouncey As it only occurs when I do hard refreshes, I don't think it will be a problem. I'm pretty sure this is the expected behavior as clearing the cache will clear the react bundle - which then needs to be re-downloaded before it can detect nightmode again, despite what's in localStorage or not |
Yes, but |
@Bouncey I know about Then this might be an edge case, or just some lag before the script gets executed (There might be inlined scripts before the one for nightmode?). |
client/sagas/night-mode-saga.js
Outdated
@@ -29,6 +29,9 @@ export default function nightModeSaga( | |||
.flatMap(() => { | |||
const { app: { theme } } = getState(); | |||
const newTheme = !theme || theme === 'default' ? 'night' : 'default'; | |||
if (typeof Storage !== 'undefined') { |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
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.
@BerkeleyTrue Nice catch! Somehow, it worked locally despite using Storage
.
This still flashes slightly on pageload, but it might just be how the rendering works.
I think this solves the issue though 😊
client/sagas/night-mode-saga.js
Outdated
@@ -29,6 +29,9 @@ export default function nightModeSaga( | |||
.flatMap(() => { | |||
const { app: { theme } } = getState(); | |||
const newTheme = !theme || theme === 'default' ? 'night' : 'default'; | |||
if (typeof localStorage !== 'undefined') { | |||
localStorage.setItem('fccTheme', newTheme); | |||
} |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
server/views/layout-react.jade
Outdated
@@ -11,5 +11,12 @@ html(lang='en') | |||
script!= state | |||
script. | |||
window.webpackManifest = !{JSON.stringify(chunkManifest || {})}; | |||
script. | |||
if (typeof localStorage !== 'undefined') { | |||
const fccTheme = JSON.parse(localStorage.getItem('fccTheme')); |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
server/views/layout-react.jade
Outdated
@@ -11,5 +11,12 @@ html(lang='en') | |||
script!= state | |||
script. | |||
window.webpackManifest = !{JSON.stringify(chunkManifest || {})}; | |||
script. | |||
if (typeof localStorage !== 'undefined') { | |||
const fccTheme = JSON.parse(localStorage.getItem('fccTheme')); |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
@Bouncey updated the pull request. |
server/views/layout-react.jade
Outdated
@@ -11,5 +11,18 @@ html(lang='en') | |||
script!= state | |||
script. | |||
window.webpackManifest = !{JSON.stringify(chunkManifest || {})}; | |||
script. |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
server/views/layout-react.jade
Outdated
if (fccTheme && fccTheme === 'night') { | ||
document.body.classList.add('night'); | ||
} | ||
} |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
client/sagas/night-mode-saga.js
Outdated
@@ -7,6 +9,10 @@ import { | |||
createErrorObservable | |||
} from '../../common/app/redux/actions'; | |||
|
|||
function persistTheme(theme) { | |||
store.set('fccTheme', theme); |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
@Bouncey updated the pull request. |
@Bouncey Great work! Thanks for you patience Happy Coding |
Pre-Submission Checklist
staging
branch of freeCodeCamp.fix/
,feature/
, ortranslate/
(e.g.fix/signin-issue
)npm test
. Usegit commit --amend
to amend any fixes.Type of Change
Checklist:
Description
Adds a key to
localStorage
which holds the theme preference of the user. This enables night mode to be enabled on page load whilst the bundle evals.