Skip to content

Commit 418c46d

Browse files
committed
feat: split up API Routes
Brings back the functionality that was reverted in #1731, but under a flag. This will be utterly slow in building, so let's try to speed that up in the next step!
1 parent a37fb72 commit 418c46d

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

packages/runtime/src/index.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
generatePagesResolver,
2525
getExtendedApiRouteConfigs,
2626
warnOnApiRoutes,
27+
getApiRouteConfigs,
2728
} from './helpers/functions'
2829
import { generateRedirects, generateStaticRedirects } from './helpers/redirects'
2930
import { shouldSkip, isNextAuthInstalled, getCustomImageResponseHeaders, getRemotePatterns } from './helpers/utils'
@@ -150,7 +151,11 @@ const plugin: NetlifyPlugin = {
150151
const buildId = readFileSync(join(publish, 'BUILD_ID'), 'utf8').trim()
151152

152153
await configureHandlerFunctions({ netlifyConfig, ignore, publish: relative(process.cwd(), publish) })
153-
const apiRoutes = await getExtendedApiRouteConfigs(publish, appDir)
154+
155+
const useNoneBundler = Boolean(process.env.NEXT_SPLIT_API_ROUTES)
156+
const apiRoutes = useNoneBundler
157+
? await getApiRouteConfigs(publish, appDir)
158+
: await getExtendedApiRouteConfigs(publish, appDir)
154159

155160
await generateFunctions(constants, appDir, apiRoutes)
156161
await generatePagesResolver(constants)

test/helpers/functions.spec.ts

+60-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,65 @@
1-
import { getExtendedApiRouteConfigs } from "../../packages/runtime/src/helpers/functions"
2-
import { describeCwdTmpDir, moveNextDist } from "../test-utils"
1+
import { getApiRouteConfigs, getExtendedApiRouteConfigs } from '../../packages/runtime/src/helpers/functions'
2+
import { describeCwdTmpDir, moveNextDist } from '../test-utils'
33

44
describeCwdTmpDir('api route file analysis', () => {
55
it('extracts correct route configs from source files', async () => {
6+
await moveNextDist()
7+
const configs = await getApiRouteConfigs('.next', process.cwd())
8+
// Using a Set means the order doesn't matter
9+
expect(new Set(configs)).toEqual(
10+
new Set([
11+
{
12+
compiled: 'pages/api/og.js',
13+
config: {
14+
runtime: 'edge',
15+
},
16+
route: '/api/og',
17+
},
18+
{
19+
compiled: 'pages/api/enterPreview.js',
20+
config: {},
21+
route: '/api/enterPreview',
22+
},
23+
{
24+
compiled: 'pages/api/exitPreview.js',
25+
config: {},
26+
route: '/api/exitPreview',
27+
},
28+
{
29+
compiled: 'pages/api/hello.js',
30+
config: {},
31+
route: '/api/hello',
32+
},
33+
{
34+
compiled: 'pages/api/shows/[...params].js',
35+
config: {},
36+
route: '/api/shows/[...params]',
37+
},
38+
{
39+
compiled: 'pages/api/shows/[id].js',
40+
config: {},
41+
route: '/api/shows/[id]',
42+
},
43+
{
44+
compiled: 'pages/api/hello-background.js',
45+
config: { type: 'experimental-background' },
46+
route: '/api/hello-background',
47+
},
48+
{
49+
compiled: 'pages/api/hello-scheduled.js',
50+
config: { schedule: '@hourly', type: 'experimental-scheduled' },
51+
route: '/api/hello-scheduled',
52+
},
53+
{
54+
compiled: 'pages/api/revalidate.js',
55+
config: {},
56+
route: '/api/revalidate',
57+
},
58+
]),
59+
)
60+
})
61+
62+
it('only shows scheduled/background functions as extended funcs', async () => {
663
await moveNextDist()
764
const configs = await getExtendedApiRouteConfigs('.next', process.cwd())
865
// Using a Set means the order doesn't matter
@@ -21,4 +78,4 @@ describeCwdTmpDir('api route file analysis', () => {
2178
]),
2279
)
2380
})
24-
})
81+
})

0 commit comments

Comments
 (0)