@netlify/plugin-nextjs 4.9.1 → 4.9.2
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/edge.js +43 -29
- package/lib/helpers/files.js +1 -1
- package/lib/index.js +1 -1
- package/package.json +3 -3
package/lib/helpers/edge.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.updateConfig = exports.
|
|
3
|
+
exports.updateConfig = exports.writeEdgeFunctions = void 0;
|
|
4
4
|
const fs_1 = require("fs");
|
|
5
5
|
const path_1 = require("path");
|
|
6
6
|
const fs_extra_1 = require("fs-extra");
|
|
@@ -14,43 +14,61 @@ const loadMiddlewareManifest = (netlifyConfig) => {
|
|
|
14
14
|
/**
|
|
15
15
|
* Convert the Next middleware name into a valid Edge Function name
|
|
16
16
|
*/
|
|
17
|
-
const sanitizeName = (name) => `
|
|
17
|
+
const sanitizeName = (name) => `next_${name.replace(/\W/g, '_')}`;
|
|
18
18
|
/**
|
|
19
19
|
* Initialization added to the top of the edge function bundle
|
|
20
20
|
*/
|
|
21
21
|
const bootstrap = /* js */ `
|
|
22
|
+
globalThis.process = { env: {...Deno.env.toObject(), NEXT_RUNTIME: 'edge', 'NEXT_PRIVATE_MINIMAL_MODE': '1' } }
|
|
22
23
|
globalThis._ENTRIES ||= {}
|
|
24
|
+
// Deno defines "window", but naughty libraries think this means it's a browser
|
|
23
25
|
delete globalThis.window
|
|
24
26
|
|
|
25
|
-
`;
|
|
26
|
-
// TODO: set the proper env
|
|
27
|
-
const getEnv = () => /* js */ `
|
|
28
|
-
globalThis.process = { env: {} }
|
|
29
27
|
`;
|
|
30
28
|
/**
|
|
31
29
|
* Concatenates the Next edge function code with the required chunks and adds an export
|
|
32
30
|
*/
|
|
33
|
-
const getMiddlewareBundle = async ({
|
|
31
|
+
const getMiddlewareBundle = async ({ edgeFunctionDefinition, netlifyConfig, }) => {
|
|
34
32
|
const { publish } = netlifyConfig.build;
|
|
35
|
-
const chunks = [bootstrap
|
|
36
|
-
for (const file of
|
|
33
|
+
const chunks = [bootstrap];
|
|
34
|
+
for (const file of edgeFunctionDefinition.files) {
|
|
37
35
|
const filePath = (0, path_1.join)(publish, file);
|
|
38
36
|
const data = await fs_1.promises.readFile(filePath, 'utf8');
|
|
39
37
|
chunks.push('{', data, '}');
|
|
40
38
|
}
|
|
41
|
-
const middleware = await fs_1.promises.readFile((0, path_1.join)(publish, `server`, `${
|
|
39
|
+
const middleware = await fs_1.promises.readFile((0, path_1.join)(publish, `server`, `${edgeFunctionDefinition.name}.js`), 'utf8');
|
|
42
40
|
chunks.push(middleware);
|
|
43
|
-
const exports = /* js */ `export default _ENTRIES["middleware_${
|
|
41
|
+
const exports = /* js */ `export default _ENTRIES["middleware_${edgeFunctionDefinition.name}"].default;`;
|
|
44
42
|
chunks.push(exports);
|
|
45
43
|
return chunks.join('\n');
|
|
46
44
|
};
|
|
47
45
|
const copyEdgeSourceFile = ({ file, target, edgeFunctionDir, }) => fs_1.promises.copyFile((0, path_1.join)(__dirname, '..', '..', 'src', 'templates', 'edge', file), (0, path_1.join)(edgeFunctionDir, target !== null && target !== void 0 ? target : file));
|
|
48
46
|
// Edge functions don't support lookahead expressions
|
|
49
47
|
const stripLookahead = (regex) => regex.replace('^/(?!_next)', '^/');
|
|
48
|
+
const writeEdgeFunction = async ({ edgeFunctionDefinition, edgeFunctionRoot, netlifyConfig, }) => {
|
|
49
|
+
const name = sanitizeName(edgeFunctionDefinition.name);
|
|
50
|
+
const edgeFunctionDir = (0, path_1.join)(edgeFunctionRoot, name);
|
|
51
|
+
const bundle = await getMiddlewareBundle({
|
|
52
|
+
edgeFunctionDefinition,
|
|
53
|
+
netlifyConfig,
|
|
54
|
+
});
|
|
55
|
+
await (0, fs_extra_1.ensureDir)(edgeFunctionDir);
|
|
56
|
+
await fs_1.promises.writeFile((0, path_1.join)(edgeFunctionDir, 'bundle.js'), bundle);
|
|
57
|
+
await copyEdgeSourceFile({
|
|
58
|
+
edgeFunctionDir,
|
|
59
|
+
file: 'runtime.ts',
|
|
60
|
+
target: 'index.ts',
|
|
61
|
+
});
|
|
62
|
+
await copyEdgeSourceFile({ edgeFunctionDir, file: 'utils.ts' });
|
|
63
|
+
return {
|
|
64
|
+
function: name,
|
|
65
|
+
pattern: stripLookahead(edgeFunctionDefinition.regexp),
|
|
66
|
+
};
|
|
67
|
+
};
|
|
50
68
|
/**
|
|
51
69
|
* Writes Edge Functions for the Next middleware
|
|
52
70
|
*/
|
|
53
|
-
const
|
|
71
|
+
const writeEdgeFunctions = async (netlifyConfig) => {
|
|
54
72
|
const middlewareManifest = await loadMiddlewareManifest(netlifyConfig);
|
|
55
73
|
if (!middlewareManifest) {
|
|
56
74
|
console.error("Couldn't find the middleware manifest");
|
|
@@ -68,29 +86,25 @@ const writeMiddleware = async (netlifyConfig) => {
|
|
|
68
86
|
path: '/_next/image*',
|
|
69
87
|
});
|
|
70
88
|
for (const middleware of middlewareManifest.sortedMiddleware) {
|
|
71
|
-
const
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
middlewareDefinition,
|
|
89
|
+
const edgeFunctionDefinition = middlewareManifest.middleware[middleware];
|
|
90
|
+
const functionDefinition = await writeEdgeFunction({
|
|
91
|
+
edgeFunctionDefinition,
|
|
92
|
+
edgeFunctionRoot,
|
|
76
93
|
netlifyConfig,
|
|
77
94
|
});
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
await copyEdgeSourceFile({ edgeFunctionDir, file: 'utils.ts' });
|
|
86
|
-
manifest.functions.push({
|
|
87
|
-
function: name,
|
|
88
|
-
pattern: stripLookahead(middlewareDefinition.regexp),
|
|
95
|
+
manifest.functions.push(functionDefinition);
|
|
96
|
+
}
|
|
97
|
+
for (const edgeFunctionDefinition of Object.values(middlewareManifest.functions)) {
|
|
98
|
+
const functionDefinition = await writeEdgeFunction({
|
|
99
|
+
edgeFunctionDefinition,
|
|
100
|
+
edgeFunctionRoot,
|
|
101
|
+
netlifyConfig,
|
|
89
102
|
});
|
|
103
|
+
manifest.functions.push(functionDefinition);
|
|
90
104
|
}
|
|
91
105
|
await (0, fs_extra_1.writeJson)((0, path_1.join)(edgeFunctionRoot, 'manifest.json'), manifest);
|
|
92
106
|
};
|
|
93
|
-
exports.
|
|
107
|
+
exports.writeEdgeFunctions = writeEdgeFunctions;
|
|
94
108
|
const updateConfig = async (publish) => {
|
|
95
109
|
const configFile = (0, path_1.join)(publish, 'required-server-files.json');
|
|
96
110
|
const config = await (0, fs_extra_1.readJSON)(configFile);
|
package/lib/helpers/files.js
CHANGED
|
@@ -72,7 +72,7 @@ const moveStaticPages = async ({ netlifyConfig, target, i18n, basePath, }) => {
|
|
|
72
72
|
const root = (0, pathe_1.join)(outputDir, 'pages');
|
|
73
73
|
const buildId = (0, fs_extra_1.readFileSync)((0, pathe_1.join)(netlifyConfig.build.publish, 'BUILD_ID'), 'utf8').trim();
|
|
74
74
|
const dataDir = (0, pathe_1.join)('_next', 'data', buildId);
|
|
75
|
-
await (0, fs_extra_1.ensureDir)(dataDir);
|
|
75
|
+
await (0, fs_extra_1.ensureDir)((0, pathe_1.join)(netlifyConfig.build.publish, dataDir));
|
|
76
76
|
// Load the middleware manifest so we can check if a file matches it before moving
|
|
77
77
|
const middlewarePaths = await (0, exports.getMiddleware)(netlifyConfig.build.publish);
|
|
78
78
|
const middleware = middlewarePaths.map((path) => path.slice(1));
|
package/lib/index.js
CHANGED
|
@@ -90,7 +90,7 @@ const plugin = {
|
|
|
90
90
|
✨ Deploying to ${(0, chalk_1.greenBright) `Netlify Edge Functions`} ✨
|
|
91
91
|
This feature is in beta. Please share your feedback here: https://ntl.fyi/next-netlify-edge
|
|
92
92
|
`);
|
|
93
|
-
await (0, edge_1.
|
|
93
|
+
await (0, edge_1.writeEdgeFunctions)(netlifyConfig);
|
|
94
94
|
await (0, edge_1.updateConfig)(publish);
|
|
95
95
|
}
|
|
96
96
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@netlify/plugin-nextjs",
|
|
3
|
-
"version": "4.9.
|
|
3
|
+
"version": "4.9.2",
|
|
4
4
|
"description": "Run Next.js seamlessly on Netlify",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"files": [
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
],
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"@netlify/functions": "^1.0.0",
|
|
13
|
-
"@netlify/ipx": "^1.1.
|
|
13
|
+
"@netlify/ipx": "^1.1.2",
|
|
14
14
|
"@vercel/node-bridge": "^2.1.0",
|
|
15
15
|
"chalk": "^4.1.2",
|
|
16
16
|
"fs-extra": "^10.0.0",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@delucis/if-env": "^1.1.2",
|
|
31
|
-
"@netlify/build": "^27.
|
|
31
|
+
"@netlify/build": "^27.2.0",
|
|
32
32
|
"@types/fs-extra": "^9.0.13",
|
|
33
33
|
"@types/jest": "^27.4.1",
|
|
34
34
|
"@types/node": "^17.0.25",
|