惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Full Disclosure
Stack Overflow Blog
Stack Overflow Blog
H
Help Net Security
Microsoft Azure Blog
Microsoft Azure Blog
The GitHub Blog
The GitHub Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Recorded Future
Recorded Future
Y
Y Combinator Blog
Cloudbric
Cloudbric
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Schneier on Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
C
Cybersecurity and Infrastructure Security Agency CISA
TaoSecurity Blog
TaoSecurity Blog
Security Latest
Security Latest
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
D
DataBreaches.Net
Security Archives - TechRepublic
Security Archives - TechRepublic
H
Hacker News: Front Page
C
Cisco Blogs
L
LangChain Blog
aimingoo的专栏
aimingoo的专栏
Recent Commits to openclaw:main
Recent Commits to openclaw:main
V
Vulnerabilities – Threatpost
L
LINUX DO - 最新话题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
A
About on SuperTechFans
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Jina AI
Jina AI
C
CXSECURITY Database RSS Feed - CXSecurity.com
Schneier on Security
Schneier on Security
T
Tenable Blog
N
News and Events Feed by Topic
W
WeLiveSecurity
有赞技术团队
有赞技术团队
AI
AI
爱范儿
爱范儿
大猫的无限游戏
大猫的无限游戏
Latest news
Latest news
T
The Blog of Author Tim Ferriss
S
Security Affairs
Know Your Adversary
Know Your Adversary
Simon Willison's Weblog
Simon Willison's Weblog
G
GRAHAM CLULEY
Google DeepMind News
Google DeepMind News
The Cloudflare Blog

Kent C. Dodds Blog

Implementing Hybrid Semantic + Lexical Search Simplifying Containers with Cloudflare Sandboxes Migrating to Workspaces and Nx Offloading FFmpeg with Cloudflare Building Semantic Search on my Content Helping YOU ask ME questions with AI How I used Cursor to Migrate Frameworks The Dow's Start on the Covenant Path 2025 in Review The next chapter: EpicAI.pro AI is taking your job How I increased my visibility Launching Epic Web 2023 in Review Stop Being a Junior RSC with Dan Abramov and Joe Savona Live Stream Fixing a Memory Leak in a Production Node.js App 2022 in Review My Car Accident I Migrated from a Postgres Cluster to Distributed SQLite with LiteFS I'm building EpicWeb.dev A review of my time at Remix Remix: The Yang to React's Yin How I help you build better websites Why I Love Remix The State Initializer Pattern How to React ⚛️ Get a catch block error message with TypeScript Building an awesome image loading experience Introducing the new kentcdodds.com How I built a modern website in 2021 How to use React Context effectively Static vs Unit vs Integration vs E2E Testing for Frontend Apps The Testing Trophy and Testing Classifications Array reduce vs chaining vs for loop Don't Solve Problems, Eliminate Them Super Simple Start to Remix Super Simple Start to ESModules in Node.js JavaScript Pass By Value Function Parameters How to write a Constrained Identity Function (CIF) in TypeScript How to optimize your context value How to write a React Component in TypeScript TypeScript Function Syntaxes Listify a JavaScript Array Build vs Buy: Component Libraries edition Using fetch with TypeScript Wrapping React.useState with TypeScript Define function overload types with TypeScript 2020 in Review Business and Engineering alignment Hi, thanks for reaching out to me 👋 useEffect vs useLayoutEffect Super simple start to Firebase functions Super simple start to Netlify functions Super Simple Start to css variables Favor Progress Over Pride in Open Source Testing Implementation Details How getting into Open Source has been awesome for me useState lazy initialization and function updates Use ternaries rather than && in JSX Application State Management with React Use react-error-boundary to handle errors in React JavaScript to Know for React How I structure Express apps What open source project should I contribute to? When I follow TDD AHA Programming 💡 How I Record Educational Videos Should I write a test or fix a bug? Stop mocking fetch Intentional Career Building Improve test error messages of your abstractions Tracing user interactions with React Eliminate an entire category of bugs with a few simple tools Common mistakes with React Testing Library Super Simple Start to React Stop using client-side route redirects The State Reducer Pattern with React Hooks Function forms Replace axios with a simple custom fetch wrapper How to test custom React hooks React Production Performance Monitoring Should I useState or useReducer? Stop using isLoading booleans Make Your Test Fail Make your own DevTools An Argument for Automation Fix the "not wrapped in act(...)" warning Super Simple Start to ESModules in the Browser Implementing a simple state machine library in JavaScript 2010s Decade in Review Why users care about how you write code Why I avoid nesting closures Don't call a React function component Why your team needs TestingJavaScript.com Inversion of Control Understanding React's key prop How to Enable React Concurrent Mode How to add testing to an existing project Profile a React App for Performance
How Remix makes CSS clashes predictable
2021-10-13 · via Kent C. Dodds Blog

CSS can be challenging at scale. This fact is evident based on the number of solutions people have come up with to make it easier to deal with. The biggest challenge with CSS is this:

How can you be sure your CSS changes aren't having an unexpected impact?

This is due to the "C" part of CSS: "Cascading Style Sheets". I've been doing this web dev thing since around 2013. I've been through just about every solution for this CSS challenge you can think of. From regular CSS with good naming conventions, through pre-processors, css-modules, css-in-js, and utility css classes.

In my post How I built a modern website in 2021, I explain that this website uses Tailwind CSS for styling. I couldn't be happier with this because it makes it so easy for me to have a maintainable and consistent styling solution all in around 12kbs of CSS for the whole site.

So my site isn't really taking advantage of the feature I'm about to show you, but I want to tell you all about it anyway because it's a brilliantly simple feature unique to Remix that has a profound impact on developer productivity and performance.

Ok, so let's say that on my About Me page, I wanted to customize the styling of something. For example, what if I wanted to make all the h1s blue? If you wanted to do that on your own about page, how would you do it? If you're using CSS-in-JS, let's imagine for a moment that you're not. So you'd probably want to do something like this:

.about-page h1 {
	color: blue;
}

And then you'd make sure you have the about-page class name applied somewhere high in your DOM tree.

Why the about-page class name? To namespace it right? You wouldn't want to have h1s on other pages to suddenly all be blue. But let's think about the problem here. We only need this CSS to be on the page when the user's on the /about page right? So why do we even have the CSS on the other pages? Wouldn't it be better if we just... like... don't have the CSS on any page other than the /about page?

And I'm not talking about just lazy-loading the CSS or anything. That's not enough. We need the CSS to not only arrive in the browser when the user gets to the /about page but also make sure it's removed from the page when the user navigates away from the /about page.

And that is the magic ingredient of Remix's anti-clash potion. Observe:

Gif showing a link tag getting removed when navigating from the about page to the homepage

Allow me to describe what's happening in this gif. We start on the /about page and there's a <link /> tag for a about-[hash].css file. That file's making our h1 title blue. Then when we navigate to the homepage, that <link /> tag is removed from the page and the h1 is restored to its regular white color.

Pretty simple right? When I first saw this I just sat there processing what I was seeing. It's almost too simple and I'm almost mad I never thought about it. I expect Remix isn't the first to ever have this idea, but it's certainly not a popular idea. I don't know of any other frameworks or tools that enable this.

Let me take it a step further though. Just in case it's not clear how profound this is. Here's how this works from a code standpoint:

import type { LinksFunction } from 'remix'
import aboutStyles from '#app/styles/routes/about.css'

export const links: LinksFunction = () => {
	return [{ rel: 'stylesheet', href: aboutStyles }]
}

export default function AboutScreen() {
	return <stuff />
}

In remix, when you import a .css file, it gives you back a URL.

What this route module does is tell Remix: "When this route is active on the page, here are the link tags I need on the page." And Remix ensures that those link tags are on the page and also that they're removed from the page when that route is not active.

So what does this mean in practice? It means that when you're working in a CSS file, you can find exactly where it's being used (which routes it's being used in) and you know exactly what impact your changes will have.

In practice, you typically will have your CSS file used on a single route, so there's normally only one page you need to worry about. So you can freely develop your CSS without worrying that your changes are impacting any other page than the one you're looking for. You don't need to even follow a namespace convention if you don't want to. You don't need a tool to auto-namespace your CSS styles for you. And Remix isn't doing anything to your CSS either. It's just loading and unloading your CSS for you.

Reusable components?

But what about the CSS for reusable components? For these, you'll need to make sure their CSS files are on every page you use the component. In practice, it's most likely you'll just want this on every page so you can put it in your root route so it's on every page. Once you do that, you have to acknowledge that when you make a change to that file (or any other CSS file you load on the root route) will be active on every page (which again, is what you want 😅).

In this case it's actually less about reusable components and more about remembering to think about the pages where the CSS file appears and ensure you're properly namespacing relative to the other CSS files that will be on the pages your CSS file is active on.

The point of this post is more to call out this fact:

With Remix, it's possible to be explicit about the pages upon which your CSS file is active and it's possible to statically determine those pages for a given CSS file.

Having a predictable outcome to the changes you're making is a huge win for maintainability.

Tailwind

As mentioned, this website is using Tailwind. Before this site, I had never used tailwind. But Stephan recommended that we go with it. I figured it might make us productive as we got started, then I could back out of tailwind over time later and use this feature of Remix.

By the end of it I realized that Tailwind does more than side-step the Cascade clashing issues, it also gives you a great set of constraints to keep your UI looking consistent. So I decided to keep it.

Conclusion

So what do I recommend? I suggest you skip to the end and use Tailwind. But when you're in need of some one-off styles (or if you really don't want to use Tailwind), it's really nice to know that Remix has your back for that sort of thing making for a stellar maintainable CSS solution.