Overview
This article provides an example of implementing theme switching and server dates using Epic Stack - Client Hints and Jacob Paris’ article as a reference. The example demonstrates how the server retrieves theme and time-zone hints on the first visit, stores them in cookies, and reloads the page for client-side usage. The approach aims to improve user experience by avoiding the flash of unstyled content on every page load.
Features
- Theme Switching: Implements theme switching based on the prefers-color-scheme media feature. It updates the theme cookie through a POST request to the /resources/theme route and either reloads the page or returns a JSON response.
- Time-Zone: Allows the server to understand the client’s time zone on initial page load. The server-side implementation uses the getHints(request).timeZone method to retrieve time zone information. For client-side usage, the useHints().timeZone method can be employed.
- UI Display: The App Demo displays the time zone in the user interface using the Intl.DateTimeFormat object created in the user’s time zone and preferred language.
Installation
To run this example locally, follow these steps:
- Clone the repository.
- Install the dependencies.
- Run the server.
Summary
This article provides a practical example of implementing theme switching and server dates using Epic Stack - Client Hints. It explains how the server retrieves theme and time-zone hints, stores them in cookies, and reloads the page for client-side usage. The article also provides resources for further understanding of Client Hints and the implementation discussed. Contributions are welcome, and the example is licensed under the MIT license.