Skip to content

Commit 122170a

Browse files
committed
Ensure codeframe when calling Client Functions from the Server
1 parent aacb58d commit 122170a

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

crates/next-core/src/next_client_reference/ecmascript_client_reference/ecmascript_client_reference_module.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,15 @@ impl EcmascriptClientReferenceModule {
149149

150150
let proxy_source = VirtualSource::new(
151151
self.server_ident.path().await?.join(
152-
// Depending on the original format, we call the file `proxy.mjs` or `proxy.cjs`.
153-
// This is because we're placing the virtual module next to the original code, so
154-
// its parsing will be affected by `type` fields in package.json --
155-
// a bare `proxy.js` may end up being unexpectedly parsed as the wrong format.
156-
&format!("proxy.{}", if is_esm { "mjs" } else { "cjs" }),
152+
// We choose the extension based on the original file because we're placing the
153+
// virtual module next to the original code, so its parsing will be
154+
// affected by `type` fields in package.json -- a bare `proxy.js`
155+
// may end up being unexpectedly parsed as the wrong format.
156+
// The name special cased later to always ignore-list this module.
157+
&format!(
158+
"__nextjs-internal-proxy.{}",
159+
if is_esm { "mjs" } else { "cjs" }
160+
),
157161
)?,
158162
proxy_module_content,
159163
);

packages/next/src/build/webpack/config/blocks/base.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { getRspackCore } from '../../../../shared/lib/get-rspack'
99
function shouldIgnorePath(modulePath: string): boolean {
1010
return (
1111
modulePath.includes('node_modules') ||
12+
modulePath.endsWith('__nextjs-internal-proxy.cjs') ||
13+
modulePath.endsWith('__nextjs-internal-proxy.mjs') ||
1214
// Only relevant for when Next.js is symlinked e.g. in the Next.js monorepo
1315
modulePath.includes('next/dist')
1416
)

test/development/app-dir/use-cache-errors/use-cache-errors.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { nextTestSetup } from 'e2e-utils'
22
import { assertNoRedbox } from '../../../lib/next-test-utils'
33

44
describe('use-cache-errors', () => {
5-
const { next } = nextTestSetup({
5+
const { isTurbopack, next } = nextTestSetup({
66
files: __dirname,
77
})
88
const isRspack = Boolean(process.env.NEXT_RSPACK)
@@ -33,7 +33,24 @@ describe('use-cache-errors', () => {
3333
],
3434
}
3535
`)
36+
} else if (isTurbopack) {
37+
await expect(browser).toDisplayRedbox(`
38+
{
39+
"description": "Attempted to call useStuff() from the server but useStuff is on the client. It's not possible to invoke a client function from the server, it can only be rendered as a Component or passed to props of a Client Component.",
40+
"environmentLabel": "Cache",
41+
"label": "Runtime Error",
42+
"source": "app/module-with-use-cache.ts (16:18) @ useCachedStuff
43+
> 16 | return useStuff()
44+
| ^",
45+
"stack": [
46+
"useCachedStuff app/module-with-use-cache.ts (16:18)",
47+
"Page app/page.tsx (22:10)",
48+
],
49+
}
50+
`)
3651
} else {
52+
// TODO(veil): Webpack does sourcemap the client reference module.
53+
// codeframe just lucks out due to being able to sourcemap.
3754
await expect(browser).toDisplayRedbox(`
3855
{
3956
"description": "Attempted to call useStuff() from the server but useStuff is on the client. It's not possible to invoke a client function from the server, it can only be rendered as a Component or passed to props of a Client Component.",

turbopack/crates/turbopack-core/src/source_map/utils.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub fn add_default_ignore_list(map: &mut swc_sourcemap::SourceMap) {
1919
if source.starts_with(concatcp!(SOURCE_URL_PROTOCOL, "///[next]"))
2020
|| source.starts_with(concatcp!(SOURCE_URL_PROTOCOL, "///[turbopack]"))
2121
|| source.contains("/node_modules/")
22+
|| source.ends_with("__nextjs-internal-proxy.cjs")
23+
|| source.ends_with("__nextjs-internal-proxy.mjs")
2224
{
2325
ignored_ids.insert(source_id);
2426
}

0 commit comments

Comments
 (0)