Skip to content

Commit

Permalink
Add hack to detect server components in edge properly
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Dec 2, 2024
1 parent f1d615e commit 5f3993d
Showing 1 changed file with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,11 @@ pub async fn client_reference_graph(
server_component: *server_component,
}
} else {
VisitClientReferenceNodeState::Entry {
entry_path: module.ident().path().resolve().await?,
let entry_path = module.ident().path().resolve().await?;
if entry_path.await?.file_name() == "edge-wrapper.js" {
VisitClientReferenceNodeState::EdgeEntry { entry_path }
} else {
VisitClientReferenceNodeState::Entry { entry_path }
}
},
ty: VisitClientReferenceNodeType::Internal(
Expand Down Expand Up @@ -221,13 +224,16 @@ pub struct ServerEntries {

#[turbo_tasks::function]
pub async fn find_server_entries(entry: ResolvedVc<Box<dyn Module>>) -> Result<Vc<ServerEntries>> {
let entry_path = entry.ident().path().resolve().await?;
let graph = AdjacencyMap::new()
.skip_duplicates()
.visit(
vec![VisitClientReferenceNode {
state: {
VisitClientReferenceNodeState::Entry {
entry_path: entry.ident().path().resolve().await?,
if entry_path.await?.file_name() == "edge-wrapper.js" {
VisitClientReferenceNodeState::EdgeEntry { entry_path }
} else {
VisitClientReferenceNodeState::Entry { entry_path }
}
},
ty: VisitClientReferenceNodeType::Internal(entry, entry.ident().to_string().await?),
Expand Down Expand Up @@ -279,6 +285,9 @@ struct VisitClientReferenceNode {
Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, Debug, ValueDebugFormat, TraceRawVcs,
)]
enum VisitClientReferenceNodeState {
EdgeEntry {
entry_path: Vc<FileSystemPath>,
},
Entry {
entry_path: Vc<FileSystemPath>,
},
Expand All @@ -290,6 +299,7 @@ enum VisitClientReferenceNodeState {
impl VisitClientReferenceNodeState {
fn server_component(&self) -> Option<Vc<NextServerComponentModule>> {
match self {
VisitClientReferenceNodeState::EdgeEntry { .. } => None,
VisitClientReferenceNodeState::Entry { .. } => None,
VisitClientReferenceNodeState::InServerComponent { server_component } => {
Some(*server_component)
Expand Down Expand Up @@ -412,6 +422,29 @@ impl Visit<VisitClientReferenceNode> for VisitClientReference {
});
}

// TODO have a cleaner way to check for template modules
// e.g. edge-wrapper.js -> edge-ssr-app.js -> app-page.js

// Skip over edge-wrapper.js -> edge-ssr-app.js and properly detect server utils
if let VisitClientReferenceNodeState::EdgeEntry { entry_path } = &node.state {
let module_path = module.ident().path().resolve().await?;
let module_path_ref = &module_path.await?.path;
if module_path != *entry_path
&& !module_path_ref
.ends_with("next/dist/esm/build/templates/edge-ssr-app.js")
{
return Ok(VisitClientReferenceNode {
state: VisitClientReferenceNodeState::Entry {
entry_path: module_path,
},
ty: VisitClientReferenceNodeType::Internal(
module,
module.ident().to_string().await?,
),
});
}
}

if let VisitClientReferenceNodeState::Entry { entry_path } = &node.state {
if module.ident().path().resolve().await? != *entry_path {
return Ok(VisitClientReferenceNode {
Expand Down

0 comments on commit 5f3993d

Please sign in to comment.