Can't apply global style based on the user preference
On Reddit, we have a user preference called Night Mode that uses CSS to make the site darker. We would like to be able to change the css of our amp pages based on this preference. We tried the approach of using amp-access to retrieve the user's preference by getting this data from the authorization json response.
Here you can find the approach that we used: https://jsbin.com/yuwaduvili/edit?html,output
We saw several problems with this approach:
- We would need to wrap the whole page inside a template, meaning that you could not have other templates within the page
- This would make the page slower because everything is wrapped inside the template
- There could potentially be a problem where the page flashes while waiting for the template to update
Is there a better approach? If we could access a cookie that would store this info that would be the easiest approach for us.
@rudygalfi @ericlindley-g
cc @dvoytenko @cramforce for thoughts
/cc @jridgewell for opinion
This problem, I believe, is more similar to amp-dynamic-css-classes.
I can certainly see where we could enlist dynamic HTTPS endpoints, viewer override, local/session storage, etc to apply a thematic CSS class. We do it today for referrer.
Certainly, though it'll cause a noticeable delay in rendering. 😞
But no more than dynamic-css-classes already regulates? And for outliers we'll use local/session storage so that it'd be available for the next doc view?
Sorry, I mean the initial ajax request. Doing styling based on referrer is not terrible, since we can get it sync at startup. If we want to do next page-view styling, using storage caching, that should work well. But after initial page visibility, it'd be pretty jarring for a full restyle to happen.
Could it be done in a way where it only works if the value has already been written to local storage and have a service that writes to it asynchronously? It would basically allow customization of the N > 0th page view.
Yup, that's what I mean. I imagine we want to redo the request on every page view (so we get new styles if necessary), with the new styles applying on the next view.
It feels somewhat special case-y, but otherwise fits well into AMP's stale-while-revalidate philosophy.
This sounds promising — any volunteers to write up a doc and take it through design review?
I could put together an ITI. I'll try to present it today since today's schedule is lighter.
@sffriend I filed #10647 and scheduled for today's Design Review (#10266). Feel free to join the review today. If you'd like to join, but can't do so today - let me know and I'll reschedule for the next week.
@dvoytenko thanks for the work on this! I can't make it this week but feel free to present regardless.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions.