@netlify/plugin-nextjs 4.2.3 → 4.2.7
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/helpers/cache.js +26 -5
- package/lib/helpers/files.js +44 -33
- package/lib/helpers/utils.js +19 -1
- package/lib/index.js +1 -1
- package/package.json +9 -5
package/lib/helpers/cache.js
CHANGED
|
@@ -1,10 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.saveCache = exports.restoreCache = void 0;
|
|
4
|
+
const fs_1 = require("fs");
|
|
4
5
|
const path_1 = require("path");
|
|
6
|
+
const utils_1 = require("./utils");
|
|
7
|
+
const findDistDir = (publish) => {
|
|
8
|
+
// In normal operation, the dist dir is the same as the publish dir
|
|
9
|
+
if (!(0, utils_1.shouldSkip)()) {
|
|
10
|
+
return publish;
|
|
11
|
+
}
|
|
12
|
+
// In this situation, the user has disabled the plugin, which means that they might be using next export,
|
|
13
|
+
// so we'll look in a few places to find the site root. This allows us to find the .next directory.
|
|
14
|
+
for (const root of [(0, path_1.resolve)(publish, '..'), (0, path_1.resolve)(publish, '..', '..')]) {
|
|
15
|
+
if ((0, fs_1.existsSync)((0, path_1.join)(root, 'next.config.js'))) {
|
|
16
|
+
return (0, path_1.join)(root, '.next');
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return null;
|
|
20
|
+
};
|
|
5
21
|
const restoreCache = async ({ cache, publish }) => {
|
|
6
|
-
const
|
|
7
|
-
if (
|
|
22
|
+
const distDir = findDistDir(publish);
|
|
23
|
+
if (!distDir) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
if (await cache.restore((0, path_1.join)(distDir, 'cache'))) {
|
|
8
27
|
console.log('Next.js cache restored.');
|
|
9
28
|
}
|
|
10
29
|
else {
|
|
@@ -13,9 +32,11 @@ const restoreCache = async ({ cache, publish }) => {
|
|
|
13
32
|
};
|
|
14
33
|
exports.restoreCache = restoreCache;
|
|
15
34
|
const saveCache = async ({ cache, publish }) => {
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
35
|
+
const distDir = findDistDir(publish);
|
|
36
|
+
if (!distDir) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (await cache.save((0, path_1.join)(distDir, 'cache'))) {
|
|
19
40
|
console.log('Next.js cache saved.');
|
|
20
41
|
}
|
|
21
42
|
else {
|
package/lib/helpers/files.js
CHANGED
|
@@ -14,6 +14,7 @@ const p_limit_1 = __importDefault(require("p-limit"));
|
|
|
14
14
|
const pathe_1 = require("pathe");
|
|
15
15
|
const slash_1 = __importDefault(require("slash"));
|
|
16
16
|
const constants_1 = require("../constants");
|
|
17
|
+
const utils_1 = require("./utils");
|
|
17
18
|
const TEST_ROUTE = /(|\/)\[[^/]+?](\/|\.html|$)/;
|
|
18
19
|
const isDynamicRoute = (route) => TEST_ROUTE.test(route);
|
|
19
20
|
exports.isDynamicRoute = isDynamicRoute;
|
|
@@ -62,7 +63,7 @@ const getMiddleware = async (publish) => {
|
|
|
62
63
|
};
|
|
63
64
|
exports.getMiddleware = getMiddleware;
|
|
64
65
|
// eslint-disable-next-line max-lines-per-function
|
|
65
|
-
const moveStaticPages = async ({ netlifyConfig, target, i18n, }) => {
|
|
66
|
+
const moveStaticPages = async ({ netlifyConfig, target, i18n, basePath, }) => {
|
|
66
67
|
console.log('Moving static page files to serve from CDN...');
|
|
67
68
|
const outputDir = (0, pathe_1.join)(netlifyConfig.build.publish, target === 'server' ? 'server' : 'serverless');
|
|
68
69
|
const root = (0, pathe_1.join)(outputDir, 'pages');
|
|
@@ -93,9 +94,10 @@ const moveStaticPages = async ({ netlifyConfig, target, i18n, }) => {
|
|
|
93
94
|
const isData = file.endsWith('.json');
|
|
94
95
|
const source = (0, pathe_1.join)(root, file);
|
|
95
96
|
const targetFile = isData ? (0, pathe_1.join)(dataDir, file) : file;
|
|
97
|
+
const targetPath = basePath ? (0, pathe_1.join)(basePath, targetFile) : targetFile;
|
|
96
98
|
files.push(file);
|
|
97
|
-
filesManifest[file] =
|
|
98
|
-
const dest = (0, pathe_1.join)(netlifyConfig.build.publish,
|
|
99
|
+
filesManifest[file] = targetPath;
|
|
100
|
+
const dest = (0, pathe_1.join)(netlifyConfig.build.publish, targetPath);
|
|
99
101
|
try {
|
|
100
102
|
await (0, fs_extra_1.move)(source, dest);
|
|
101
103
|
}
|
|
@@ -189,19 +191,23 @@ const moveStaticPages = async ({ netlifyConfig, target, i18n, }) => {
|
|
|
189
191
|
// Write the manifest for use in the serverless functions
|
|
190
192
|
await (0, fs_extra_1.writeJson)((0, pathe_1.join)(netlifyConfig.build.publish, 'static-manifest.json'), Object.entries(filesManifest));
|
|
191
193
|
if (i18n === null || i18n === void 0 ? void 0 : i18n.defaultLocale) {
|
|
194
|
+
const rootPath = basePath ? (0, pathe_1.join)(netlifyConfig.build.publish, basePath) : netlifyConfig.build.publish;
|
|
192
195
|
// Copy the default locale into the root
|
|
193
|
-
const defaultLocaleDir = (0, pathe_1.join)(
|
|
196
|
+
const defaultLocaleDir = (0, pathe_1.join)(rootPath, i18n.defaultLocale);
|
|
194
197
|
if ((0, fs_extra_1.existsSync)(defaultLocaleDir)) {
|
|
195
|
-
await (0, fs_extra_1.copy)(defaultLocaleDir, `${
|
|
198
|
+
await (0, fs_extra_1.copy)(defaultLocaleDir, `${rootPath}/`);
|
|
196
199
|
}
|
|
197
|
-
const defaultLocaleIndex = (0, pathe_1.join)(
|
|
198
|
-
const indexHtml = (0, pathe_1.join)(
|
|
200
|
+
const defaultLocaleIndex = (0, pathe_1.join)(rootPath, `${i18n.defaultLocale}.html`);
|
|
201
|
+
const indexHtml = (0, pathe_1.join)(rootPath, 'index.html');
|
|
199
202
|
if ((0, fs_extra_1.existsSync)(defaultLocaleIndex) && !(0, fs_extra_1.existsSync)(indexHtml)) {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
}
|
|
204
|
-
|
|
203
|
+
await (0, fs_extra_1.copy)(defaultLocaleIndex, indexHtml, { overwrite: false }).catch(() => {
|
|
204
|
+
/* ignore */
|
|
205
|
+
});
|
|
206
|
+
await (0, fs_extra_1.copy)((0, pathe_1.join)(rootPath, `${i18n.defaultLocale}.json`), (0, pathe_1.join)(rootPath, 'index.json'), {
|
|
207
|
+
overwrite: false,
|
|
208
|
+
}).catch(() => {
|
|
209
|
+
/* ignore */
|
|
210
|
+
});
|
|
205
211
|
}
|
|
206
212
|
}
|
|
207
213
|
if (shortRevalidateRoutes.length !== 0) {
|
|
@@ -220,47 +226,52 @@ const moveStaticPages = async ({ netlifyConfig, target, i18n, }) => {
|
|
|
220
226
|
}
|
|
221
227
|
};
|
|
222
228
|
exports.moveStaticPages = moveStaticPages;
|
|
229
|
+
/**
|
|
230
|
+
* Attempt to patch a source file, preserving a backup
|
|
231
|
+
*/
|
|
223
232
|
const patchFile = async ({ file, from, to }) => {
|
|
224
233
|
if (!(0, fs_extra_1.existsSync)(file)) {
|
|
225
|
-
|
|
234
|
+
console.warn('File was not found');
|
|
235
|
+
return false;
|
|
226
236
|
}
|
|
227
237
|
const content = await (0, fs_extra_1.readFile)(file, 'utf8');
|
|
228
238
|
if (content.includes(to)) {
|
|
229
|
-
|
|
239
|
+
console.log('File already patched');
|
|
240
|
+
return false;
|
|
230
241
|
}
|
|
231
242
|
const newContent = content.replace(from, to);
|
|
243
|
+
if (newContent === content) {
|
|
244
|
+
console.warn('File was not changed');
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
232
247
|
await (0, fs_extra_1.writeFile)(`${file}.orig`, content);
|
|
233
248
|
await (0, fs_extra_1.writeFile)(file, newContent);
|
|
249
|
+
console.log('Done');
|
|
250
|
+
return true;
|
|
234
251
|
};
|
|
252
|
+
/**
|
|
253
|
+
* The file we need has moved around a bit over the past few versions,
|
|
254
|
+
* so we iterate through the options until we find it
|
|
255
|
+
*/
|
|
235
256
|
const getServerFile = (root) => {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
}
|
|
243
|
-
if (!serverFile) {
|
|
244
|
-
try {
|
|
245
|
-
// eslint-disable-next-line node/no-missing-require
|
|
246
|
-
serverFile = require.resolve('next/dist/next-server/server/next-server', { paths: [root] });
|
|
247
|
-
}
|
|
248
|
-
catch {
|
|
249
|
-
// Ignore
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
return serverFile;
|
|
257
|
+
const candidates = [
|
|
258
|
+
'next/dist/server/base-server',
|
|
259
|
+
'next/dist/server/next-server',
|
|
260
|
+
'next/dist/next-server/server/next-server',
|
|
261
|
+
];
|
|
262
|
+
return (0, utils_1.findModuleFromBase)({ candidates, paths: [root] });
|
|
253
263
|
};
|
|
254
|
-
const patchNextFiles =
|
|
264
|
+
const patchNextFiles = (root) => {
|
|
255
265
|
const serverFile = getServerFile(root);
|
|
256
266
|
console.log(`Patching ${serverFile}`);
|
|
257
267
|
if (serverFile) {
|
|
258
|
-
|
|
268
|
+
return patchFile({
|
|
259
269
|
file: serverFile,
|
|
260
270
|
from: `let ssgCacheKey = `,
|
|
261
271
|
to: `let ssgCacheKey = process.env._BYPASS_SSG || `,
|
|
262
272
|
});
|
|
263
273
|
}
|
|
274
|
+
return false;
|
|
264
275
|
};
|
|
265
276
|
exports.patchNextFiles = patchNextFiles;
|
|
266
277
|
const unpatchNextFiles = async (root) => {
|
package/lib/helpers/utils.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.shouldSkip = exports.getPreviewRewrites = exports.getApiRewrites = exports.redirectsForNextRouteWithData = exports.redirectsForNextRoute = exports.isApiRoute = exports.routeToDataRoute = exports.netlifyRoutesForNextRouteWithData = exports.toNetlifyRoute = void 0;
|
|
6
|
+
exports.findModuleFromBase = exports.shouldSkip = exports.getPreviewRewrites = exports.getApiRewrites = exports.redirectsForNextRouteWithData = exports.redirectsForNextRoute = exports.isApiRoute = exports.routeToDataRoute = exports.netlifyRoutesForNextRouteWithData = exports.toNetlifyRoute = void 0;
|
|
7
7
|
const globby_1 = __importDefault(require("globby"));
|
|
8
8
|
const pathe_1 = require("pathe");
|
|
9
9
|
const constants_1 = require("../constants");
|
|
@@ -116,3 +116,21 @@ const shouldSkip = () => process.env.NEXT_PLUGIN_FORCE_RUN === 'false' ||
|
|
|
116
116
|
process.env.NETLIFY_NEXT_PLUGIN_SKIP === 'true' ||
|
|
117
117
|
process.env.NETLIFY_NEXT_PLUGIN_SKIP === '1';
|
|
118
118
|
exports.shouldSkip = shouldSkip;
|
|
119
|
+
/**
|
|
120
|
+
* Given an array of base paths and candidate modules, return the first one that exists
|
|
121
|
+
*/
|
|
122
|
+
const findModuleFromBase = ({ paths, candidates }) => {
|
|
123
|
+
for (const candidate of candidates) {
|
|
124
|
+
try {
|
|
125
|
+
const modulePath = require.resolve(candidate, { paths });
|
|
126
|
+
if (modulePath) {
|
|
127
|
+
return modulePath;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
// Ignore the error
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return null;
|
|
135
|
+
};
|
|
136
|
+
exports.findModuleFromBase = findModuleFromBase;
|
package/lib/index.js
CHANGED
|
@@ -49,7 +49,7 @@ const plugin = {
|
|
|
49
49
|
console.log("The flag 'EXPERIMENTAL_MOVE_STATIC_PAGES' is no longer required, as it is now the default. To disable this behavior, set the env var 'SERVE_STATIC_FILES_FROM_ORIGIN' to 'true'");
|
|
50
50
|
}
|
|
51
51
|
if (!process.env.SERVE_STATIC_FILES_FROM_ORIGIN) {
|
|
52
|
-
await (0, files_1.moveStaticPages)({ target, netlifyConfig, i18n });
|
|
52
|
+
await (0, files_1.moveStaticPages)({ target, netlifyConfig, i18n, basePath });
|
|
53
53
|
}
|
|
54
54
|
await (0, redirects_1.generateStaticRedirects)({
|
|
55
55
|
netlifyConfig,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@netlify/plugin-nextjs",
|
|
3
|
-
"version": "4.2.
|
|
3
|
+
"version": "4.2.7",
|
|
4
4
|
"description": "Run Next.js seamlessly on Netlify",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"files": [
|
|
@@ -53,13 +53,14 @@
|
|
|
53
53
|
},
|
|
54
54
|
"homepage": "https://github.com/netlify/netlify-plugin-nextjs#readme",
|
|
55
55
|
"dependencies": {
|
|
56
|
-
"@netlify/functions": "^0.
|
|
56
|
+
"@netlify/functions": "^1.0.0",
|
|
57
57
|
"@netlify/ipx": "^0.0.9",
|
|
58
58
|
"@vercel/node-bridge": "^2.1.0",
|
|
59
59
|
"chalk": "^4.1.2",
|
|
60
60
|
"fs-extra": "^10.0.0",
|
|
61
61
|
"globby": "^11.0.4",
|
|
62
62
|
"moize": "^6.1.0",
|
|
63
|
+
"next": "^12.0.10",
|
|
63
64
|
"node-fetch": "^2.6.6",
|
|
64
65
|
"node-stream-zip": "^1.15.0",
|
|
65
66
|
"outdent": "^0.8.0",
|
|
@@ -74,13 +75,16 @@
|
|
|
74
75
|
"@babel/core": "^7.15.8",
|
|
75
76
|
"@babel/preset-env": "^7.15.8",
|
|
76
77
|
"@babel/preset-typescript": "^7.16.0",
|
|
77
|
-
"@netlify/build": "^26.
|
|
78
|
-
"@netlify/eslint-config-node": "^
|
|
78
|
+
"@netlify/build": "^26.3.3",
|
|
79
|
+
"@netlify/eslint-config-node": "^5.1.4",
|
|
80
|
+
"@reach/dialog": "^0.16.2",
|
|
81
|
+
"@reach/visually-hidden": "^0.16.0",
|
|
79
82
|
"@testing-library/cypress": "^8.0.1",
|
|
80
83
|
"@types/fs-extra": "^9.0.13",
|
|
81
84
|
"@types/jest": "^27.0.2",
|
|
82
85
|
"@types/mocha": "^9.0.0",
|
|
83
86
|
"@types/node": "^17.0.10",
|
|
87
|
+
"@types/react": "^17.0.38",
|
|
84
88
|
"babel-jest": "^27.2.5",
|
|
85
89
|
"cpy": "^8.1.2",
|
|
86
90
|
"cypress": "^9.0.0",
|
|
@@ -88,12 +92,12 @@
|
|
|
88
92
|
"husky": "^7.0.4",
|
|
89
93
|
"jest": "^27.0.0",
|
|
90
94
|
"netlify-plugin-cypress": "^2.2.0",
|
|
91
|
-
"next": "^12.0.8",
|
|
92
95
|
"npm-run-all": "^4.1.5",
|
|
93
96
|
"prettier": "^2.1.2",
|
|
94
97
|
"react": "^17.0.1",
|
|
95
98
|
"react-dom": "^17.0.1",
|
|
96
99
|
"rimraf": "^3.0.2",
|
|
100
|
+
"sass": "^1.49.0",
|
|
97
101
|
"tmp-promise": "^3.0.2",
|
|
98
102
|
"typescript": "^4.3.4"
|
|
99
103
|
},
|