Giter Club home page Giter Club logo

Comments (5)

sabercoy avatar sabercoy commented on July 30, 2024 2

Would you like to put some details about the issue and workaround for people coming here later.

The desired behavior is to use CSS Modules and have no visible style changes once the page loads (this is caused by CSS files being loaded in and parsed after the browser has already painted the screen)

The issue begins with how vite behaves differently between its dev server and its build output
In dev, your styles are injected into the initial document, you are not given separate CSS files. This means that you will not see style changes, for all of your styles are there in the beginning globally.

When building, vite takes your styles and separates them into CSS files that get loaded at different times depending on when they are needed. In the case of SolidStart and vinxi, these CSS files are loaded in as part of "modulepreload" functionality. This is intended to have your assets loaded in the background upon an action (such as hovering an anchor tag) so that the page transition happens faster.

The modulepreload functionality is nice during page transition, but awkward during initial load of the page. These CSS files for the page you are loading are fetched by JavaScript, not by link tags in your document's head, so the browser does not wait for them before painting the screen, so when they arrive its too late and you see style changes.

This is not an issue when cssCodeSplit is set to false (it makes just 1 CSS file with ALL styles thats loaded with the initial document) but there are bugs I ran into with this vitejs/vite#1141 (comment)

It is also not an issue with Tailwind because this also just puts all utility classes into 1 CSS file loaded with the initial document.

So what is the workaround for CSS Modules? On initial page load, I check which route path I am entering and inline the CSS Module styles into the document:

import styles_home from '#/routes/pages/home/(home).module.css?inline'
import styles_upload from '#/routes/pages/upload/(upload).module.css?inline'

export default createHandler((context) => (
  <StartServer
    document={(props) => {
      return (
        <html lang='en'>
          <head>
            <meta charset='utf-8' />
            <meta name='viewport' content='width=device-width, initial-scale=1' />
            <style>{context.path === '/' ? styles_home : styles_upload}</style>
            {props.assets}
          </head>
          <body>
            <div id='app'>{props.children}</div>
            {props.scripts}
          </body>
        </html>
      )
    }}
  />
))

This will immediately apply the necessary styles, while also still loading (and caching) the separate CSS file, so no change in styles can be seen.

This solution may imply that all route paths can only have 1 CSS file, for having more CSS files further down in the route paths may result in the same issue (and needing to inline more styles with more route logic) but this works for my use case.

from vinxi.

nksaraf avatar nksaraf commented on July 30, 2024

Should be fixed in v0.0.63 (#80)

from vinxi.

sabercoy avatar sabercoy commented on July 30, 2024

Should be fixed in v0.0.63 (#80)

I am still experiencing this in 0.1.1

from vinxi.

sabercoy avatar sabercoy commented on July 30, 2024

After more research this seems to be a behavior of vite and not a bug. I have found a workaround. No worries!

from vinxi.

nksaraf avatar nksaraf commented on July 30, 2024

Would you like to put some details about the issue and workaround for people coming here later.

from vinxi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.