Unverified Commit cba6144e authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

[backport] Apply server actions transform to `node_modules` in route handlers (#89380)

Backports:

- #89316
parent 3db90632
...@@ -845,6 +845,7 @@ pub async fn get_server_module_options_context( ...@@ -845,6 +845,7 @@ pub async fn get_server_module_options_context(
next_server_rules.extend(common_next_server_rules.iter().cloned()); next_server_rules.extend(common_next_server_rules.iter().cloned());
internal_custom_rules.extend(common_next_server_rules); internal_custom_rules.extend(common_next_server_rules);
foreign_next_server_rules.extend(internal_custom_rules.clone());
let module_options_context = ModuleOptionsContext { let module_options_context = ModuleOptionsContext {
ecmascript: EcmascriptOptionsContext { ecmascript: EcmascriptOptionsContext {
...@@ -854,7 +855,7 @@ pub async fn get_server_module_options_context( ...@@ -854,7 +855,7 @@ pub async fn get_server_module_options_context(
..module_options_context ..module_options_context
}; };
let foreign_code_module_options_context = ModuleOptionsContext { let foreign_code_module_options_context = ModuleOptionsContext {
module_rules: internal_custom_rules.clone(), module_rules: foreign_next_server_rules.clone(),
enable_webpack_loaders: foreign_enable_webpack_loaders, enable_webpack_loaders: foreign_enable_webpack_loaders,
// NOTE(WEB-1016) PostCSS transforms should also apply to foreign code. // NOTE(WEB-1016) PostCSS transforms should also apply to foreign code.
enable_postcss_transform: enable_foreign_postcss_transform, enable_postcss_transform: enable_foreign_postcss_transform,
......
import { cacheLife } from 'next/cache' import { getCachedRandomWithCacheLife } from 'my-pkg'
async function getCachedRandom(n: number) {
'use cache'
cacheLife('weeks')
return String(Math.ceil(Math.random() * n))
}
export async function generateStaticParams() { export async function generateStaticParams() {
return [ return [
{ id: `a${await getCachedRandom(9)}` }, { id: `a${await getCachedRandomWithCacheLife(9)}` },
{ id: `b${await getCachedRandom(2)}` }, { id: `b${await getCachedRandomWithCacheLife(2)}` },
] ]
} }
export default async function Page() { export default async function Page() {
const value = getCachedRandom(1) const value = getCachedRandomWithCacheLife(1)
return <p>{value}</p> return <p>{value}</p>
} }
import { getCachedRandomWithCacheLife } from 'my-pkg'
import { NextRequest } from 'next/server'
export async function generateStaticParams() {
return [{ id: await getCachedRandomWithCacheLife() }]
}
export async function GET(
req: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const { id } = await params
return Response.json({ id })
}
import { cacheTag } from 'next/cache' import { getCachedRandomWithTag } from 'my-pkg'
async function getCachedRandom() {
'use cache'
cacheTag('api')
return Math.random()
}
export async function GET() { export async function GET() {
const rand1 = await getCachedRandom() const rand1 = await getCachedRandomWithTag('api')
const rand2 = await getCachedRandom() const rand2 = await getCachedRandomWithTag('api')
return Response.json({ rand1, rand2 }) return Response.json({ rand1, rand2 })
} }
import { cacheLife, cacheTag } from 'next/cache'
export async function getCachedRandom() { export async function getCachedRandom() {
'use cache' 'use cache'
return Math.random() return Math.random()
} }
export async function getCachedRandomWithTag(tag) {
'use cache'
cacheTag(tag)
return Math.random()
}
export async function getCachedRandomWithCacheLife(n = 1) {
'use cache'
cacheLife('weeks')
return String(Math.ceil(Math.random() * n))
}
export async function getCachedRandomWithHandler() { export async function getCachedRandomWithHandler() {
'use cache: default' 'use cache: default'
return Math.random() return Math.random()
......
...@@ -474,6 +474,8 @@ describe('use-cache', () => { ...@@ -474,6 +474,8 @@ describe('use-cache', () => {
// [id] route, first entry in generateStaticParams // [id] route, first entry in generateStaticParams
expect.stringMatching(/\/a\d/), expect.stringMatching(/\/a\d/),
withCacheComponents && '/api', withCacheComponents && '/api',
// api/[id] route handler using generateStaticParams with 'use cache' from node_modules
expect.stringMatching(/\/api\/\d/),
// [id] route, second entry in generateStaticParams // [id] route, second entry in generateStaticParams
expect.stringMatching(/\/b\d/), expect.stringMatching(/\/b\d/),
'/cache-fetch', '/cache-fetch',
......
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