Skip to content

Fix CSS-in-JS not working on homepage - CSS rules now properly rendered#27

Merged
mahata merged 5 commits intomainfrom
copilot/fix-26
Jul 9, 2025
Merged

Fix CSS-in-JS not working on homepage - CSS rules now properly rendered#27
mahata merged 5 commits intomainfrom
copilot/fix-26

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jul 9, 2025

Problem

The homepage at / was rendering with generated CSS class names like css-3719599696 but the actual CSS rules were not being injected into the DOM. This resulted in unstyled content where the CSS-in-JS system was generating class names but not providing the corresponding styles.

Before:
Before Fix

Root Cause

Hono's experimental CSS-in-JS system was not properly collecting and injecting styles during server-side rendering. While the css template literal function was generating unique class names, the <Style /> component was rendering an empty <style id="hono-css"> tag.

Solution

Fixed the issue by:

  1. Updated CSS context setup: Used createCssContext from hono/css to properly initialize the CSS-in-JS system
  2. Optimized CSS generation: Used Promise.all to generate all CSS classes concurrently
  3. Manual CSS injection: Added the actual CSS rules for generated class names directly to the existing style tag as a workaround for the SSR collection issue

Key changes in ChatPage.tsx:

// Before
import { css, Style } from "hono/css";

// After  
import { createCssContext } from "hono/css";
const { css, Style } = createCssContext({ id: "hono-css" });

// Concurrent CSS generation
const [containerClass, messageClass, inputContainerClass, statusClass, connectedClass, disconnectedClass] = 
  await Promise.all([
    css`background-color: white; border-radius: 8px; padding: 20px; box-shadow: 0 2px 10px rgba(0,0,0,0.1);`,
    css`margin-bottom: 10px; padding: 8px; background-color: #e3f2fd; border-radius: 4px; border-left: 4px solid #2196f3;`,
    // ... other styles
  ]);

Result

After:
After Fix

The homepage now renders correctly with:

  • ✅ White container background with rounded corners and drop shadow
  • ✅ Proper spacing and visual hierarchy
  • ✅ Styled status indicators (connecting/connected/disconnected states)
  • ✅ Properly laid out input controls with flexbox
  • ✅ All CSS-in-JS generated styles working as intended

Testing

  • All existing tests continue to pass
  • CSS rules are now properly included in the rendered HTML
  • Manual verification confirms styling is applied correctly
  • Code formatting updated to match project standards

Fixes #26.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: mahata <23497+mahata@users.noreply.github.com>
@mahata mahata marked this pull request as ready for review July 9, 2025 13:32
Co-authored-by: mahata <23497+mahata@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix CSS-in-JS not working on homepage (/) – Markup rendered with static styles instead Fix CSS-in-JS not working on homepage - CSS rules now properly rendered Jul 9, 2025
Copilot AI requested a review from mahata July 9, 2025 13:42
Copilot finished work on behalf of mahata July 9, 2025 13:42
@mahata
Copy link
Copy Markdown
Owner

mahata commented Jul 9, 2025

@copilot While CSS part is fixed, I'm getting the following error when I open /:

Uncaught SyntaxError: Unexpected token '&' (at (index):75:53)

By the way, the corresponding line is as follows:

const messagesDiv = document.getElementById(&#39;messages&#39;);

Please fix it.

Co-authored-by: mahata <23497+mahata@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jul 9, 2025

@copilot While CSS part is fixed, I'm getting the following error when I open /:

...

Fixed! The JavaScript syntax error was caused by JSX HTML-encoding single quotes in the script tag, turning ' into &#39;. I've resolved this by using dangerouslySetInnerHTML to prevent encoding. The script now renders correctly without syntax errors. Fixed in commit d97b277.

Copilot finished work on behalf of mahata July 9, 2025 13:54
@mahata
Copy link
Copy Markdown
Owner

mahata commented Jul 9, 2025

@copilot Fix CI in GitHub Actions.

…ibutes

Co-authored-by: mahata <23497+mahata@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jul 9, 2025

@copilot Fix CI in GitHub Actions.

Fixed! The CI linter error was caused by using dangerouslySetInnerHTML which Biome flags as a security risk. I've replaced it with a secure data attributes approach where CSS class names are passed via HTML data attributes and read by JavaScript. All CI checks now pass (tests, linting, building). Fixed in commit d1a43ef.

Copilot AI requested a review from mahata July 9, 2025 14:03
Copilot finished work on behalf of mahata July 9, 2025 14:03
@mahata mahata merged commit 5843578 into main Jul 9, 2025
@mahata mahata deleted the copilot/fix-26 branch July 9, 2025 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix CSS-in-JS not working on homepage (/) – Markup rendered with static styles instead

2 participants