@netlify/plugin-nextjs 4.9.1 → 4.10.0

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.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.updateConfig = exports.writeMiddleware = void 0;
3
+ exports.updateConfig = exports.writeEdgeFunctions = exports.loadMiddlewareManifest = 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");
@@ -11,47 +11,66 @@ const loadMiddlewareManifest = (netlifyConfig) => {
11
11
  }
12
12
  return (0, fs_extra_1.readJson)(middlewarePath);
13
13
  };
14
+ exports.loadMiddlewareManifest = loadMiddlewareManifest;
14
15
  /**
15
16
  * Convert the Next middleware name into a valid Edge Function name
16
17
  */
17
- const sanitizeName = (name) => `next${name === '/' ? '_index' : name.replace(/\W/g, '_')}`;
18
+ const sanitizeName = (name) => `next_${name.replace(/\W/g, '_')}`;
18
19
  /**
19
20
  * Initialization added to the top of the edge function bundle
20
21
  */
21
22
  const bootstrap = /* js */ `
23
+ globalThis.process = { env: {...Deno.env.toObject(), NEXT_RUNTIME: 'edge', 'NEXT_PRIVATE_MINIMAL_MODE': '1' } }
22
24
  globalThis._ENTRIES ||= {}
25
+ // Deno defines "window", but naughty libraries think this means it's a browser
23
26
  delete globalThis.window
24
27
 
25
- `;
26
- // TODO: set the proper env
27
- const getEnv = () => /* js */ `
28
- globalThis.process = { env: {} }
29
28
  `;
30
29
  /**
31
30
  * Concatenates the Next edge function code with the required chunks and adds an export
32
31
  */
33
- const getMiddlewareBundle = async ({ middlewareDefinition, netlifyConfig, }) => {
32
+ const getMiddlewareBundle = async ({ edgeFunctionDefinition, netlifyConfig, }) => {
34
33
  const { publish } = netlifyConfig.build;
35
- const chunks = [bootstrap, getEnv()];
36
- for (const file of middlewareDefinition.files) {
34
+ const chunks = [bootstrap];
35
+ for (const file of edgeFunctionDefinition.files) {
37
36
  const filePath = (0, path_1.join)(publish, file);
38
37
  const data = await fs_1.promises.readFile(filePath, 'utf8');
39
38
  chunks.push('{', data, '}');
40
39
  }
41
- const middleware = await fs_1.promises.readFile((0, path_1.join)(publish, `server`, `${middlewareDefinition.name}.js`), 'utf8');
40
+ const middleware = await fs_1.promises.readFile((0, path_1.join)(publish, `server`, `${edgeFunctionDefinition.name}.js`), 'utf8');
42
41
  chunks.push(middleware);
43
- const exports = /* js */ `export default _ENTRIES["middleware_${middlewareDefinition.name}"].default;`;
42
+ const exports = /* js */ `export default _ENTRIES["middleware_${edgeFunctionDefinition.name}"].default;`;
44
43
  chunks.push(exports);
45
44
  return chunks.join('\n');
46
45
  };
47
46
  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
47
  // Edge functions don't support lookahead expressions
49
48
  const stripLookahead = (regex) => regex.replace('^/(?!_next)', '^/');
49
+ const writeEdgeFunction = async ({ edgeFunctionDefinition, edgeFunctionRoot, netlifyConfig, }) => {
50
+ const name = sanitizeName(edgeFunctionDefinition.name);
51
+ const edgeFunctionDir = (0, path_1.join)(edgeFunctionRoot, name);
52
+ const bundle = await getMiddlewareBundle({
53
+ edgeFunctionDefinition,
54
+ netlifyConfig,
55
+ });
56
+ await (0, fs_extra_1.ensureDir)(edgeFunctionDir);
57
+ await fs_1.promises.writeFile((0, path_1.join)(edgeFunctionDir, 'bundle.js'), bundle);
58
+ await copyEdgeSourceFile({
59
+ edgeFunctionDir,
60
+ file: 'runtime.ts',
61
+ target: 'index.ts',
62
+ });
63
+ await copyEdgeSourceFile({ edgeFunctionDir, file: 'utils.ts' });
64
+ return {
65
+ function: name,
66
+ pattern: stripLookahead(edgeFunctionDefinition.regexp),
67
+ };
68
+ };
50
69
  /**
51
70
  * Writes Edge Functions for the Next middleware
52
71
  */
53
- const writeMiddleware = async (netlifyConfig) => {
54
- const middlewareManifest = await loadMiddlewareManifest(netlifyConfig);
72
+ const writeEdgeFunctions = async (netlifyConfig) => {
73
+ const middlewareManifest = await (0, exports.loadMiddlewareManifest)(netlifyConfig);
55
74
  if (!middlewareManifest) {
56
75
  console.error("Couldn't find the middleware manifest");
57
76
  return;
@@ -68,29 +87,29 @@ const writeMiddleware = async (netlifyConfig) => {
68
87
  path: '/_next/image*',
69
88
  });
70
89
  for (const middleware of middlewareManifest.sortedMiddleware) {
71
- const name = sanitizeName(middleware);
72
- const edgeFunctionDir = (0, path_1.join)(edgeFunctionRoot, name);
73
- const middlewareDefinition = middlewareManifest.middleware[middleware];
74
- const bundle = await getMiddlewareBundle({
75
- middlewareDefinition,
90
+ const edgeFunctionDefinition = middlewareManifest.middleware[middleware];
91
+ const functionDefinition = await writeEdgeFunction({
92
+ edgeFunctionDefinition,
93
+ edgeFunctionRoot,
76
94
  netlifyConfig,
77
95
  });
78
- await (0, fs_extra_1.ensureDir)(edgeFunctionDir);
79
- await fs_1.promises.writeFile((0, path_1.join)(edgeFunctionDir, 'bundle.js'), bundle);
80
- await copyEdgeSourceFile({
81
- edgeFunctionDir,
82
- file: 'runtime.ts',
83
- target: 'index.ts',
84
- });
85
- await copyEdgeSourceFile({ edgeFunctionDir, file: 'utils.ts' });
86
- manifest.functions.push({
87
- function: name,
88
- pattern: stripLookahead(middlewareDefinition.regexp),
89
- });
96
+ manifest.functions.push(functionDefinition);
97
+ }
98
+ // Older versions of the manifest format don't have the functions field
99
+ // No, the version field was not incremented
100
+ if (typeof middlewareManifest.functions === 'object') {
101
+ for (const edgeFunctionDefinition of Object.values(middlewareManifest.functions)) {
102
+ const functionDefinition = await writeEdgeFunction({
103
+ edgeFunctionDefinition,
104
+ edgeFunctionRoot,
105
+ netlifyConfig,
106
+ });
107
+ manifest.functions.push(functionDefinition);
108
+ }
90
109
  }
91
110
  await (0, fs_extra_1.writeJson)((0, path_1.join)(edgeFunctionRoot, 'manifest.json'), manifest);
92
111
  };
93
- exports.writeMiddleware = writeMiddleware;
112
+ exports.writeEdgeFunctions = writeEdgeFunctions;
94
113
  const updateConfig = async (publish) => {
95
114
  const configFile = (0, path_1.join)(publish, 'required-server-files.json');
96
115
  const config = await (0, fs_extra_1.readJSON)(configFile);
@@ -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,9 +90,17 @@ 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.writeMiddleware)(netlifyConfig);
93
+ await (0, edge_1.writeEdgeFunctions)(netlifyConfig);
94
94
  await (0, edge_1.updateConfig)(publish);
95
95
  }
96
+ const middlewareManifest = await (0, edge_1.loadMiddlewareManifest)(netlifyConfig);
97
+ if (!process.env.NEXT_USE_NETLIFY_EDGE && middlewareManifest.sortedMiddleware.length !== 0) {
98
+ console.log((0, chalk_1.yellowBright)((0, outdent_1.outdent) `
99
+ You are using Next.js Middleware without Netlify Edge Functions.
100
+ This will soon be deprecated because it negatively affects performance and will disable ISR and static rendering.
101
+ To get the best performance and use Netlify Edge Functions, set the env var ${(0, chalk_1.bold) `NEXT_USE_NETLIFY_EDGE=true`}.
102
+ `));
103
+ }
96
104
  },
97
105
  async onPostBuild({ netlifyConfig: { build: { publish }, redirects, headers, }, utils: { status, cache, functions, build: { failBuild }, }, constants: { FUNCTIONS_DIST }, }) {
98
106
  await (0, cache_1.saveCache)({ cache, publish });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/plugin-nextjs",
3
- "version": "4.9.1",
3
+ "version": "4.10.0",
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.0",
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,11 +28,11 @@
28
28
  },
29
29
  "devDependencies": {
30
30
  "@delucis/if-env": "^1.1.2",
31
- "@netlify/build": "^27.1.3",
31
+ "@netlify/build": "^27.3.1",
32
32
  "@types/fs-extra": "^9.0.13",
33
33
  "@types/jest": "^27.4.1",
34
34
  "@types/node": "^17.0.25",
35
- "next": "^12.1.7-canary.33",
35
+ "next": "^12.2.0",
36
36
  "npm-run-all": "^4.1.5",
37
37
  "typescript": "^4.6.3"
38
38
  },