Skip to content

Commit 1050817

Browse files
committed
fix: now only custom rewrites are created for background and scheduled functions
1 parent 9ec8688 commit 1050817

File tree

2 files changed

+19
-46
lines changed

2 files changed

+19
-46
lines changed

packages/runtime/src/helpers/utils.ts

+19-16
Original file line numberDiff line numberDiff line change
@@ -145,22 +145,25 @@ export const redirectsForNextRouteWithData = ({
145145
}))
146146

147147
export const getApiRewrites = (basePath: string, apiRoutes: Array<ApiRouteConfig>) => {
148-
const apiRewrites = apiRoutes.map((apiRoute) => {
149-
const [from] = toNetlifyRoute(`${basePath}${apiRoute.route}`)
150-
151-
// Scheduled functions can't be invoked directly, so we 404 them.
152-
if (apiRoute.config.type === ApiRouteType.SCHEDULED) {
153-
return { from, to: '/404.html', status: 404 }
154-
}
155-
return {
156-
from,
157-
to: `/.netlify/functions/${getFunctionNameForPage(
158-
apiRoute.route,
159-
apiRoute.config.type === ApiRouteType.BACKGROUND,
160-
)}`,
161-
status: 200,
162-
}
163-
})
148+
const apiRewrites = apiRoutes
149+
// Only create custom rewrites for API routes that are background or scheduled functions.
150+
.filter((apiRoute) => apiRoute.config.type !== undefined)
151+
.map((apiRoute) => {
152+
const [from] = toNetlifyRoute(`${basePath}${apiRoute.route}`)
153+
154+
// Scheduled functions can't be invoked directly, so we 404 them.
155+
if (apiRoute.config.type === ApiRouteType.SCHEDULED) {
156+
return { from, to: '/404.html', status: 404 }
157+
}
158+
return {
159+
from,
160+
to: `/.netlify/functions/${getFunctionNameForPage(
161+
apiRoute.route,
162+
apiRoute.config.type === ApiRouteType.BACKGROUND,
163+
)}`,
164+
status: 200,
165+
}
166+
})
164167

165168
return [
166169
...apiRewrites,

test/__snapshots__/index.js.snap

-30
Original file line numberDiff line numberDiff line change
@@ -1095,21 +1095,6 @@ Array [
10951095
"status": 200,
10961096
"to": "/.netlify/functions/___netlify-handler",
10971097
},
1098-
Object {
1099-
"from": "/api/enterPreview",
1100-
"status": 200,
1101-
"to": "/.netlify/functions/_api_enterPreview-handler",
1102-
},
1103-
Object {
1104-
"from": "/api/exitPreview",
1105-
"status": 200,
1106-
"to": "/.netlify/functions/_api_exitPreview-handler",
1107-
},
1108-
Object {
1109-
"from": "/api/hello",
1110-
"status": 200,
1111-
"to": "/.netlify/functions/_api_hello-handler",
1112-
},
11131098
Object {
11141099
"from": "/api/hello-background",
11151100
"status": 200,
@@ -1120,21 +1105,6 @@ Array [
11201105
"status": 404,
11211106
"to": "/404.html",
11221107
},
1123-
Object {
1124-
"from": "/api/og",
1125-
"status": 200,
1126-
"to": "/.netlify/functions/_api_og-handler",
1127-
},
1128-
Object {
1129-
"from": "/api/shows/:id",
1130-
"status": 200,
1131-
"to": "/.netlify/functions/_api_shows_id-PARAM-handler",
1132-
},
1133-
Object {
1134-
"from": "/api/shows/:params/*",
1135-
"status": 200,
1136-
"to": "/.netlify/functions/_api_shows_params-SPLAT-handler",
1137-
},
11381108
Object {
11391109
"force": false,
11401110
"from": "/broken-image",

0 commit comments

Comments
 (0)