UI: Flickering when loading themes on pages with long loading times #94706

Closed
opened 2024-09-27 12:15:34 +02:00 by Márton Lente · 1 comment

There is a flicker in loading the theme that is quite noticeable on pages heavy on images, like Speakers, Attendees, or Schedule.

This is not just a problem of our implementation but something many run into when introducing themes.

Some propose using a bit of inline Javascript (vanilla) in the <head>, before anything, which is probably the best. Outlined in this StackExchange answer or this GitHub comment.

Originally posted by @pablovazquez in infrastructure/conference-website#103972 (comment)

There is a flicker in loading the theme that is quite noticeable on pages heavy on images, like Speakers, Attendees, or Schedule. <video src="/attachments/cd3c3ac2-d7e9-4ee0-b8df-d16324b99fcd" title="slow_switch.mov" controls></video> This is not just a problem of our implementation but something many run into when introducing themes. Some propose using a bit of inline Javascript (vanilla) in the `<head>`, before anything, which is probably the best. Outlined in [this StackExchange answer](https://stackoverflow.com/a/62635541/8444393) or [this GitHub comment](https://github.com/vercel/next.js/discussions/12533#discussioncomment-6696252). _Originally posted by @pablovazquez in https://projects.blender.org/infrastructure/conference-website/pulls/103972#issuecomment-1303571_
Author
Owner

To prevent fickering, a theme initialization JavaScript snippet (and its minified version) has been created and added to development utilities, that can be included in project templates' <head> as inline JS. The detailed usage instructions have been added to Web Assets wiki page section on Enabling themes as well.

The snippet initializes themes before the DOM has been loaded, and works like this in a nutshell:

  • It checks the dataTheme item in the localStorage.
  • If dataTheme is set, it assigns the according data-theme attribute to the <html> tag.
  • As the correct data-theme attribute is already present while loading the styles, the page colours are correctly rendered from start.
  • The snippet runs as an IIFE, and is self-contained. It doesn't conflict with the base theme system's JavaScript, that is compiled as part of the project's JS build pipeline.
  • Using the snippet is optional, and doesn't change how the base theme system is enabled.

The solution has been tested locally with throttling simulating slow networks, and seems to work reliably.

@pablovazquez

To prevent fickering, a theme initialization JavaScript [snippet](https://projects.blender.org/infrastructure/web-assets/src/branch/v2/dev-utils/init-theme.js) (and its [minified version](https://projects.blender.org/infrastructure/web-assets/src/branch/v2/dev-utils/init-theme.min.js)) has been created and added to development utilities, that can be included in project templates' `<head>` as inline JS. The detailed usage instructions have been added to Web Assets [wiki](https://projects.blender.org/infrastructure/web-assets/wiki/v2-guidelines-and-notes.md#enable-themes) page section on _Enabling themes_ as well. The snippet initializes themes before the DOM has been loaded, and works like this in a nutshell: - It checks the `dataTheme` item in the _localStorage_. - If `dataTheme` is set, it assigns the according `data-theme` attribute to the `<html>` tag. - As the correct `data-theme` attribute is already present while loading the styles, the page colours are correctly rendered from start. - The snippet runs as an IIFE, and is self-contained. It doesn't conflict with the base theme system's JavaScript, that is compiled as part of the project's JS build pipeline. - Using the snippet is optional, and doesn't change how the base theme system is enabled. The solution has been tested locally with throttling simulating slow networks, and seems to work reliably. @pablovazquez ⌃
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: infrastructure/web-assets#94706
No description provided.