@netlify/plugin-nextjs 4.8.0 → 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.
@@ -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,25 @@ 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),
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.writeMiddleware = writeMiddleware;
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);
@@ -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));
@@ -40,7 +40,7 @@ const generatePagesResolver = async ({ constants: { INTERNAL_FUNCTIONS_SRC, FUNC
40
40
  };
41
41
  exports.generatePagesResolver = generatePagesResolver;
42
42
  // Move our next/image function into the correct functions directory
43
- const setupImageFunction = async ({ constants: { INTERNAL_FUNCTIONS_SRC, FUNCTIONS_SRC = constants_1.DEFAULT_FUNCTIONS_SRC }, imageconfig = {}, netlifyConfig, basePath, }) => {
43
+ const setupImageFunction = async ({ constants: { INTERNAL_FUNCTIONS_SRC, FUNCTIONS_SRC = constants_1.DEFAULT_FUNCTIONS_SRC }, imageconfig = {}, netlifyConfig, basePath, remotePatterns, }) => {
44
44
  const functionsPath = INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC;
45
45
  const functionName = `${constants_1.IMAGE_FUNCTION_NAME}.js`;
46
46
  const functionDirectory = (0, pathe_1.join)(functionsPath, constants_1.IMAGE_FUNCTION_NAME);
@@ -48,6 +48,7 @@ const setupImageFunction = async ({ constants: { INTERNAL_FUNCTIONS_SRC, FUNCTIO
48
48
  await (0, fs_extra_1.writeJSON)((0, pathe_1.join)(functionDirectory, 'imageconfig.json'), {
49
49
  ...imageconfig,
50
50
  basePath: [basePath, constants_1.IMAGE_FUNCTION_NAME].join('/'),
51
+ remotePatterns,
51
52
  });
52
53
  await (0, fs_extra_1.copyFile)((0, pathe_1.join)(__dirname, '..', '..', 'lib', 'templates', 'ipx.js'), (0, pathe_1.join)(functionDirectory, functionName));
53
54
  const imagePath = imageconfig.path || '/_next/image';
package/lib/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ /* eslint-disable max-lines */
3
4
  const path_1 = require("path");
4
5
  const chalk_1 = require("chalk");
5
6
  const fs_extra_1 = require("fs-extra");
@@ -38,10 +39,14 @@ const plugin = {
38
39
  }
39
40
  const { publish } = netlifyConfig.build;
40
41
  (0, verification_1.checkNextSiteHasBuilt)({ publish, failBuild });
41
- const { appDir, basePath, i18n, images, target, ignore, trailingSlash, outdir } = await (0, config_1.getNextConfig)({
42
+ let experimentalRemotePatterns = [];
43
+ const { appDir, basePath, i18n, images, target, ignore, trailingSlash, outdir, experimental } = await (0, config_1.getNextConfig)({
42
44
  publish,
43
45
  failBuild,
44
46
  });
47
+ if (experimental.images) {
48
+ experimentalRemotePatterns = experimental.images.remotePatterns || [];
49
+ }
45
50
  if ((0, utils_1.isNextAuthInstalled)()) {
46
51
  const config = await (0, config_1.getRequiredServerFiles)(publish);
47
52
  const userDefinedNextAuthUrl = config.config.env.NEXTAUTH_URL;
@@ -68,7 +73,13 @@ const plugin = {
68
73
  netlifyConfig,
69
74
  nextConfig: { basePath, i18n },
70
75
  });
71
- await (0, functions_1.setupImageFunction)({ constants, imageconfig: images, netlifyConfig, basePath });
76
+ await (0, functions_1.setupImageFunction)({
77
+ constants,
78
+ imageconfig: images,
79
+ netlifyConfig,
80
+ basePath,
81
+ remotePatterns: experimentalRemotePatterns,
82
+ });
72
83
  await (0, redirects_1.generateRedirects)({
73
84
  netlifyConfig,
74
85
  nextConfig: { basePath, i18n, trailingSlash, appDir },
@@ -79,7 +90,7 @@ const plugin = {
79
90
  ✨ Deploying to ${(0, chalk_1.greenBright) `Netlify Edge Functions`} ✨
80
91
  This feature is in beta. Please share your feedback here: https://ntl.fyi/next-netlify-edge
81
92
  `);
82
- await (0, edge_1.writeMiddleware)(netlifyConfig);
93
+ await (0, edge_1.writeEdgeFunctions)(netlifyConfig);
83
94
  await (0, edge_1.updateConfig)(publish);
84
95
  }
85
96
  },
@@ -105,3 +116,4 @@ const plugin = {
105
116
  },
106
117
  };
107
118
  module.exports = plugin;
119
+ /* eslint-enable max-lines */
@@ -7,5 +7,6 @@ const imageconfig_json_1 = require("./imageconfig.json");
7
7
  exports.handler = (0, ipx_1.createIPXHandler)({
8
8
  basePath: imageconfig_json_1.basePath,
9
9
  domains: imageconfig_json_1.domains,
10
+ remotePatterns: imageconfig_json_1.remotePatterns,
10
11
  });
11
12
  /* eslint-enable node/no-missing-import, import/no-unresolved, @typescript-eslint/ban-ts-comment */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/plugin-nextjs",
3
- "version": "4.8.0",
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.0.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,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.2.0",
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.6",
35
+ "next": "^12.1.7-canary.33",
36
36
  "npm-run-all": "^4.1.5",
37
37
  "typescript": "^4.6.3"
38
38
  },
@@ -34,7 +34,7 @@ export interface RequestData {
34
34
 
35
35
  const handler = async (req: Request, context: Context) => {
36
36
  const url = new URL(req.url)
37
- if (url.pathname.startsWith('/_next/')) {
37
+ if (url.pathname.startsWith('/_next/static/')) {
38
38
  return
39
39
  }
40
40