This repository was archived by the owner on May 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathgetNetlifyFunctionName.js
41 lines (34 loc) · 1.71 KB
/
getNetlifyFunctionName.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const path = require("path");
// Generate the Netlify Function name for the given file path.
// The file path will be pages/directory/subdirectory/[id].js
// The function name will be next_directory_subdirectory_[id]
// We do this because every function needs to be at the top-level, i.e.
// nested functions are not allowed.
const getNetlifyFunctionName = (filePath, isApiPage) => {
// Remove pages/ from file path:
// pages/directory/page.js > directory/page.js
const relativeFilePath = path.relative("pages", filePath);
// Extract directory path and file name without extension
const { dir: directoryPath, name } = path.parse(relativeFilePath);
// Combine next, directory path, and file name:
// next/directory/page
const filePathWithoutExtension = path.join("next", directoryPath, name);
// Replace slashes with underscores:
// next/directory/page > next_directory_page
let functionName = filePathWithoutExtension.split(path.sep).join("_");
// Netlify Function names may not contain periods or square brackets.
// To be safe, we keep only alphanumeric characters and underscores.
// See: https://community.netlify.com/t/failed-to-upload-functions-file-function-too-large/3549/8
const cleanNameRegex = new RegExp(/[^\w\d]/g);
// Allow users to use background functions for /api pages *only*
// Note: this means that there is a chance a Next on Netlify user could
// unknowingly create a background function if they're not familiar with them
// and their syntax
const allowBackgroundRegex = new RegExp(/[^\w\d-]|-(?!background$)/g);
functionName = functionName.replace(
isApiPage ? allowBackgroundRegex : cleanNameRegex,
""
);
return functionName;
};
module.exports = getNetlifyFunctionName;