@netlify/plugin-nextjs 4.0.0-beta.7 → 4.0.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/constants.js +17 -10
- package/lib/helpers/cache.js +11 -6
- package/lib/helpers/config.js +20 -77
- package/lib/helpers/files.js +232 -10
- package/lib/helpers/functions.js +35 -29
- package/lib/helpers/redirects.js +130 -0
- package/lib/helpers/utils.js +32 -0
- package/lib/helpers/verification.js +17 -17
- package/lib/index.js +18 -7
- package/lib/templates/getHandler.js +37 -93
- package/lib/templates/getPageResolver.js +20 -11
- package/lib/templates/handlerUtils.js +162 -0
- package/lib/templates/ipx.js +11 -7
- package/package.json +13 -10
- package/lib/.DS_Store +0 -0
package/lib/constants.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DIVIDER = exports.MINIMUM_REVALIDATE_SECONDS = exports.DYNAMIC_PARAMETER_REGEX = exports.OPTIONAL_CATCH_ALL_REGEX = exports.CATCH_ALL_REGEX = exports.DEFAULT_FUNCTIONS_SRC = exports.HANDLER_FUNCTION_PATH = exports.ODB_FUNCTION_PATH = exports.HIDDEN_PATHS = exports.IMAGE_FUNCTION_NAME = exports.ODB_FUNCTION_NAME = exports.HANDLER_FUNCTION_NAME = void 0;
|
|
4
|
+
exports.HANDLER_FUNCTION_NAME = '___netlify-handler';
|
|
5
|
+
exports.ODB_FUNCTION_NAME = '___netlify-odb-handler';
|
|
6
|
+
exports.IMAGE_FUNCTION_NAME = '_ipx';
|
|
4
7
|
// These are paths in .next that shouldn't be publicly accessible
|
|
5
|
-
|
|
8
|
+
exports.HIDDEN_PATHS = [
|
|
6
9
|
'/cache/*',
|
|
7
10
|
'/server/*',
|
|
8
11
|
'/serverless/*',
|
|
@@ -13,9 +16,13 @@ const HIDDEN_PATHS = [
|
|
|
13
16
|
'/react-loadable-manifest.json',
|
|
14
17
|
'/BUILD_ID',
|
|
15
18
|
];
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
exports.ODB_FUNCTION_PATH = `/.netlify/builders/${exports.ODB_FUNCTION_NAME}`;
|
|
20
|
+
exports.HANDLER_FUNCTION_PATH = `/.netlify/functions/${exports.HANDLER_FUNCTION_NAME}`;
|
|
21
|
+
exports.DEFAULT_FUNCTIONS_SRC = 'netlify/functions';
|
|
22
|
+
exports.CATCH_ALL_REGEX = /\/\[\.{3}(.*)](.json)?$/;
|
|
23
|
+
exports.OPTIONAL_CATCH_ALL_REGEX = /\/\[{2}\.{3}(.*)]{2}(.json)?$/;
|
|
24
|
+
exports.DYNAMIC_PARAMETER_REGEX = /\/\[(.*?)]/g;
|
|
25
|
+
exports.MINIMUM_REVALIDATE_SECONDS = 60;
|
|
26
|
+
exports.DIVIDER = `
|
|
27
|
+
────────────────────────────────────────────────────────────────
|
|
28
|
+
`;
|
package/lib/helpers/cache.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
exports
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.saveCache = exports.restoreCache = void 0;
|
|
4
|
+
const path_1 = require("path");
|
|
5
|
+
const restoreCache = async ({ cache, publish }) => {
|
|
6
|
+
const cacheDir = path_1.posix.join(publish, 'cache');
|
|
4
7
|
if (await cache.restore(cacheDir)) {
|
|
5
8
|
console.log('Next.js cache restored.');
|
|
6
9
|
}
|
|
@@ -8,9 +11,10 @@ exports.restoreCache = async ({ cache, publish }) => {
|
|
|
8
11
|
console.log('No Next.js cache to restore.');
|
|
9
12
|
}
|
|
10
13
|
};
|
|
11
|
-
exports.
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
+
exports.restoreCache = restoreCache;
|
|
15
|
+
const saveCache = async ({ cache, publish }) => {
|
|
16
|
+
const cacheDir = path_1.posix.join(publish, 'cache');
|
|
17
|
+
const buildManifest = path_1.posix.join(publish, 'build-manifest.json');
|
|
14
18
|
if (await cache.save(cacheDir, { digests: [buildManifest] })) {
|
|
15
19
|
console.log('Next.js cache saved.');
|
|
16
20
|
}
|
|
@@ -18,3 +22,4 @@ exports.saveCache = async ({ cache, publish }) => {
|
|
|
18
22
|
console.log('No Next.js cache to save.');
|
|
19
23
|
}
|
|
20
24
|
};
|
|
25
|
+
exports.saveCache = saveCache;
|
package/lib/helpers/config.js
CHANGED
|
@@ -1,81 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.configureHandlerFunctions = exports.getNextConfig = void 0;
|
|
7
|
+
const fs_extra_1 = require("fs-extra");
|
|
8
|
+
const pathe_1 = require("pathe");
|
|
9
|
+
const slash_1 = __importDefault(require("slash"));
|
|
10
|
+
const constants_1 = require("../constants");
|
|
5
11
|
const defaultFailBuild = (message, { error }) => {
|
|
6
12
|
throw new Error(`${message}\n${error && error.stack}`);
|
|
7
13
|
};
|
|
8
|
-
const {
|
|
9
|
-
const ODB_FUNCTION_PATH = `/.netlify/builders/${ODB_FUNCTION_NAME}`;
|
|
10
|
-
const HANDLER_FUNCTION_PATH = `/.netlify/functions/${HANDLER_FUNCTION_NAME}`;
|
|
11
|
-
const CATCH_ALL_REGEX = /\/\[\.{3}(.*)](.json)?$/;
|
|
12
|
-
const OPTIONAL_CATCH_ALL_REGEX = /\/\[{2}\.{3}(.*)]{2}(.json)?$/;
|
|
13
|
-
const DYNAMIC_PARAMETER_REGEX = /\/\[(.*?)]/g;
|
|
14
|
-
const getNetlifyRoutes = (nextRoute) => {
|
|
15
|
-
let netlifyRoutes = [nextRoute];
|
|
16
|
-
// If the route is an optional catch-all route, we need to add a second
|
|
17
|
-
// Netlify route for the base path (when no parameters are present).
|
|
18
|
-
// The file ending must be present!
|
|
19
|
-
if (OPTIONAL_CATCH_ALL_REGEX.test(nextRoute)) {
|
|
20
|
-
let netlifyRoute = nextRoute.replace(OPTIONAL_CATCH_ALL_REGEX, '$2');
|
|
21
|
-
// When optional catch-all route is at top-level, the regex on line 19 will
|
|
22
|
-
// create an empty string, but actually needs to be a forward slash
|
|
23
|
-
if (netlifyRoute === '')
|
|
24
|
-
netlifyRoute = '/';
|
|
25
|
-
// When optional catch-all route is at top-level, the regex on line 19 will
|
|
26
|
-
// create an incorrect route for the data route. For example, it creates
|
|
27
|
-
// /_next/data/%BUILDID%.json, but NextJS looks for
|
|
28
|
-
// /_next/data/%BUILDID%/index.json
|
|
29
|
-
netlifyRoute = netlifyRoute.replace(/(\/_next\/data\/[^/]+).json/, '$1/index.json');
|
|
30
|
-
// Add second route to the front of the array
|
|
31
|
-
netlifyRoutes.unshift(netlifyRoute);
|
|
32
|
-
}
|
|
33
|
-
// Replace catch-all, e.g., [...slug]
|
|
34
|
-
netlifyRoutes = netlifyRoutes.map((route) => route.replace(CATCH_ALL_REGEX, '/:$1/*'));
|
|
35
|
-
// Replace optional catch-all, e.g., [[...slug]]
|
|
36
|
-
netlifyRoutes = netlifyRoutes.map((route) => route.replace(OPTIONAL_CATCH_ALL_REGEX, '/*'));
|
|
37
|
-
// Replace dynamic parameters, e.g., [id]
|
|
38
|
-
netlifyRoutes = netlifyRoutes.map((route) => route.replace(DYNAMIC_PARAMETER_REGEX, '/:$1'));
|
|
39
|
-
return netlifyRoutes;
|
|
40
|
-
};
|
|
41
|
-
exports.generateRedirects = async ({ netlifyConfig, basePath, i18n }) => {
|
|
42
|
-
const { dynamicRoutes } = await readJSON(join(netlifyConfig.build.publish, 'prerender-manifest.json'));
|
|
43
|
-
const redirects = [];
|
|
44
|
-
netlifyConfig.redirects.push(...HIDDEN_PATHS.map((path) => ({
|
|
45
|
-
from: `${basePath}${path}`,
|
|
46
|
-
to: '/404.html',
|
|
47
|
-
status: 404,
|
|
48
|
-
force: true,
|
|
49
|
-
})));
|
|
50
|
-
const dynamicRouteEntries = Object.entries(dynamicRoutes);
|
|
51
|
-
dynamicRouteEntries.sort((a, b) => a[0].localeCompare(b[0]));
|
|
52
|
-
dynamicRouteEntries.forEach(([route, { dataRoute, fallback }]) => {
|
|
53
|
-
// Add redirects if fallback is "null" (aka blocking) or true/a string
|
|
54
|
-
if (fallback === false) {
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
redirects.push(...getNetlifyRoutes(route), ...getNetlifyRoutes(dataRoute));
|
|
58
|
-
});
|
|
59
|
-
if (i18n) {
|
|
60
|
-
netlifyConfig.redirects.push({ from: `${basePath}/:locale/_next/static/*`, to: `/static/:splat`, status: 200 });
|
|
61
|
-
}
|
|
62
|
-
// This is only used in prod, so dev uses `next dev` directly
|
|
63
|
-
netlifyConfig.redirects.push({ from: `${basePath}/_next/static/*`, to: `/static/:splat`, status: 200 }, {
|
|
64
|
-
from: `${basePath}/*`,
|
|
65
|
-
to: HANDLER_FUNCTION_PATH,
|
|
66
|
-
status: 200,
|
|
67
|
-
conditions: { Cookie: ['__prerender_bypass', '__next_preview_data'] },
|
|
68
|
-
force: true,
|
|
69
|
-
}, ...redirects.map((redirect) => ({
|
|
70
|
-
from: `${basePath}${redirect}`,
|
|
71
|
-
to: ODB_FUNCTION_PATH,
|
|
72
|
-
status: 200,
|
|
73
|
-
})), { from: `${basePath}/*`, to: HANDLER_FUNCTION_PATH, status: 200 });
|
|
74
|
-
};
|
|
75
|
-
exports.getNextConfig = async function getNextConfig({ publish, failBuild = defaultFailBuild }) {
|
|
14
|
+
const getNextConfig = async function getNextConfig({ publish, failBuild = defaultFailBuild, }) {
|
|
76
15
|
try {
|
|
77
|
-
const { config, appDir, ignore } = await readJSON(join(publish, 'required-server-files.json'));
|
|
16
|
+
const { config, appDir, ignore } = await fs_extra_1.readJSON(pathe_1.join(publish, 'required-server-files.json'));
|
|
78
17
|
if (!config) {
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
19
|
+
// @ts-ignore
|
|
79
20
|
return failBuild('Error loading your Next config');
|
|
80
21
|
}
|
|
81
22
|
return { ...config, appDir, ignore };
|
|
@@ -84,26 +25,27 @@ exports.getNextConfig = async function getNextConfig({ publish, failBuild = defa
|
|
|
84
25
|
return failBuild('Error loading your Next config', { error });
|
|
85
26
|
}
|
|
86
27
|
};
|
|
28
|
+
exports.getNextConfig = getNextConfig;
|
|
87
29
|
const resolveModuleRoot = (moduleName) => {
|
|
88
30
|
try {
|
|
89
|
-
return dirname(relative(process.cwd(), require.resolve(`${moduleName}/package.json`, { paths: [process.cwd()] })));
|
|
31
|
+
return pathe_1.dirname(pathe_1.relative(process.cwd(), require.resolve(`${moduleName}/package.json`, { paths: [process.cwd()] })));
|
|
90
32
|
}
|
|
91
33
|
catch (error) {
|
|
92
34
|
return null;
|
|
93
35
|
}
|
|
94
36
|
};
|
|
95
37
|
const DEFAULT_EXCLUDED_MODULES = ['sharp', 'electron'];
|
|
96
|
-
|
|
38
|
+
const configureHandlerFunctions = ({ netlifyConfig, publish, ignore = [] }) => {
|
|
97
39
|
var _a;
|
|
98
40
|
/* eslint-disable no-underscore-dangle */
|
|
99
41
|
(_a = netlifyConfig.functions)._ipx || (_a._ipx = {});
|
|
100
42
|
netlifyConfig.functions._ipx.node_bundler = 'nft';
|
|
101
|
-
[HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME].forEach((functionName) => {
|
|
43
|
+
[constants_1.HANDLER_FUNCTION_NAME, constants_1.ODB_FUNCTION_NAME].forEach((functionName) => {
|
|
102
44
|
var _a, _b;
|
|
103
45
|
(_a = netlifyConfig.functions)[functionName] || (_a[functionName] = { included_files: [], external_node_modules: [] });
|
|
104
46
|
netlifyConfig.functions[functionName].node_bundler = 'nft';
|
|
105
47
|
(_b = netlifyConfig.functions[functionName]).included_files || (_b.included_files = []);
|
|
106
|
-
netlifyConfig.functions[functionName].included_files.push(`${publish}/server/**`, `${publish}/serverless/**`, `${publish}/*.json`, `${publish}/BUILD_ID`, `${publish}/static/chunks/webpack-middleware*.js`, `!${publish}/server/**/*.js.nft.json`, ...ignore.map((path) => `!${
|
|
48
|
+
netlifyConfig.functions[functionName].included_files.push('.env', '.env.local', '.env.production', '.env.production.local', `${publish}/server/**`, `${publish}/serverless/**`, `${publish}/*.json`, `${publish}/BUILD_ID`, `${publish}/static/chunks/webpack-middleware*.js`, `!${publish}/server/**/*.js.nft.json`, ...ignore.map((path) => `!${slash_1.default(path)}`));
|
|
107
49
|
const nextRoot = resolveModuleRoot('next');
|
|
108
50
|
if (nextRoot) {
|
|
109
51
|
netlifyConfig.functions[functionName].included_files.push(`!${nextRoot}/dist/server/lib/squoosh/**/*.wasm`, `!${nextRoot}/dist/next-server/server/lib/squoosh/**/*.wasm`, `!${nextRoot}/dist/compiled/webpack/bundle4.js`, `!${nextRoot}/dist/compiled/webpack/bundle5.js`, `!${nextRoot}/dist/compiled/terser/bundle.min.js`);
|
|
@@ -116,3 +58,4 @@ exports.configureHandlerFunctions = ({ netlifyConfig, publish, ignore = [] }) =>
|
|
|
116
58
|
});
|
|
117
59
|
});
|
|
118
60
|
};
|
|
61
|
+
exports.configureHandlerFunctions = configureHandlerFunctions;
|
package/lib/helpers/files.js
CHANGED
|
@@ -1,51 +1,273 @@
|
|
|
1
|
-
|
|
1
|
+
/* eslint-disable max-lines */
|
|
2
2
|
const { cpus } = require('os');
|
|
3
|
-
const {
|
|
3
|
+
const { yellowBright } = require('chalk');
|
|
4
|
+
const { existsSync, readJson, move, copy, writeJson, readFile, writeFile, ensureDir, readFileSync, } = require('fs-extra');
|
|
4
5
|
const globby = require('globby');
|
|
6
|
+
const { outdent } = require('outdent');
|
|
5
7
|
const pLimit = require('p-limit');
|
|
6
8
|
const { join } = require('pathe');
|
|
7
9
|
const slash = require('slash');
|
|
10
|
+
const { MINIMUM_REVALIDATE_SECONDS, DIVIDER } = require('../constants');
|
|
8
11
|
const TEST_ROUTE = /(|\/)\[[^/]+?](\/|\.html|$)/;
|
|
9
12
|
const isDynamicRoute = (route) => TEST_ROUTE.test(route);
|
|
10
|
-
|
|
13
|
+
const stripLocale = (rawPath, locales = []) => {
|
|
14
|
+
const [locale, ...segments] = rawPath.split('/');
|
|
15
|
+
if (locales.includes(locale)) {
|
|
16
|
+
return segments.join('/');
|
|
17
|
+
}
|
|
18
|
+
return rawPath;
|
|
19
|
+
};
|
|
20
|
+
const matchMiddleware = (middleware, filePath) => (middleware === null || middleware === void 0 ? void 0 : middleware.includes('')) ||
|
|
21
|
+
(middleware === null || middleware === void 0 ? void 0 : middleware.find((middlewarePath) => filePath === middlewarePath || filePath === `${middlewarePath}.html` || filePath.startsWith(`${middlewarePath}/`)));
|
|
22
|
+
const matchesRedirect = (file, redirects) => {
|
|
23
|
+
if (!Array.isArray(redirects)) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
return redirects.some((redirect) => {
|
|
27
|
+
if (!redirect.regex || redirect.internal) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
// Strips the extension from the file path
|
|
31
|
+
return new RegExp(redirect.regex).test(`/${file.slice(0, -5)}`);
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
const matchesRewrite = (file, rewrites) => {
|
|
35
|
+
if (Array.isArray(rewrites)) {
|
|
36
|
+
return matchesRedirect(file, rewrites);
|
|
37
|
+
}
|
|
38
|
+
if (!Array.isArray(rewrites === null || rewrites === void 0 ? void 0 : rewrites.beforeFiles)) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
return matchesRedirect(file, rewrites.beforeFiles);
|
|
42
|
+
};
|
|
43
|
+
exports.matchesRedirect = matchesRedirect;
|
|
44
|
+
exports.matchesRewrite = matchesRewrite;
|
|
45
|
+
exports.matchMiddleware = matchMiddleware;
|
|
46
|
+
exports.stripLocale = stripLocale;
|
|
47
|
+
exports.isDynamicRoute = isDynamicRoute;
|
|
48
|
+
// eslint-disable-next-line max-lines-per-function
|
|
49
|
+
exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
|
|
11
50
|
console.log('Moving static page files to serve from CDN...');
|
|
12
|
-
const
|
|
51
|
+
const outputDir = join(netlifyConfig.build.publish, target === 'server' ? 'server' : 'serverless');
|
|
52
|
+
const root = join(outputDir, 'pages');
|
|
53
|
+
const buildId = readFileSync(join(netlifyConfig.build.publish, 'BUILD_ID'), 'utf8').trim();
|
|
54
|
+
const dataDir = join('_next', 'data', buildId);
|
|
55
|
+
await ensureDir(dataDir);
|
|
56
|
+
// Load the middleware manifest so we can check if a file matches it before moving
|
|
57
|
+
let middleware;
|
|
58
|
+
const manifestPath = join(outputDir, 'middleware-manifest.json');
|
|
59
|
+
if (existsSync(manifestPath)) {
|
|
60
|
+
const manifest = await readJson(manifestPath);
|
|
61
|
+
if (manifest === null || manifest === void 0 ? void 0 : manifest.middleware) {
|
|
62
|
+
middleware = Object.keys(manifest.middleware).map((path) => path.slice(1));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const prerenderManifest = await readJson(join(netlifyConfig.build.publish, 'prerender-manifest.json'));
|
|
66
|
+
const { redirects, rewrites } = await readJson(join(netlifyConfig.build.publish, 'routes-manifest.json'));
|
|
67
|
+
const isrFiles = new Set();
|
|
68
|
+
const shortRevalidateRoutes = [];
|
|
69
|
+
Object.entries(prerenderManifest.routes).forEach(([route, { initialRevalidateSeconds }]) => {
|
|
70
|
+
if (initialRevalidateSeconds) {
|
|
71
|
+
// Find all files used by ISR routes
|
|
72
|
+
const trimmedPath = route.slice(1);
|
|
73
|
+
isrFiles.add(`${trimmedPath}.html`);
|
|
74
|
+
isrFiles.add(`${trimmedPath}.json`);
|
|
75
|
+
if (initialRevalidateSeconds < MINIMUM_REVALIDATE_SECONDS) {
|
|
76
|
+
shortRevalidateRoutes.push({ Route: route, Revalidate: initialRevalidateSeconds });
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
});
|
|
13
80
|
const files = [];
|
|
81
|
+
const filesManifest = {};
|
|
14
82
|
const moveFile = async (file) => {
|
|
83
|
+
const isData = file.endsWith('.json');
|
|
15
84
|
const source = join(root, file);
|
|
85
|
+
const targetFile = isData ? join(dataDir, file) : file;
|
|
16
86
|
files.push(file);
|
|
17
|
-
|
|
18
|
-
|
|
87
|
+
filesManifest[file] = targetFile;
|
|
88
|
+
const dest = join(netlifyConfig.build.publish, targetFile);
|
|
89
|
+
try {
|
|
90
|
+
await move(source, dest);
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
console.warn('Error moving file', source, error);
|
|
94
|
+
}
|
|
19
95
|
};
|
|
20
96
|
// Move all static files, except error documents and nft manifests
|
|
21
97
|
const pages = await globby(['**/*.{html,json}', '!**/(500|404|*.js.nft).{html,json}'], {
|
|
22
98
|
cwd: root,
|
|
23
99
|
dot: true,
|
|
24
100
|
});
|
|
101
|
+
const matchingMiddleware = new Set();
|
|
102
|
+
const matchedPages = new Set();
|
|
103
|
+
const matchedRedirects = new Set();
|
|
104
|
+
const matchedRewrites = new Set();
|
|
25
105
|
// Limit concurrent file moves to number of cpus or 2 if there is only 1
|
|
26
106
|
const limit = pLimit(Math.max(2, cpus().length));
|
|
27
|
-
const promises = pages.map(
|
|
107
|
+
const promises = pages.map((rawPath) => {
|
|
28
108
|
const filePath = slash(rawPath);
|
|
109
|
+
// Don't move ISR files, as they're used for the first request
|
|
110
|
+
if (isrFiles.has(filePath)) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
29
113
|
if (isDynamicRoute(filePath)) {
|
|
30
114
|
return;
|
|
31
115
|
}
|
|
116
|
+
if (matchesRedirect(filePath, redirects)) {
|
|
117
|
+
matchedRedirects.add(filePath);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (matchesRewrite(filePath, rewrites)) {
|
|
121
|
+
matchedRewrites.add(filePath);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
// Middleware matches against the unlocalised path
|
|
125
|
+
const unlocalizedPath = stripLocale(rawPath, i18n === null || i18n === void 0 ? void 0 : i18n.locales);
|
|
126
|
+
const middlewarePath = matchMiddleware(middleware, unlocalizedPath);
|
|
127
|
+
// If a file matches middleware it can't be offloaded to the CDN, and needs to stay at the origin to be served by next/server
|
|
128
|
+
if (middlewarePath) {
|
|
129
|
+
matchingMiddleware.add(middlewarePath);
|
|
130
|
+
matchedPages.add(rawPath);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
32
133
|
return limit(moveFile, filePath);
|
|
33
134
|
});
|
|
34
135
|
await Promise.all(promises);
|
|
35
136
|
console.log(`Moved ${files.length} files`);
|
|
137
|
+
if (matchedPages.size !== 0) {
|
|
138
|
+
console.log(yellowBright(outdent `
|
|
139
|
+
Skipped moving ${matchedPages.size} ${matchedPages.size === 1 ? 'file because it matches' : 'files because they match'} middleware, so cannot be deployed to the CDN and will be served from the origin instead.
|
|
140
|
+
This is fine, but we're letting you know because it may not be what you expect.
|
|
141
|
+
`));
|
|
142
|
+
console.log(outdent `
|
|
143
|
+
The following middleware matched statically-rendered pages:
|
|
144
|
+
|
|
145
|
+
${yellowBright([...matchingMiddleware].map((mid) => `- /${mid}/_middleware`).join('\n'))}
|
|
146
|
+
${DIVIDER}
|
|
147
|
+
`);
|
|
148
|
+
// There could potentially be thousands of matching pages, so we don't want to spam the console with this
|
|
149
|
+
if (matchedPages.size < 50) {
|
|
150
|
+
console.log(outdent `
|
|
151
|
+
The following files matched middleware and were not moved to the CDN:
|
|
152
|
+
|
|
153
|
+
${yellowBright([...matchedPages].map((mid) => `- ${mid}`).join('\n'))}
|
|
154
|
+
${DIVIDER}
|
|
155
|
+
`);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
if (matchedRedirects.size !== 0 || matchedRewrites.size !== 0) {
|
|
159
|
+
console.log(yellowBright(outdent `
|
|
160
|
+
Skipped moving ${matchedRedirects.size + matchedRewrites.size} files because they match redirects or beforeFiles rewrites, so cannot be deployed to the CDN and will be served from the origin instead.
|
|
161
|
+
`));
|
|
162
|
+
if (matchedRedirects.size < 50 && matchedRedirects.size !== 0) {
|
|
163
|
+
console.log(outdent `
|
|
164
|
+
The following files matched redirects and were not moved to the CDN:
|
|
165
|
+
|
|
166
|
+
${yellowBright([...matchedRedirects].map((mid) => `- ${mid}`).join('\n'))}
|
|
167
|
+
${DIVIDER}
|
|
168
|
+
`);
|
|
169
|
+
}
|
|
170
|
+
if (matchedRewrites.size < 50 && matchedRewrites.size !== 0) {
|
|
171
|
+
console.log(outdent `
|
|
172
|
+
The following files matched beforeFiles rewrites and were not moved to the CDN:
|
|
173
|
+
|
|
174
|
+
${yellowBright([...matchedRewrites].map((mid) => `- ${mid}`).join('\n'))}
|
|
175
|
+
${DIVIDER}
|
|
176
|
+
`);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
36
179
|
// Write the manifest for use in the serverless functions
|
|
37
|
-
await writeJson(join(netlifyConfig.build.publish, 'static-manifest.json'),
|
|
180
|
+
await writeJson(join(netlifyConfig.build.publish, 'static-manifest.json'), Object.entries(filesManifest));
|
|
38
181
|
if (i18n === null || i18n === void 0 ? void 0 : i18n.defaultLocale) {
|
|
39
182
|
// Copy the default locale into the root
|
|
40
183
|
const defaultLocaleDir = join(netlifyConfig.build.publish, i18n.defaultLocale);
|
|
41
184
|
if (existsSync(defaultLocaleDir)) {
|
|
42
185
|
await copy(defaultLocaleDir, `${netlifyConfig.build.publish}/`);
|
|
43
186
|
}
|
|
187
|
+
const defaultLocaleIndex = join(netlifyConfig.build.publish, `${i18n.defaultLocale}.html`);
|
|
188
|
+
const indexHtml = join(netlifyConfig.build.publish, 'index.html');
|
|
189
|
+
if (existsSync(defaultLocaleIndex) && !existsSync(indexHtml)) {
|
|
190
|
+
try {
|
|
191
|
+
await copy(defaultLocaleIndex, indexHtml, { overwrite: false });
|
|
192
|
+
await copy(join(netlifyConfig.build.publish, `${i18n.defaultLocale}.json`), join(netlifyConfig.build.publish, 'index.json'), { overwrite: false });
|
|
193
|
+
}
|
|
194
|
+
catch { }
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (shortRevalidateRoutes.length !== 0) {
|
|
198
|
+
console.log(outdent `
|
|
199
|
+
The following routes use "revalidate" values of under ${MINIMUM_REVALIDATE_SECONDS} seconds, which is not supported.
|
|
200
|
+
They will use a revalidate time of ${MINIMUM_REVALIDATE_SECONDS} seconds instead.
|
|
201
|
+
`);
|
|
202
|
+
console.table(shortRevalidateRoutes);
|
|
203
|
+
// TODO: add these docs
|
|
204
|
+
// console.log(
|
|
205
|
+
// outdent`
|
|
206
|
+
// For more information, see https://ntl.fyi/next-revalidate-time
|
|
207
|
+
// ${DIVIDER}
|
|
208
|
+
// `,
|
|
209
|
+
// )
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
const patchFile = async ({ file, from, to }) => {
|
|
213
|
+
if (!existsSync(file)) {
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
const content = await readFile(file, 'utf8');
|
|
217
|
+
if (content.includes(to)) {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
const newContent = content.replace(from, to);
|
|
221
|
+
await writeFile(`${file}.orig`, content);
|
|
222
|
+
await writeFile(file, newContent);
|
|
223
|
+
};
|
|
224
|
+
const getServerFile = (root) => {
|
|
225
|
+
let serverFile;
|
|
226
|
+
try {
|
|
227
|
+
serverFile = require.resolve('next/dist/server/next-server', { paths: [root] });
|
|
228
|
+
}
|
|
229
|
+
catch {
|
|
230
|
+
// Ignore
|
|
231
|
+
}
|
|
232
|
+
if (!serverFile) {
|
|
233
|
+
try {
|
|
234
|
+
// eslint-disable-next-line node/no-missing-require
|
|
235
|
+
serverFile = require.resolve('next/dist/next-server/server/next-server', { paths: [root] });
|
|
236
|
+
}
|
|
237
|
+
catch {
|
|
238
|
+
// Ignore
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return serverFile;
|
|
242
|
+
};
|
|
243
|
+
exports.patchNextFiles = async (root) => {
|
|
244
|
+
const serverFile = getServerFile(root);
|
|
245
|
+
console.log(`Patching ${serverFile}`);
|
|
246
|
+
if (serverFile) {
|
|
247
|
+
await patchFile({
|
|
248
|
+
file: serverFile,
|
|
249
|
+
from: `let ssgCacheKey = `,
|
|
250
|
+
to: `let ssgCacheKey = process.env._BYPASS_SSG || `,
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
exports.unpatchNextFiles = async (root) => {
|
|
255
|
+
const serverFile = getServerFile(root);
|
|
256
|
+
const origFile = `${serverFile}.orig`;
|
|
257
|
+
if (existsSync(origFile)) {
|
|
258
|
+
await move(origFile, serverFile, { overwrite: true });
|
|
44
259
|
}
|
|
45
260
|
};
|
|
46
|
-
exports.movePublicFiles = async ({ appDir, publish }) => {
|
|
47
|
-
|
|
261
|
+
exports.movePublicFiles = async ({ appDir, outdir, publish }) => {
|
|
262
|
+
// `outdir` is a config property added when using Next.js with Nx. It's typically
|
|
263
|
+
// a relative path outside of the appDir, e.g. '../../dist/apps/<app-name>', and
|
|
264
|
+
// the parent directory of the .next directory.
|
|
265
|
+
// If it exists, copy the files from the public folder there in order to include
|
|
266
|
+
// any files that were generated during the build. Otherwise, copy the public
|
|
267
|
+
// directory from the original app directory.
|
|
268
|
+
const publicDir = outdir ? join(appDir, outdir, 'public') : join(appDir, 'public');
|
|
48
269
|
if (existsSync(publicDir)) {
|
|
49
270
|
await copy(publicDir, `${publish}/`);
|
|
50
271
|
}
|
|
51
272
|
};
|
|
273
|
+
/* eslint-enable max-lines */
|
package/lib/helpers/functions.js
CHANGED
|
@@ -1,56 +1,61 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setupImageFunction = exports.generatePagesResolver = exports.generateFunctions = void 0;
|
|
4
|
+
const fs_extra_1 = require("fs-extra");
|
|
5
|
+
const pathe_1 = require("pathe");
|
|
6
|
+
const constants_1 = require("../constants");
|
|
7
|
+
const getHandler_1 = require("../templates/getHandler");
|
|
8
|
+
const getPageResolver_1 = require("../templates/getPageResolver");
|
|
9
|
+
const generateFunctions = async ({ FUNCTIONS_SRC = constants_1.DEFAULT_FUNCTIONS_SRC, INTERNAL_FUNCTIONS_SRC, PUBLISH_DIR }, appDir) => {
|
|
8
10
|
const functionsDir = INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC;
|
|
9
11
|
const bridgeFile = require.resolve('@vercel/node/dist/bridge');
|
|
10
|
-
const functionDir = join(process.cwd(), functionsDir, HANDLER_FUNCTION_NAME);
|
|
11
|
-
const publishDir = relative(functionDir, join(process.cwd(), PUBLISH_DIR));
|
|
12
|
+
const functionDir = pathe_1.join(process.cwd(), functionsDir, constants_1.HANDLER_FUNCTION_NAME);
|
|
13
|
+
const publishDir = pathe_1.relative(functionDir, pathe_1.join(process.cwd(), PUBLISH_DIR));
|
|
12
14
|
const writeHandler = async (func, isODB) => {
|
|
13
|
-
const handlerSource = await getHandler({ isODB, publishDir, appDir: relative(functionDir, appDir) });
|
|
14
|
-
await ensureDir(join(functionsDir, func));
|
|
15
|
-
await writeFile(join(functionsDir, func, `${func}.js`), handlerSource);
|
|
16
|
-
await copyFile(bridgeFile, join(functionsDir, func, 'bridge.js'));
|
|
15
|
+
const handlerSource = await getHandler_1.getHandler({ isODB, publishDir, appDir: pathe_1.relative(functionDir, appDir) });
|
|
16
|
+
await fs_extra_1.ensureDir(pathe_1.join(functionsDir, func));
|
|
17
|
+
await fs_extra_1.writeFile(pathe_1.join(functionsDir, func, `${func}.js`), handlerSource);
|
|
18
|
+
await fs_extra_1.copyFile(bridgeFile, pathe_1.join(functionsDir, func, 'bridge.js'));
|
|
19
|
+
await fs_extra_1.copyFile(pathe_1.join(__dirname, '..', '..', 'lib', 'templates', 'handlerUtils.js'), pathe_1.join(functionsDir, func, 'handlerUtils.js'));
|
|
17
20
|
};
|
|
18
|
-
await writeHandler(HANDLER_FUNCTION_NAME, false);
|
|
19
|
-
await writeHandler(ODB_FUNCTION_NAME, true);
|
|
21
|
+
await writeHandler(constants_1.HANDLER_FUNCTION_NAME, false);
|
|
22
|
+
await writeHandler(constants_1.ODB_FUNCTION_NAME, true);
|
|
20
23
|
};
|
|
24
|
+
exports.generateFunctions = generateFunctions;
|
|
21
25
|
/**
|
|
22
26
|
* Writes a file in each function directory that contains references to every page entrypoint.
|
|
23
27
|
* This is just so that the nft bundler knows about them. We'll eventually do this better.
|
|
24
28
|
*/
|
|
25
|
-
|
|
29
|
+
const generatePagesResolver = async ({ constants: { INTERNAL_FUNCTIONS_SRC, FUNCTIONS_SRC = constants_1.DEFAULT_FUNCTIONS_SRC }, netlifyConfig, target, }) => {
|
|
26
30
|
const functionsPath = INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC;
|
|
27
|
-
const jsSource = await getPageResolver({
|
|
31
|
+
const jsSource = await getPageResolver_1.getPageResolver({
|
|
28
32
|
netlifyConfig,
|
|
29
33
|
target,
|
|
30
34
|
});
|
|
31
|
-
await writeFile(join(functionsPath, ODB_FUNCTION_NAME, 'pages.js'), jsSource);
|
|
32
|
-
await writeFile(join(functionsPath, HANDLER_FUNCTION_NAME, 'pages.js'), jsSource);
|
|
35
|
+
await fs_extra_1.writeFile(pathe_1.join(functionsPath, constants_1.ODB_FUNCTION_NAME, 'pages.js'), jsSource);
|
|
36
|
+
await fs_extra_1.writeFile(pathe_1.join(functionsPath, constants_1.HANDLER_FUNCTION_NAME, 'pages.js'), jsSource);
|
|
33
37
|
};
|
|
38
|
+
exports.generatePagesResolver = generatePagesResolver;
|
|
34
39
|
// Move our next/image function into the correct functions directory
|
|
35
|
-
|
|
40
|
+
const setupImageFunction = async ({ constants: { INTERNAL_FUNCTIONS_SRC, FUNCTIONS_SRC = constants_1.DEFAULT_FUNCTIONS_SRC }, imageconfig = {}, netlifyConfig, basePath, }) => {
|
|
36
41
|
const functionsPath = INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC;
|
|
37
|
-
const functionName = `${IMAGE_FUNCTION_NAME}.js`;
|
|
38
|
-
const functionDirectory = join(functionsPath, IMAGE_FUNCTION_NAME);
|
|
39
|
-
await ensureDir(functionDirectory);
|
|
40
|
-
await writeJSON(join(functionDirectory, 'imageconfig.json'), {
|
|
42
|
+
const functionName = `${constants_1.IMAGE_FUNCTION_NAME}.js`;
|
|
43
|
+
const functionDirectory = pathe_1.join(functionsPath, constants_1.IMAGE_FUNCTION_NAME);
|
|
44
|
+
await fs_extra_1.ensureDir(functionDirectory);
|
|
45
|
+
await fs_extra_1.writeJSON(pathe_1.join(functionDirectory, 'imageconfig.json'), {
|
|
41
46
|
...imageconfig,
|
|
42
|
-
basePath: [basePath, IMAGE_FUNCTION_NAME].join('/'),
|
|
47
|
+
basePath: [basePath, constants_1.IMAGE_FUNCTION_NAME].join('/'),
|
|
43
48
|
});
|
|
44
|
-
await copyFile(join(__dirname, '..', 'templates', 'ipx.js'), join(functionDirectory, functionName));
|
|
49
|
+
await fs_extra_1.copyFile(pathe_1.join(__dirname, '..', '..', 'lib', 'templates', 'ipx.js'), pathe_1.join(functionDirectory, functionName));
|
|
45
50
|
const imagePath = imageconfig.path || '/_next/image';
|
|
46
51
|
netlifyConfig.redirects.push({
|
|
47
52
|
from: `${imagePath}*`,
|
|
48
53
|
query: { url: ':url', w: ':width', q: ':quality' },
|
|
49
|
-
to: `${basePath}/${IMAGE_FUNCTION_NAME}/w_:width,q_:quality/:url`,
|
|
54
|
+
to: `${basePath}/${constants_1.IMAGE_FUNCTION_NAME}/w_:width,q_:quality/:url`,
|
|
50
55
|
status: 301,
|
|
51
56
|
}, {
|
|
52
|
-
from: `${basePath}/${IMAGE_FUNCTION_NAME}/*`,
|
|
53
|
-
to: `/.netlify/builders/${IMAGE_FUNCTION_NAME}`,
|
|
57
|
+
from: `${basePath}/${constants_1.IMAGE_FUNCTION_NAME}/*`,
|
|
58
|
+
to: `/.netlify/builders/${constants_1.IMAGE_FUNCTION_NAME}`,
|
|
54
59
|
status: 200,
|
|
55
60
|
});
|
|
56
61
|
if (basePath) {
|
|
@@ -62,3 +67,4 @@ exports.setupImageFunction = async ({ constants: { INTERNAL_FUNCTIONS_SRC, FUNCT
|
|
|
62
67
|
});
|
|
63
68
|
}
|
|
64
69
|
};
|
|
70
|
+
exports.setupImageFunction = setupImageFunction;
|