Needs review
Project:
High contrast
Version:
8.x-1.x-dev
Component:
Code
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Reporter:
Created:
17 Feb 2017 at 08:27 UTC
Updated:
10 Mar 2021 at 07:06 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
neograph734Comment #3
jesse_k commentedI'm currently struggling with this. The module does not appear to work behind Varnish.
Does anyone have further insight into this?
Comment #4
neograph734I was about to close this issue last week, as it had not gotten a response for over a year. But apparently there is an issue :)
I have 0 experience with Varnish, but during the development of this module I learned some of Drupal's caching system. As you might already know Drupal uses cache tags and cache contexts. The first is used to invalidate content once it is updated. The second is used to store a variation of a page.
High contrast for Drupal 8 uses cache contexts to distinguish between regular and high contrast pages. I was assuming (though now I am searching for it, I can't find it) that Varnish would cache both tags and contexts. But all documentation is about cache tags only. Can you somewhere configure Varnish to also use the
X-Drupal-Cache-Contextheader for testing or something?Comment #5
neograph734Additionally, could you try disabling the 'internal page cache' module and see if that works?
Comment #6
hkirsman commentedThis could also be solved purely with js by dynamically injecting the CSS tag to page. That way we would not mess with the cache at all - less code and works with any caching system. It's done like that in Drupal 7. Injecting CSS tag could be one option but other would be to always load the css (it's not that big) and then js toggles body class like like high-contrast-enabled etc. Always loading the CSS should give us url with cache buster parameter.
Comment #7
neograph734There have been two things withholding me from doing it with JavaScript.
- The first is that I experienced a 'flash' with the Drupal 7 version of the module. Where the base theme would render with a white background and as soon as the DOM is ready JavaScript starts working and then makes the background black.
- The second is that it is not only the CSS, but also the site logo. And because there is no single place for the logo anymore (there is the system branding block, but you do not have to use that and other modules might provide their own branding blocks or it is hardcoded in the theme). For this reason overriding it in the configuration made the most sense, but then you'd have to cache 2 'versions' of the same page.
Luckly, Drupal also has a solution for that Cache Contexts. So far so good. The only thing I have never taken into account is that such cache contexts do not play nice together with the Internal Page Cache for caching anonymous pages...
So one point for JavaScript there as that is compatible with the Internal Page Cache module.
Comment #8
hkirsman commentedIt's possible to overcome those flashes if you apply some tricks with js - don't use any DOM ready detection nor Drupals behaviours (which I think is using jQuery's ready() function). Instead just start the js but add the js script after whatever is needed to be altered. Inlining it would make it potentially the fastest but browsers cache js files so it's also instant.
I'll see if I can make a minimal working example
Comment #9
hkirsman commentedIt's a bit differently executed than in D7 because the below idea seemed more simpler thus more elegant in my eyes :) . In D7 there's LINK tag created and the js is a bit bloated - it contains copy pasted code of "JavaScript Cookie v2.0.4", "HTML string to DOM.", "DOM ready libraries.". In D8 we natively ask for cookie and set HTML class to high-contrast--enabled (basically 3 lines of code). CSS is always loaded but everything is prefixed with high-contrast--enabled class. I think having high contrast being controlled by css class could be benefited in theming because anybody can add extra css to their theme.
Didn't remove any existing code.
Didn't work with logos but I think one way of doing this is to always load the high contrast logo but hide if there's no high-contrast--enabled. Not sure if possible but I want to believe that is just technicality to overcome.
I separated js to 3 scripts:
Comment #10
hkirsman commentedAdded support for logos by getting the default and high contrast logo in backend, set them in js and then replace in front-end by the src parameter. Can you detect any flickreing? Still it's just MVP.
Comment #11
neograph734Sorry, I've had no time to look yet. But considering the fact that page cache for anonymous users does not support cache contexts I believe this is the only way. It also allows to do some 'auto-detection' using
-ms-high-contrastmedia feature and perhaps controlling the toggle with-ms-high-contrast-adjustproperty.https://docs.microsoft.com/en-us/previous-versions/hh771830(v=vs.85)
https://docs.microsoft.com/en-us/previous-versions/hh771863(v=vs.85)
I will have limited access to internet next week, but feel free to continue on this. The method I picked 3 years ago does not work.
Comment #12
hkirsman commentedSo you think the high contrast mode could be enabled by detecting if Windows has enabled it natively? I don't have Windows, but shouldn't browsers already have native high contrast then? Is it good idea to override it with our stylesheet and is it even possible? I guess if we'd wrap it in the above media query, it could work. In case Windows high contrast mode does not change websites, then this could be separate task for Windows users.
Hope you can test the above MVP patch. I think it's quite good although I didn't yet test in MacOS or Windows. Also it still has all the backend code which at least partly can be remoeved.
Comment #13
hkirsman commentedThis MVP didn't work with BigPipe and for anon users with standard profile. ForBigPipe needed to use behaviours and for anon users, core/drupal dependency was missing. And last but not least, one needs to save the config form after applying patch to update stylesheets - otherwise it's always high contrast enabled (hm, I guess we would need update hook for this).
Also tested with MacOS Chrome, Safari and Firefox - no clitches.
Windows to go
Comment #14
hkirsman commentedAdding update hook
Comment #15
neograph734I hope to get a review this week. Simplytest failed when I wanted to have a quick look at it, so I will spawn a clean development site.
Comment #16
hkirsman commentedYou could try Lando local environment I've set up ;)
More info here https://github.com/hkirsman/lando_drupal_module_high_contrast
PS: I haven't really started with tests for this. Would also need to get rid of some PHP code if this new javascript version looks promising.
Comment #17
neograph734Oke, lando works pretty nice. I've read about it months ago, but felt I did not need it as I was already using docker. It was eh.. quite a surpise to see my site spawn. Sorry it took so long..
Anyway I agree that locally there was no flickering. So this could be a nice way forward.
Comment #18
hkirsman commentedHad some time on my hands and advanced further with this:
Now need to remove all redundant back-end code eg enableHighContrast(), disableHighContrast() etc
Comment #19
hkirsman commentedShould be done
Comment #20
lordzik commentedHello,
i came to this as high contrast module literaly does nothing on my site. If i click a switcher or just open /high-contrast/enable?destination=/node page gets refreshed but nothing is changing (no contrast-mode on).
I tried to apply patch #19 but that didn't help. Now when i click switcher or open /high-contrast/enable?destination=/node i get Drupal's "page not found" error.
Does this module actualy work in Drupal 8.9.13?
btw - i'm using modified bootstrap theme.
[edit]
Nevermind. I found the reason why high_contrast module was "not working". For some reason our site was missing /sites/site-name/files/css/high_contrast.css file. The problem is when one goes to admin/config/user-interface/high-contrast and save config - this missing high_contrast.css file is not created unless any settings on this page is modified. When i changed definition of one of colors - file has been [re]created.
Comment #21
hkirsman commentedI was just to about to check it. Good it was resolved for you. Still it might be bug that the css file was not generated.