@netlify/plugin-nextjs 4.9.0 → 4.9.3

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 = 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) => `next${name === '/' ? '_index' : name.replace(/\W/g, '_')}`;
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 ({ middlewareDefinition, netlifyConfig, }) => {
31
+ const getMiddlewareBundle = async ({ edgeFunctionDefinition, netlifyConfig, }) => {
34
32
  const { publish } = netlifyConfig.build;
35
- const chunks = [bootstrap, getEnv()];
36
- for (const file of middlewareDefinition.files) {
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`, `${middlewareDefinition.name}.js`), 'utf8');
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_${middlewareDefinition.name}"].default;`;
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 writeMiddleware = async (netlifyConfig) => {
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,29 @@ const writeMiddleware = async (netlifyConfig) => {
68
86
  path: '/_next/image*',
69
87
  });
70
88
  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,
89
+ const edgeFunctionDefinition = middlewareManifest.middleware[middleware];
90
+ const functionDefinition = await writeEdgeFunction({
91
+ edgeFunctionDefinition,
92
+ edgeFunctionRoot,
76
93
  netlifyConfig,
77
94
  });
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
- });
95
+ manifest.functions.push(functionDefinition);
96
+ }
97
+ // Older versions of the manifest format don't have the functions field
98
+ // No, the version field was not incremented
99
+ if (typeof middlewareManifest.functions === 'object') {
100
+ for (const edgeFunctionDefinition of Object.values(middlewareManifest.functions)) {
101
+ const functionDefinition = await writeEdgeFunction({
102
+ edgeFunctionDefinition,
103
+ edgeFunctionRoot,
104
+ netlifyConfig,
105
+ });
106
+ manifest.functions.push(functionDefinition);
107
+ }
90
108
  }
91
109
  await (0, fs_extra_1.writeJson)((0, path_1.join)(edgeFunctionRoot, 'manifest.json'), manifest);
92
110
  };
93
- exports.writeMiddleware = writeMiddleware;
111
+ exports.writeEdgeFunctions = writeEdgeFunctions;
94
112
  const updateConfig = async (publish) => {
95
113
  const configFile = (0, path_1.join)(publish, 'required-server-files.json');
96
114
  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
@@ -39,10 +39,14 @@ const plugin = {
39
39
  }
40
40
  const { publish } = netlifyConfig.build;
41
41
  (0, verification_1.checkNextSiteHasBuilt)({ publish, failBuild });
42
- const { appDir, basePath, i18n, images, target, ignore, trailingSlash, outdir, experimental: { images: { remotePatterns }, }, } = await (0, config_1.getNextConfig)({
42
+ let experimentalRemotePatterns = [];
43
+ const { appDir, basePath, i18n, images, target, ignore, trailingSlash, outdir, experimental } = await (0, config_1.getNextConfig)({
43
44
  publish,
44
45
  failBuild,
45
46
  });
47
+ if (experimental.images) {
48
+ experimentalRemotePatterns = experimental.images.remotePatterns || [];
49
+ }
46
50
  if ((0, utils_1.isNextAuthInstalled)()) {
47
51
  const config = await (0, config_1.getRequiredServerFiles)(publish);
48
52
  const userDefinedNextAuthUrl = config.config.env.NEXTAUTH_URL;
@@ -69,7 +73,13 @@ const plugin = {
69
73
  netlifyConfig,
70
74
  nextConfig: { basePath, i18n },
71
75
  });
72
- await (0, functions_1.setupImageFunction)({ constants, imageconfig: images, netlifyConfig, basePath, remotePatterns });
76
+ await (0, functions_1.setupImageFunction)({
77
+ constants,
78
+ imageconfig: images,
79
+ netlifyConfig,
80
+ basePath,
81
+ remotePatterns: experimentalRemotePatterns,
82
+ });
73
83
  await (0, redirects_1.generateRedirects)({
74
84
  netlifyConfig,
75
85
  nextConfig: { basePath, i18n, trailingSlash, appDir },
@@ -80,7 +90,7 @@ const plugin = {
80
90
  ✨ Deploying to ${(0, chalk_1.greenBright) `Netlify Edge Functions`} ✨
81
91
  This feature is in beta. Please share your feedback here: https://ntl.fyi/next-netlify-edge
82
92
  `);
83
- await (0, edge_1.writeMiddleware)(netlifyConfig);
93
+ await (0, edge_1.writeEdgeFunctions)(netlifyConfig);
84
94
  await (0, edge_1.updateConfig)(publish);
85
95
  }
86
96
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/plugin-nextjs",
3
- "version": "4.9.0",
3
+ "version": "4.9.3",
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,7 +28,7 @@
28
28
  },
29
29
  "devDependencies": {
30
30
  "@delucis/if-env": "^1.1.2",
31
- "@netlify/build": "^27.1.3",
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",