Skip to content

Conversation

hi-ogawa
Copy link
Contributor

@hi-ogawa hi-ogawa commented Sep 13, 2025

Description

Currently we have different css wrapper virtual invalidation strategies.

vite-rsc/css?type=ssr&... is invalidated via multiple addWatchFile:

// invalidate virtual module on js file changes to reflect added/deleted css import
for (const file of [mod.file, ...result.visitedFiles]) {
this.addWatchFile(file)
}

vite-rsc/css?type=rsc&... is invalidated via hotUpdate:

invalidteModuleById(
server.environments.rsc!,
`\0` + toCssVirtual({ id: mod.id, type: 'rsc' }),
)
}

This PR changes type=rsc invalidation to align with type=ssr.

Notes on why single "addWatchFile" isn't enough

I feel like both should just work with one shot addWatchFile. Obviously I tried this before and something wasn't working, but let's try it again and see the issues.

It looks like the issue is that addWatchFile doesn't help chaining invalidation e.g.

virtual?root.tsx ---(addWatchFile)---> root.tsx --(import)--> other.tsx
                 <--(dynamic import)--

Here changing root.tsx invalidates virtual?root.tsx, but changing other.tsx doesn't invalidate virtual?root.tsx. Could this be because addWatchFile makes cycles? Could this be a Vite bug?

(EDIT: It looks like this is because of "soft invalidation". technically virtual is invalidated but it's only "soft invalidation", so it doesn't trigger load/transform again. When changing addWatchFile dep, it causes non-soft invalidation and that's why it works when changing root.tsx. See https://github.com/hi-ogawa/reproductions/tree/main/vite-addWatchFile-indirect-invalidate)


Note that toggling import in examples/starter is working because it toggles import.meta.viteRsc.loadCss transform of root.tsx. It would still fail if there were multiple css imports and toggling them individually.

import './index.css' // css import is automatically injected in exported server components

@hi-ogawa hi-ogawa marked this pull request as ready for review September 14, 2025 06:47
@hi-ogawa hi-ogawa merged commit 78a3f56 into main Sep 15, 2025
23 checks passed
@hi-ogawa hi-ogawa deleted the 09-13-refactor_rsc_use_addwatchfile_to_invalidate_server_css_virtual branch September 15, 2025 02:39
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.

1 participant