Skip to content
This repository was archived by the owner on May 10, 2021. It is now read-only.

Commit 8a6e08a

Browse files
committed
add support for background functions in api pages only
1 parent a73045f commit 8a6e08a

File tree

7 files changed

+26
-6
lines changed

7 files changed

+26
-6
lines changed

lib/helpers/getNetlifyFunctionName.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const path = require("path");
55
// The function name will be next_directory_subdirectory_[id]
66
// We do this because every function needs to be at the top-level, i.e.
77
// nested functions are not allowed.
8-
const getNetlifyFunctionName = (filePath) => {
8+
const getNetlifyFunctionName = (filePath, isApiPage) => {
99
// Remove pages/ from file path:
1010
// pages/directory/page.js > directory/page.js
1111
const relativeFilePath = path.relative("pages", filePath);
@@ -24,7 +24,16 @@ const getNetlifyFunctionName = (filePath) => {
2424
// Netlify Function names may not contain periods or square brackets.
2525
// To be safe, we keep only alphanumeric characters and underscores.
2626
// See: https://community.netlify.com/t/failed-to-upload-functions-file-function-too-large/3549/8
27-
functionName = functionName.replace(/[^\w\d]/g, "");
27+
const cleanNameRegex = new RegExp(/[^\w\d]/g);
28+
// Allow users to use background functions for /api pages *only*
29+
// Note: this means that there is a chance a Next on Netlify user could
30+
// unknowingly create a background function if they're not familiar with them
31+
// and their syntax
32+
const allowBackgroundRegex = new RegExp(/[^\w\d-]|-(?!background$)/g);
33+
functionName = functionName.replace(
34+
isApiPage ? allowBackgroundRegex : cleanNameRegex,
35+
""
36+
);
2837

2938
return functionName;
3039
};

lib/helpers/setupNetlifyFunctionForPage.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ const {
88
const getNetlifyFunctionName = require("./getNetlifyFunctionName");
99

1010
// Create a Netlify Function for the page with the given file path
11-
const setupNetlifyFunctionForPage = ({ filePath, functionsPath }) => {
11+
const setupNetlifyFunctionForPage = ({
12+
filePath,
13+
functionsPath,
14+
isApiPage,
15+
}) => {
1216
// Set function name based on file path
13-
const functionName = getNetlifyFunctionName(filePath);
17+
const functionName = getNetlifyFunctionName(filePath, isApiPage);
1418
const functionDirectory = join(functionsPath, functionName);
1519

1620
// Copy function template

lib/pages/api/redirects.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const pages = require("./pages");
33

44
const redirects = pages.map(({ route, filePath }) => ({
55
route,
6-
target: `/.netlify/functions/${getNetlifyFunctionName(filePath)}`,
6+
target: `/.netlify/functions/${getNetlifyFunctionName(filePath, true)}`,
77
}));
88

99
module.exports = redirects;

lib/pages/api/setup.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const setup = (functionsPath) => {
1212
// Create Netlify Function for every page
1313
pages.forEach(({ filePath }) => {
1414
logItem(filePath);
15-
setupNetlifyFunctionForPage({ filePath, functionsPath });
15+
setupNetlifyFunctionForPage({ filePath, functionsPath, isApiPage: true });
1616
});
1717
};
1818

tests/__snapshots__/defaults.test.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ exports[`Routing creates Netlify redirects 1`] = `
2424
/_next/data/%BUILD_ID%/getStaticProps/withFallbackBlocking/4.json /.netlify/functions/next_getStaticProps_withFallbackBlocking_id 200! Cookie=__prerender_bypass,__next_preview_data
2525
/_next/data/%BUILD_ID%/getStaticProps/withRevalidate/1.json /.netlify/functions/next_getStaticProps_withRevalidate_id 200
2626
/_next/data/%BUILD_ID%/getStaticProps/withRevalidate/2.json /.netlify/functions/next_getStaticProps_withRevalidate_id 200
27+
/api/hello-background /.netlify/functions/next_api_hello-background 200
2728
/api/static /.netlify/functions/next_api_static 200
2829
/getServerSideProps/static /.netlify/functions/next_getServerSideProps_static 200
2930
/getStaticProps/1 /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data

tests/__snapshots__/i18n.test.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ exports[`Routing creates Netlify redirects 1`] = `
2323
/_next/data/%BUILD_ID%/es/getStaticProps/static.json /.netlify/functions/next_getStaticProps_static 200! Cookie=__prerender_bypass,__next_preview_data
2424
/_next/data/%BUILD_ID%/es/getStaticProps/with-revalidate.json /.netlify/functions/next_getStaticProps_withrevalidate 200
2525
/_next/data/%BUILD_ID%/getServerSideProps/static.json /.netlify/functions/next_getServerSideProps_static 200
26+
/api/hello-background /.netlify/functions/next_api_hello-background 200
2627
/api/static /.netlify/functions/next_api_static 200
2728
/en /.netlify/functions/next_index 200
2829
/en/getServerSideProps/static /.netlify/functions/next_getServerSideProps_static 200
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default (req, res) => {
2+
res.setHeader("Content-Type", "application/json");
3+
res.status(200);
4+
res.json({ message: "hello world :)" });
5+
};

0 commit comments

Comments
 (0)