Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stale styles: Multiple <style> are injected on <head> on save, for the same component - Monorepo structure #1305

Open
JoaoFGuiomar opened this issue Jul 27, 2023 · 1 comment
Labels
bug report 🦗 Issue is probably a bug, but it needs to be checked cat: monorepo 🔱 Issues related to usage of linaria in monorepo needs: complete repro 🖥️ Issue need to have complete repro provided

Comments

@JoaoFGuiomar
Copy link

Environment

This particular case is happening in a Monorepo configure with rush

  • Linaria version:
    • linaria/vite 4.5.3
    • linaria/react 4.5.3
    • linaria/core 4.5.3
  • Bundler (+ version): Vite 4.3.9
  • Node.js version: 18.12
  • OS: Mac OS Ventura 13.4.1

Description

Best way to describe this is to check the video below.

Quick TLDR:
When changing styles on a component, the <head> element is injected with multiple <style> elements for the same component. Previous styles for the same component don't seem to be cleared.

This makes it look like the page doesn't react to style changes on save.
A hard refresh (refresh the browser manually) clears all old styles and the page looks good.

This only happens when we change to previously used styles

Example

  • font-size is in the beginning 3rem
  • update font-size to 4rem
  • app refreshes, styles are correct ✅
  • update font-size to 3rem (previous value)
  • app refreshes, styles are incorrect ❌
  • checking the DOM, we see multiple <style> elements for the same component

⚠️ This only seems to happen to imported components. ⚠️
The "Dotted" styled component in the "App.tsx" file updates fine if we change its values.

Reproducible Demo

I created a minimal reproducible repository:
https://github.com/JoaoFGuiomar/minimal-repo

Instructions are on the README. Will also drop a quick video below

Bug.Video.mov

Thanks 👍

@JoaoFGuiomar JoaoFGuiomar added bug report 🦗 Issue is probably a bug, but it needs to be checked needs: complete repro 🖥️ Issue need to have complete repro provided needs: triage 🏷 Issue needs to be checked and prioritized labels Jul 27, 2023
@github-actions github-actions bot added cat: monorepo 🔱 Issues related to usage of linaria in monorepo and removed needs: triage 🏷 Issue needs to be checked and prioritized labels Jul 27, 2023
@JoaoFGuiomar JoaoFGuiomar changed the title Stale styles: Multiple <styled> are injected on <head> on save, for the same component - Monorepo structure Stale styles: Multiple <style> are injected on <head> on save, for the same component - Monorepo structure Jul 27, 2023
@lukaskl
Copy link

lukaskl commented Jul 27, 2023

It seems that there is a way to workaround it, using vite plugin to transform every single JS/TS file and add this line to all those files:

import.meta.hot;

so the plugin would be as simple as:

{
  name: "inject-hmr-for-linaria",
  transform(code: string, id: string) {
    if ([".js", ".ts", ".tsx"].some((x) => id.endsWith(x))) {
      return `${code}

      import.meta.hot;
      `;
    }
  },
}
Full vite.config.ts example:
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import linaria from "@linaria/vite";

const jsExtension = [".js", ".ts", ".tsx"]

export default ({ command, mode, ssrBuild }) => {
  const isDev = mode === "development";

  return defineConfig({
    build: {
      outDir: "build",
    },
    plugins: [
      linaria(),
      react({
        jsxRuntime: "classic",
      }),

      isDev && {
        name: "inject-hmr-for-linaria",
        transform(code: string, id: string) {
          if (jsExtension.some((x) => id.endsWith(x))) {
            return `${code}

            import.meta.hot;
            `;
          }
        },
      },
    ].filter((x) => !!x),
  });
};

However, I would love that I wouldn't have to do such "workaround"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug report 🦗 Issue is probably a bug, but it needs to be checked cat: monorepo 🔱 Issues related to usage of linaria in monorepo needs: complete repro 🖥️ Issue need to have complete repro provided
Projects
None yet
Development

No branches or pull requests

2 participants