Skip to content

Commit 04d8fdb

Browse files
committed
Correctly skip CSS in SSR
1 parent b7e23da commit 04d8fdb

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

crates/next-api/src/app.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,18 @@ impl AppProject {
647647
Ok(ModuleAssetContext::new(
648648
TransitionOptions {
649649
named_transitions: transitions,
650+
transition_rules: vec![
651+
// Change context, this is used to determine the list of CSS module classes.
652+
TransitionRule::new(
653+
RuleCondition::all(vec![
654+
RuleCondition::ReferenceType(ReferenceType::Css(
655+
CssReferenceSubType::Analyze,
656+
)),
657+
module_styles_rule_condition(),
658+
]),
659+
ResolvedVc::upcast(self.client_transition().to_resolved().await?),
660+
),
661+
],
650662
..Default::default()
651663
}
652664
.cell(),
@@ -707,6 +719,18 @@ impl AppProject {
707719
Ok(ModuleAssetContext::new(
708720
TransitionOptions {
709721
named_transitions: transitions,
722+
transition_rules: vec![
723+
// Change context, this is used to determine the list of CSS module classes.
724+
TransitionRule::new(
725+
RuleCondition::all(vec![
726+
RuleCondition::ReferenceType(ReferenceType::Css(
727+
CssReferenceSubType::Analyze,
728+
)),
729+
module_styles_rule_condition(),
730+
]),
731+
ResolvedVc::upcast(self.client_transition().to_resolved().await?),
732+
),
733+
],
710734
..Default::default()
711735
}
712736
.cell(),

crates/next-core/src/next_server/transforms.rs

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use next_custom_transforms::transforms::strip_page_exports::ExportFilter;
33
use turbo_rcstr::RcStr;
44
use turbo_tasks::{ResolvedVc, Vc};
55
use turbopack::module_options::{ModuleRule, ModuleRuleEffect, RuleCondition};
6-
use turbopack_core::reference_type::{ReferenceType, UrlReferenceSubType};
6+
use turbopack_core::reference_type::{CssReferenceSubType, ReferenceType, UrlReferenceSubType};
77

88
use crate::{
99
mode::NextMode,
@@ -21,7 +21,7 @@ use crate::{
2121
next_page_static_info::get_next_page_static_info_assert_rule,
2222
next_pure::get_next_pure_rule, server_actions::ActionsTransform,
2323
},
24-
util::NextRuntime,
24+
util::{NextRuntime, module_styles_rule_condition, styles_rule_condition},
2525
};
2626

2727
/// Returns a list of module rules which apply server-side, Next.js-specific
@@ -51,35 +51,18 @@ pub async fn get_next_server_transforms_rules(
5151

5252
if !matches!(context_ty, ServerContextType::AppRSC { .. }) {
5353
rules.extend([
54-
// Ignore the internal ModuleCssAsset -> CssModuleAsset references
55-
// The CSS Module module itself is still needed for class names
56-
ModuleRule::new_internal(
57-
RuleCondition::any(vec![
58-
RuleCondition::ResourcePathEndsWith(".module.css".into()),
59-
RuleCondition::ContentTypeStartsWith("text/css+module".into()),
60-
]),
61-
vec![ModuleRuleEffect::Ignore],
62-
),
63-
]);
64-
rules.extend([
65-
// Ignore all non-module CSS references
54+
// Ignore the inner ModuleCssAsset -> CssModuleAsset references
55+
// The CSS Module module itself (and the Analyze reference) is still needed to generate
56+
// the class names object.
6657
ModuleRule::new(
67-
RuleCondition::any(vec![
68-
RuleCondition::all(vec![
69-
RuleCondition::ResourcePathEndsWith(".css".into()),
70-
RuleCondition::not(RuleCondition::ResourcePathEndsWith(
71-
".module.css".into(),
72-
)),
73-
]),
74-
RuleCondition::all(vec![
75-
RuleCondition::ContentTypeStartsWith("text/css".into()),
76-
RuleCondition::not(RuleCondition::ContentTypeStartsWith(
77-
"text/css+module".into(),
78-
)),
79-
]),
58+
RuleCondition::all(vec![
59+
RuleCondition::ReferenceType(ReferenceType::Css(CssReferenceSubType::Inner)),
60+
module_styles_rule_condition(),
8061
]),
8162
vec![ModuleRuleEffect::Ignore],
8263
),
64+
// Ignore all non-module CSS references
65+
ModuleRule::new(styles_rule_condition(), vec![ModuleRuleEffect::Ignore]),
8366
]);
8467
}
8568

0 commit comments

Comments
 (0)