Unverified Commit 909dad55 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Turbopack: use edge environment in server-side rendering of client components too (#53099)

### What?

* use the correct edge resolve options for SSR of client components

### Why?

It need to use the edge import conditions here for
https://github.com/vercel/next.js/pull/51083
parent 43640827
...@@ -193,6 +193,38 @@ fn next_ssr_client_module_transition( ...@@ -193,6 +193,38 @@ fn next_ssr_client_module_transition(
) )
} }
#[turbo_tasks::function]
fn next_edge_ssr_client_module_transition(
project_path: Vc<FileSystemPath>,
execution_context: Vc<ExecutionContext>,
app_dir: Vc<FileSystemPath>,
next_config: Vc<NextConfig>,
server_addr: Vc<ServerAddr>,
) -> Vc<Box<dyn Transition>> {
let ty = Value::new(ServerContextType::AppSSR { app_dir });
let mode = NextMode::Development;
Vc::upcast(
NextSSRClientModuleTransition {
ssr_module_options_context: get_server_module_options_context(
project_path,
execution_context,
ty,
mode,
next_config,
),
ssr_resolve_options_context: get_edge_resolve_options_context(
project_path,
ty,
mode,
next_config,
execution_context,
),
ssr_environment: get_edge_compile_time_info(project_path, server_addr),
}
.cell(),
)
}
#[turbo_tasks::function] #[turbo_tasks::function]
fn next_server_component_transition( fn next_server_component_transition(
project_path: Vc<FileSystemPath>, project_path: Vc<FileSystemPath>,
...@@ -377,8 +409,6 @@ fn app_context( ...@@ -377,8 +409,6 @@ fn app_context(
server_addr: Vc<ServerAddr>, server_addr: Vc<ServerAddr>,
output_path: Vc<FileSystemPath>, output_path: Vc<FileSystemPath>,
) -> Vc<ModuleAssetContext> { ) -> Vc<ModuleAssetContext> {
let next_server_to_client_transition = Vc::upcast(NextServerToClientTransition { ssr }.cell());
let mut transitions = HashMap::new(); let mut transitions = HashMap::new();
transitions.insert( transitions.insert(
"next-edge-route".to_string(), "next-edge-route".to_string(),
...@@ -420,6 +450,11 @@ fn app_context( ...@@ -420,6 +450,11 @@ fn app_context(
Vc::cell(ecmacscript_client_reference_transition_name.clone()), Vc::cell(ecmacscript_client_reference_transition_name.clone()),
), ),
); );
transitions.insert(
ecmacscript_client_reference_transition_name,
Vc::upcast(NextServerToClientTransition { ssr, edge: false }.cell()),
);
let ecmacscript_edge_client_reference_transition_name = "edge-server-to-client".to_string();
transitions.insert( transitions.insert(
"next-edge-server-component".to_string(), "next-edge-server-component".to_string(),
next_edge_server_component_transition( next_edge_server_component_transition(
...@@ -430,12 +465,12 @@ fn app_context( ...@@ -430,12 +465,12 @@ fn app_context(
mode, mode,
next_config, next_config,
server_addr, server_addr,
Vc::cell(ecmacscript_client_reference_transition_name.clone()), Vc::cell(ecmacscript_edge_client_reference_transition_name.clone()),
), ),
); );
transitions.insert( transitions.insert(
ecmacscript_client_reference_transition_name, ecmacscript_edge_client_reference_transition_name,
next_server_to_client_transition, Vc::upcast(NextServerToClientTransition { ssr, edge: true }.cell()),
); );
transitions.insert( transitions.insert(
"next-client".to_string(), "next-client".to_string(),
...@@ -473,6 +508,16 @@ fn app_context( ...@@ -473,6 +508,16 @@ fn app_context(
server_addr, server_addr,
), ),
); );
transitions.insert(
"next-edge-ssr-client-module".to_string(),
next_edge_ssr_client_module_transition(
project_path,
execution_context,
app_dir,
next_config,
server_addr,
),
);
let ssr_ty = Value::new(ServerContextType::AppSSR { app_dir }); let ssr_ty = Value::new(ServerContextType::AppSSR { app_dir });
ModuleAssetContext::new( ModuleAssetContext::new(
......
...@@ -16,6 +16,7 @@ use crate::embed_js::next_asset; ...@@ -16,6 +16,7 @@ use crate::embed_js::next_asset;
#[turbo_tasks::value(shared)] #[turbo_tasks::value(shared)]
pub struct NextServerToClientTransition { pub struct NextServerToClientTransition {
pub ssr: bool, pub ssr: bool,
pub edge: bool,
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -41,9 +42,12 @@ impl Transition for NextServerToClientTransition { ...@@ -41,9 +42,12 @@ impl Transition for NextServerToClientTransition {
Ok(match this.ssr { Ok(match this.ssr {
true => { true => {
let internal_source = next_asset("entry/app/server-to-client-ssr.tsx".to_string()); let internal_source = next_asset("entry/app/server-to-client-ssr.tsx".to_string());
let client_module = context let transition = if this.edge {
.with_transition("next-ssr-client-module".to_string()) "next-edge-ssr-client-module"
.process( } else {
"next-ssr-client-module"
};
let client_module = context.with_transition(transition.to_string()).process(
source, source,
Value::new(ReferenceType::Entry( Value::new(ReferenceType::Entry(
EntryReferenceSubType::AppClientComponent, EntryReferenceSubType::AppClientComponent,
......
import edgeThenNode from 'edge-then-node' import edgeThenNode from 'edge-then-node'
import nodeThenEdge from 'node-then-edge' import nodeThenEdge from 'node-then-edge'
import ClientComponent from '../client'
export const runtime = 'edge' export const runtime = 'edge'
export default function AppEdge() { export default function AppEdge() {
return JSON.stringify({ return (
<>
<div id="server">
{JSON.stringify({
edgeThenNode, edgeThenNode,
nodeThenEdge, nodeThenEdge,
}) })}
</div>
<ClientComponent />
</>
)
} }
import edgeThenNode from 'edge-then-node' import edgeThenNode from 'edge-then-node'
import nodeThenEdge from 'node-then-edge' import nodeThenEdge from 'node-then-edge'
import ClientComponent from '../client'
export const runtime = 'nodejs' export const runtime = 'nodejs'
export default function AppNodeJs() { export default function AppNodeJs() {
return JSON.stringify({ return (
<>
<div id="server">
{JSON.stringify({
edgeThenNode, edgeThenNode,
nodeThenEdge, nodeThenEdge,
}) })}
</div>
<ClientComponent />
</>
)
} }
'use client'
import edgeThenNode from 'edge-then-node'
import nodeThenEdge from 'node-then-edge'
export const runtime = 'edge'
export default function ClientComponent() {
return (
<div id="client">
{JSON.stringify({
edgeThenNode,
nodeThenEdge,
})}
</div>
)
}
...@@ -9,9 +9,24 @@ export default function Test() { ...@@ -9,9 +9,24 @@ export default function Test() {
async function getJson(url) { async function getJson(url) {
const res = await fetch(url) const res = await fetch(url)
const text = await res.text() const text = await res.text()
const jsonText = /(\{[^}]*\})/.exec(text) const regex = /<div id="(\w+)">(\{[^}]*\})/g
const matches = text.matchAll(regex)
const result = {}
try { try {
return JSON.parse(jsonText[0].replace(/&quot;/g, '"')) for (const match of matches) {
result[match[1]] = JSON.parse(match[2].replace(/&quot;/g, '"'))
}
return result
} catch (err) {
throw new Error(`Expected JSON but got:\n${text}`)
}
}
async function getJsonApi(url) {
const res = await fetch(url)
const text = await res.text()
try {
return JSON.parse(text)
} catch (err) { } catch (err) {
throw new Error(`Expected JSON but got:\n${text}`) throw new Error(`Expected JSON but got:\n${text}`)
} }
...@@ -21,8 +36,10 @@ function runTests() { ...@@ -21,8 +36,10 @@ function runTests() {
it('page with nodejs runtime should import node conditions', async () => { it('page with nodejs runtime should import node conditions', async () => {
const json = await getJson('/page-nodejs') const json = await getJson('/page-nodejs')
expect(json).toMatchObject({ expect(json).toMatchObject({
server: {
edgeThenNode: 'node', edgeThenNode: 'node',
nodeThenEdge: 'node', nodeThenEdge: 'node',
},
}) })
}) })
...@@ -31,18 +48,22 @@ function runTests() { ...@@ -31,18 +48,22 @@ function runTests() {
// TODO We don't currently support edge config in page rendering. // TODO We don't currently support edge config in page rendering.
// When we do, this needs to be updated. // When we do, this needs to be updated.
expect(json).not.toMatchObject({ expect(json).not.toMatchObject({
server: {
edgeThenNode: 'edge', edgeThenNode: 'edge',
nodeThenEdge: 'edge', nodeThenEdge: 'edge',
},
}) })
// TODO: delete this. // TODO: delete this.
expect(json).toMatchObject({ expect(json).toMatchObject({
server: {
edgeThenNode: 'node', edgeThenNode: 'node',
nodeThenEdge: 'node', nodeThenEdge: 'node',
},
}) })
}) })
it('page api with nodejs runtime should import node conditions', async () => { it('page api with nodejs runtime should import node conditions', async () => {
const json = await getJson('/api/api-nodejs') const json = await getJsonApi('/api/api-nodejs')
expect(json).toMatchObject({ expect(json).toMatchObject({
edgeThenNode: 'node', edgeThenNode: 'node',
nodeThenEdge: 'node', nodeThenEdge: 'node',
...@@ -50,7 +71,7 @@ function runTests() { ...@@ -50,7 +71,7 @@ function runTests() {
}) })
it('page api with edge runtime should import edge conditions', async () => { it('page api with edge runtime should import edge conditions', async () => {
const json = await getJson('/api/api-edge') const json = await getJsonApi('/api/api-edge')
expect(json).toMatchObject({ expect(json).toMatchObject({
edgeThenNode: 'edge', edgeThenNode: 'edge',
nodeThenEdge: 'edge', nodeThenEdge: 'edge',
...@@ -60,21 +81,33 @@ function runTests() { ...@@ -60,21 +81,33 @@ function runTests() {
it('app with nodejs runtime should import node conditions', async () => { it('app with nodejs runtime should import node conditions', async () => {
const json = await getJson('/app-nodejs') const json = await getJson('/app-nodejs')
expect(json).toMatchObject({ expect(json).toMatchObject({
server: {
edgeThenNode: 'node',
nodeThenEdge: 'node',
},
client: {
edgeThenNode: 'node', edgeThenNode: 'node',
nodeThenEdge: 'node', nodeThenEdge: 'node',
},
}) })
}) })
it('app with edge runtime should import edge conditions', async () => { it('app with edge runtime should import edge conditions', async () => {
const json = await getJson('/app-edge') const json = await getJson('/app-edge')
expect(json).toMatchObject({ expect(json).toMatchObject({
server: {
edgeThenNode: 'edge',
nodeThenEdge: 'edge',
},
client: {
edgeThenNode: 'edge', edgeThenNode: 'edge',
nodeThenEdge: 'edge', nodeThenEdge: 'edge',
},
}) })
}) })
it('app route with nodejs runtime should import node conditions', async () => { it('app route with nodejs runtime should import node conditions', async () => {
const json = await getJson('/route-nodejs') const json = await getJsonApi('/route-nodejs')
expect(json).toMatchObject({ expect(json).toMatchObject({
edgeThenNode: 'node', edgeThenNode: 'node',
nodeThenEdge: 'node', nodeThenEdge: 'node',
...@@ -82,7 +115,7 @@ function runTests() { ...@@ -82,7 +115,7 @@ function runTests() {
}) })
it('app route with edge runtime should import edge conditions', async () => { it('app route with edge runtime should import edge conditions', async () => {
const json = await getJson('/route-edge') const json = await getJsonApi('/route-edge')
expect(json).toMatchObject({ expect(json).toMatchObject({
edgeThenNode: 'edge', edgeThenNode: 'edge',
nodeThenEdge: 'edge', nodeThenEdge: 'edge',
...@@ -90,8 +123,7 @@ function runTests() { ...@@ -90,8 +123,7 @@ function runTests() {
}) })
it('middleware should import edge conditions', async () => { it('middleware should import edge conditions', async () => {
const res = await fetch('/middleware') const json = await getJsonApi('/middleware')
const json = await res.json()
expect(json).toMatchObject({ expect(json).toMatchObject({
edgeThenNode: 'edge', edgeThenNode: 'edge',
nodeThenEdge: 'edge', nodeThenEdge: 'edge',
......
...@@ -6,8 +6,12 @@ export const config = { ...@@ -6,8 +6,12 @@ export const config = {
} }
export default function PageEdge() { export default function PageEdge() {
return JSON.stringify({ return (
<div id="server">
{JSON.stringify({
edgeThenNode, edgeThenNode,
nodeThenEdge, nodeThenEdge,
}) })}
</div>
)
} }
...@@ -6,8 +6,12 @@ export const config = { ...@@ -6,8 +6,12 @@ export const config = {
} }
export default function PageNodeJs() { export default function PageNodeJs() {
return JSON.stringify({ return (
<div id="server">
{JSON.stringify({
edgeThenNode, edgeThenNode,
nodeThenEdge, nodeThenEdge,
}) })}
</div>
)
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment