Skip to content

Commit cba6144

Browse files
authored
[backport] Apply server actions transform to node_modules in route handlers (#89380)
Backports: - #89316
1 parent 3db9063 commit cba6144

File tree

6 files changed

+40
-21
lines changed

6 files changed

+40
-21
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,7 @@ pub async fn get_server_module_options_context(
845845

846846
next_server_rules.extend(common_next_server_rules.iter().cloned());
847847
internal_custom_rules.extend(common_next_server_rules);
848+
foreign_next_server_rules.extend(internal_custom_rules.clone());
848849

849850
let module_options_context = ModuleOptionsContext {
850851
ecmascript: EcmascriptOptionsContext {
@@ -854,7 +855,7 @@ pub async fn get_server_module_options_context(
854855
..module_options_context
855856
};
856857
let foreign_code_module_options_context = ModuleOptionsContext {
857-
module_rules: internal_custom_rules.clone(),
858+
module_rules: foreign_next_server_rules.clone(),
858859
enable_webpack_loaders: foreign_enable_webpack_loaders,
859860
// NOTE(WEB-1016) PostCSS transforms should also apply to foreign code.
860861
enable_postcss_transform: enable_foreign_postcss_transform,
Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
import { cacheLife } from 'next/cache'
2-
3-
async function getCachedRandom(n: number) {
4-
'use cache'
5-
cacheLife('weeks')
6-
return String(Math.ceil(Math.random() * n))
7-
}
1+
import { getCachedRandomWithCacheLife } from 'my-pkg'
82

93
export async function generateStaticParams() {
104
return [
11-
{ id: `a${await getCachedRandom(9)}` },
12-
{ id: `b${await getCachedRandom(2)}` },
5+
{ id: `a${await getCachedRandomWithCacheLife(9)}` },
6+
{ id: `b${await getCachedRandomWithCacheLife(2)}` },
137
]
148
}
159

1610
export default async function Page() {
17-
const value = getCachedRandom(1)
11+
const value = getCachedRandomWithCacheLife(1)
1812

1913
return <p>{value}</p>
2014
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { getCachedRandomWithCacheLife } from 'my-pkg'
2+
import { NextRequest } from 'next/server'
3+
4+
export async function generateStaticParams() {
5+
return [{ id: await getCachedRandomWithCacheLife() }]
6+
}
7+
8+
export async function GET(
9+
req: NextRequest,
10+
{ params }: { params: Promise<{ id: string }> }
11+
) {
12+
const { id } = await params
13+
14+
return Response.json({ id })
15+
}
Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
import { cacheTag } from 'next/cache'
2-
3-
async function getCachedRandom() {
4-
'use cache'
5-
cacheTag('api')
6-
7-
return Math.random()
8-
}
1+
import { getCachedRandomWithTag } from 'my-pkg'
92

103
export async function GET() {
11-
const rand1 = await getCachedRandom()
12-
const rand2 = await getCachedRandom()
4+
const rand1 = await getCachedRandomWithTag('api')
5+
const rand2 = await getCachedRandomWithTag('api')
136

147
return Response.json({ rand1, rand2 })
158
}

test/e2e/app-dir/use-cache/node_modules/my-pkg/index.js

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/e2e/app-dir/use-cache/use-cache.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,8 @@ describe('use-cache', () => {
474474
// [id] route, first entry in generateStaticParams
475475
expect.stringMatching(/\/a\d/),
476476
withCacheComponents && '/api',
477+
// api/[id] route handler using generateStaticParams with 'use cache' from node_modules
478+
expect.stringMatching(/\/api\/\d/),
477479
// [id] route, second entry in generateStaticParams
478480
expect.stringMatching(/\/b\d/),
479481
'/cache-fetch',

0 commit comments

Comments
 (0)