@netlify/plugin-nextjs 4.28.6 → 4.29.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.
@@ -53,12 +53,12 @@ const matchesRewrite = (file, rewrites) => {
53
53
  return (0, exports.matchesRedirect)(file, rewrites.beforeFiles);
54
54
  };
55
55
  exports.matchesRewrite = matchesRewrite;
56
- const getMiddleware = async (publish) => {
56
+ const getMiddleware = async (distDir) => {
57
57
  var _a;
58
58
  if (process.env.NEXT_DISABLE_NETLIFY_EDGE !== 'true' && process.env.NEXT_DISABLE_NETLIFY_EDGE !== '1') {
59
59
  return [];
60
60
  }
61
- const manifestPath = (0, pathe_1.join)(publish, 'server', 'middleware-manifest.json');
61
+ const manifestPath = (0, pathe_1.join)(distDir, 'server', 'middleware-manifest.json');
62
62
  if ((0, fs_extra_1.existsSync)(manifestPath)) {
63
63
  const manifest = await (0, fs_extra_1.readJson)(manifestPath, { throws: false });
64
64
  return (_a = manifest === null || manifest === void 0 ? void 0 : manifest.sortedMiddleware) !== null && _a !== void 0 ? _a : [];
@@ -67,18 +67,18 @@ const getMiddleware = async (publish) => {
67
67
  };
68
68
  exports.getMiddleware = getMiddleware;
69
69
  // eslint-disable-next-line max-lines-per-function
70
- const moveStaticPages = async ({ netlifyConfig, target, i18n, basePath, }) => {
70
+ const moveStaticPages = async ({ distDir, i18n, basePath, publishDir, }) => {
71
71
  console.log('Moving static page files to serve from CDN...');
72
- const outputDir = (0, pathe_1.join)(netlifyConfig.build.publish, target === 'server' ? 'server' : 'serverless');
72
+ const outputDir = (0, pathe_1.join)(distDir, 'server');
73
73
  const root = (0, pathe_1.join)(outputDir, 'pages');
74
- const buildId = (0, fs_extra_1.readFileSync)((0, pathe_1.join)(netlifyConfig.build.publish, 'BUILD_ID'), 'utf8').trim();
74
+ const buildId = (0, fs_extra_1.readFileSync)((0, pathe_1.join)(distDir, 'BUILD_ID'), 'utf8').trim();
75
75
  const dataDir = (0, pathe_1.join)('_next', 'data', buildId);
76
- await (0, fs_extra_1.ensureDir)((0, pathe_1.join)(netlifyConfig.build.publish, dataDir));
76
+ await (0, fs_extra_1.ensureDir)((0, pathe_1.join)(publishDir, dataDir));
77
77
  // Load the middleware manifest so we can check if a file matches it before moving
78
- const middlewarePaths = await (0, exports.getMiddleware)(netlifyConfig.build.publish);
78
+ const middlewarePaths = await (0, exports.getMiddleware)(distDir);
79
79
  const middleware = middlewarePaths.map((path) => path.slice(1));
80
- const prerenderManifest = await (0, fs_extra_1.readJson)((0, pathe_1.join)(netlifyConfig.build.publish, 'prerender-manifest.json'));
81
- const { redirects, rewrites } = await (0, fs_extra_1.readJson)((0, pathe_1.join)(netlifyConfig.build.publish, 'routes-manifest.json'));
80
+ const prerenderManifest = await (0, fs_extra_1.readJson)((0, pathe_1.join)(distDir, 'prerender-manifest.json'));
81
+ const { redirects, rewrites } = await (0, fs_extra_1.readJson)((0, pathe_1.join)(distDir, 'routes-manifest.json'));
82
82
  const isrFiles = new Set();
83
83
  const shortRevalidateRoutes = [];
84
84
  Object.entries(prerenderManifest.routes).forEach(([route, { initialRevalidateSeconds }]) => {
@@ -101,7 +101,7 @@ const moveStaticPages = async ({ netlifyConfig, target, i18n, basePath, }) => {
101
101
  const targetPath = basePath ? (0, pathe_1.join)(basePath, targetFile) : targetFile;
102
102
  files.push(file);
103
103
  filesManifest[file] = targetPath;
104
- const dest = (0, pathe_1.join)(netlifyConfig.build.publish, targetPath);
104
+ const dest = (0, pathe_1.join)(publishDir, targetPath);
105
105
  try {
106
106
  await (0, fs_extra_1.move)(source, dest);
107
107
  }
@@ -193,9 +193,9 @@ const moveStaticPages = async ({ netlifyConfig, target, i18n, basePath, }) => {
193
193
  }
194
194
  }
195
195
  // Write the manifest for use in the serverless functions
196
- await (0, fs_extra_1.writeJson)((0, pathe_1.join)(netlifyConfig.build.publish, 'static-manifest.json'), Object.entries(filesManifest));
196
+ await (0, fs_extra_1.writeJson)((0, pathe_1.join)(distDir, 'static-manifest.json'), Object.entries(filesManifest));
197
197
  if (i18n === null || i18n === void 0 ? void 0 : i18n.defaultLocale) {
198
- const rootPath = basePath ? (0, pathe_1.join)(netlifyConfig.build.publish, basePath) : netlifyConfig.build.publish;
198
+ const rootPath = basePath ? (0, pathe_1.join)(publishDir, basePath) : publishDir;
199
199
  // Copy the default locale into the root
200
200
  const defaultLocaleDir = (0, pathe_1.join)(rootPath, i18n.defaultLocale);
201
201
  if ((0, fs_extra_1.existsSync)(defaultLocaleDir)) {
@@ -358,7 +358,8 @@ const unpatchNextFiles = async (root) => {
358
358
  }
359
359
  };
360
360
  exports.unpatchNextFiles = unpatchNextFiles;
361
- const movePublicFiles = async ({ appDir, outdir, publish, }) => {
361
+ const movePublicFiles = async ({ appDir, outdir, publishDir, }) => {
362
+ await (0, fs_extra_1.ensureDir)(publishDir);
362
363
  // `outdir` is a config property added when using Next.js with Nx. It's typically
363
364
  // a relative path outside of the appDir, e.g. '../../dist/apps/<app-name>', and
364
365
  // the parent directory of the .next directory.
@@ -367,7 +368,7 @@ const movePublicFiles = async ({ appDir, outdir, publish, }) => {
367
368
  // directory from the original app directory.
368
369
  const publicDir = outdir ? (0, pathe_1.join)(appDir, outdir, 'public') : (0, pathe_1.join)(appDir, 'public');
369
370
  if ((0, fs_extra_1.existsSync)(publicDir)) {
370
- await (0, fs_extra_1.copy)(publicDir, `${publish}/`);
371
+ await (0, fs_extra_1.copy)(publicDir, `${publishDir}/`);
371
372
  }
372
373
  };
373
374
  exports.movePublicFiles = movePublicFiles;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generateRedirects = exports.generateStaticRedirects = void 0;
3
+ exports.generateRedirects = void 0;
4
4
  const chalk_1 = require("chalk");
5
5
  const fs_extra_1 = require("fs-extra");
6
6
  const outdent_1 = require("outdent");
@@ -9,12 +9,6 @@ const constants_1 = require("../constants");
9
9
  const files_1 = require("./files");
10
10
  const utils_1 = require("./utils");
11
11
  const matchesMiddleware = (middleware, route) => middleware.some((middlewarePath) => route.startsWith(middlewarePath));
12
- const generateHiddenPathRedirects = ({ basePath }) => constants_1.HIDDEN_PATHS.map((path) => ({
13
- from: `${basePath}${path}`,
14
- to: '/404.html',
15
- status: 404,
16
- force: true,
17
- }));
18
12
  const generateLocaleRedirects = ({ i18n, basePath, trailingSlash, }) => {
19
13
  const redirects = [];
20
14
  // If the cookie is set, we need to redirect at the origin
@@ -43,14 +37,6 @@ const generateLocaleRedirects = ({ i18n, basePath, trailingSlash, }) => {
43
37
  });
44
38
  return redirects;
45
39
  };
46
- const generateStaticRedirects = ({ netlifyConfig, nextConfig: { i18n, basePath }, }) => {
47
- // Static files are in `static`
48
- netlifyConfig.redirects.push({ from: `${basePath}/_next/static/*`, to: `/static/:splat`, status: 200 });
49
- if (i18n) {
50
- netlifyConfig.redirects.push({ from: `${basePath}/:locale/_next/static/*`, to: `/static/:splat`, status: 200 });
51
- }
52
- };
53
- exports.generateStaticRedirects = generateStaticRedirects;
54
40
  /**
55
41
  * Routes that match middleware need to always use the SSR function
56
42
  * This generates a rewrite for every middleware in every locale, both with and without a splat
@@ -155,7 +141,6 @@ const generateDynamicRewrites = ({ dynamicRoutes, prerenderedDynamicRoutes, midd
155
141
  const generateRedirects = async ({ netlifyConfig, nextConfig: { i18n, basePath, trailingSlash, appDir }, buildId, apiRoutes, }) => {
156
142
  const { dynamicRoutes: prerenderedDynamicRoutes, routes: prerenderedStaticRoutes } = await (0, fs_extra_1.readJSON)((0, pathe_1.join)(netlifyConfig.build.publish, 'prerender-manifest.json'));
157
143
  const { dynamicRoutes, staticRoutes } = await (0, fs_extra_1.readJSON)((0, pathe_1.join)(netlifyConfig.build.publish, 'routes-manifest.json'));
158
- netlifyConfig.redirects.push(...generateHiddenPathRedirects({ basePath }));
159
144
  if (i18n && i18n.localeDetection !== false) {
160
145
  netlifyConfig.redirects.push(...generateLocaleRedirects({ i18n, basePath, trailingSlash }));
161
146
  }
package/lib/index.js CHANGED
@@ -44,10 +44,14 @@ const plugin = {
44
44
  }
45
45
  const { publish } = netlifyConfig.build;
46
46
  (0, verification_1.checkNextSiteHasBuilt)({ publish, failBuild });
47
- const { appDir, basePath, i18n, images, target, ignore, trailingSlash, outdir, experimental } = await (0, config_1.getNextConfig)({
47
+ const { appDir, basePath, i18n, images, target, ignore, trailingSlash, outdir, experimental, distDir } = await (0, config_1.getNextConfig)({
48
48
  publish,
49
49
  failBuild,
50
50
  });
51
+ const dotNextDir = (0, path_1.join)(appDir, distDir);
52
+ // This is the *generated* publish dir. The user specifies .next, be we actually use this subdirectory
53
+ const publishDir = (0, path_1.join)(dotNextDir, 'dist');
54
+ await (0, fs_extra_1.ensureDir)(publishDir);
51
55
  await (0, edge_1.cleanupEdgeFunctions)(constants);
52
56
  const middlewareManifest = await (0, edge_1.loadMiddlewareManifest)(netlifyConfig);
53
57
  if ((middlewareManifest === null || middlewareManifest === void 0 ? void 0 : middlewareManifest.functions) &&
@@ -69,7 +73,7 @@ const plugin = {
69
73
  `));
70
74
  }
71
75
  if ((0, utils_1.isNextAuthInstalled)()) {
72
- const config = await (0, config_1.getRequiredServerFiles)(publish);
76
+ const config = await (0, config_1.getRequiredServerFiles)(dotNextDir);
73
77
  const userDefinedNextAuthUrl = config.config.env.NEXTAUTH_URL;
74
78
  if (userDefinedNextAuthUrl) {
75
79
  console.log(`NextAuth package detected, NEXTAUTH_URL environment variable set by user in next.config.js to ${userDefinedNextAuthUrl}`);
@@ -79,7 +83,7 @@ const plugin = {
79
83
  const nextAuthUrl = `${process.env.NEXTAUTH_URL}${basePath}`;
80
84
  console.log(`NextAuth package detected, NEXTAUTH_URL environment variable set by user in Netlify configuration to ${nextAuthUrl}`);
81
85
  config.config.env.NEXTAUTH_URL = nextAuthUrl;
82
- await (0, config_1.updateRequiredServerFiles)(publish, config);
86
+ await (0, config_1.updateRequiredServerFiles)(dotNextDir, config);
83
87
  }
84
88
  else {
85
89
  // Using the deploy prime url in production leads to issues because the unique deploy ID is part of the generated URL
@@ -87,23 +91,20 @@ const plugin = {
87
91
  const nextAuthUrl = `${process.env.CONTEXT === 'production' ? process.env.URL : process.env.DEPLOY_PRIME_URL}${basePath}`;
88
92
  console.log(`NextAuth package detected, setting NEXTAUTH_URL environment variable to ${nextAuthUrl}`);
89
93
  config.config.env.NEXTAUTH_URL = nextAuthUrl;
90
- await (0, config_1.updateRequiredServerFiles)(publish, config);
94
+ await (0, config_1.updateRequiredServerFiles)(dotNextDir, config);
91
95
  }
92
96
  }
93
- const buildId = (0, fs_extra_1.readFileSync)((0, path_1.join)(publish, 'BUILD_ID'), 'utf8').trim();
94
- await (0, config_1.configureHandlerFunctions)({ netlifyConfig, ignore, publish: (0, path_1.relative)(process.cwd(), publish) });
95
- const apiRoutes = await (0, functions_1.getExtendedApiRouteConfigs)(publish, appDir);
97
+ const buildId = (0, fs_extra_1.readFileSync)((0, path_1.join)(dotNextDir, 'BUILD_ID'), 'utf8').trim();
98
+ await (0, config_1.configureHandlerFunctions)({ netlifyConfig, ignore, publish: (0, path_1.relative)(process.cwd(), dotNextDir) });
99
+ const apiRoutes = await (0, functions_1.getExtendedApiRouteConfigs)(dotNextDir, appDir);
96
100
  await (0, functions_1.generateFunctions)(constants, appDir, apiRoutes);
97
101
  await (0, functions_1.generatePagesResolver)({ target, constants });
98
- await (0, files_1.movePublicFiles)({ appDir, outdir, publish });
102
+ await (0, files_1.movePublicFiles)({ appDir, outdir, publishDir });
99
103
  await (0, files_1.patchNextFiles)(appDir);
100
104
  if (!(0, destr_1.default)(process.env.SERVE_STATIC_FILES_FROM_ORIGIN)) {
101
- await (0, files_1.moveStaticPages)({ target, netlifyConfig, i18n, basePath });
105
+ await (0, files_1.moveStaticPages)({ distDir: dotNextDir, i18n, basePath, publishDir });
102
106
  }
103
- await (0, redirects_1.generateStaticRedirects)({
104
- netlifyConfig,
105
- nextConfig: { basePath, i18n },
106
- });
107
+ await (0, fs_extra_1.copy)((0, path_1.join)(dotNextDir, 'static'), (0, path_1.join)(publishDir, '_next', 'static'));
107
108
  await (0, functions_1.setupImageFunction)({
108
109
  constants,
109
110
  imageconfig: images,
@@ -120,8 +121,8 @@ const plugin = {
120
121
  });
121
122
  await (0, edge_1.writeEdgeFunctions)(netlifyConfig);
122
123
  },
123
- async onPostBuild({ netlifyConfig: { build: { publish }, redirects, headers, }, utils: { status, cache, functions, build: { failBuild }, }, constants: { FUNCTIONS_DIST }, }) {
124
- await (0, cache_1.saveCache)({ cache, publish });
124
+ async onPostBuild({ netlifyConfig, utils: { status, cache, functions, build: { failBuild }, }, constants: { FUNCTIONS_DIST, PUBLISH_DIR }, }) {
125
+ await (0, cache_1.saveCache)({ cache, publish: netlifyConfig.build.publish });
125
126
  if ((0, utils_1.shouldSkip)()) {
126
127
  status.show({
127
128
  title: 'Next Runtime did not run',
@@ -133,12 +134,13 @@ const plugin = {
133
134
  }
134
135
  await (0, verification_1.checkForOldFunctions)({ functions });
135
136
  await (0, verification_1.checkZipSize)((0, path_1.join)(FUNCTIONS_DIST, `${constants_1.ODB_FUNCTION_NAME}.zip`));
136
- const nextConfig = await (0, config_1.getNextConfig)({ publish, failBuild });
137
+ const nextConfig = await (0, config_1.getNextConfig)({ publish: netlifyConfig.build.publish, failBuild });
137
138
  const { basePath, appDir } = nextConfig;
138
- (0, config_1.generateCustomHeaders)(nextConfig, headers);
139
- (0, verification_1.warnForProblematicUserRewrites)({ basePath, redirects });
139
+ (0, config_1.generateCustomHeaders)(nextConfig, netlifyConfig.headers);
140
+ (0, verification_1.warnForProblematicUserRewrites)({ basePath, redirects: netlifyConfig.redirects });
140
141
  (0, verification_1.warnForRootRedirects)({ appDir });
141
142
  await (0, functions_1.warnOnApiRoutes)({ FUNCTIONS_DIST });
143
+ netlifyConfig.build.publish = (0, path_1.join)(PUBLISH_DIR, 'dist');
142
144
  },
143
145
  };
144
146
  // The types haven't been updated yet
@@ -92,6 +92,13 @@ const makeHandler = (conf, app, pageRoot, staticManifest = [], mode = 'ssr') =>
92
92
  const { headers, ...result } = await getBridge(event).launcher(event, context);
93
93
  // Convert all headers to multiValueHeaders
94
94
  const multiValueHeaders = getMultiValueHeaders(headers);
95
+ if (event.headers['x-next-debug-logging']) {
96
+ const response = {
97
+ headers: multiValueHeaders,
98
+ statusCode: result.statusCode,
99
+ };
100
+ console.log('Origin response:', JSON.stringify(response, null, 2));
101
+ }
95
102
  if ((_b = (_a = multiValueHeaders['set-cookie']) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.includes('__prerender_bypass')) {
96
103
  delete multiValueHeaders.etag;
97
104
  multiValueHeaders['cache-control'] = ['no-cache'];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/plugin-nextjs",
3
- "version": "4.28.6",
3
+ "version": "4.29.0",
4
4
  "description": "Run Next.js seamlessly on Netlify",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -36,7 +36,7 @@
36
36
  },
37
37
  "devDependencies": {
38
38
  "@delucis/if-env": "^1.1.2",
39
- "@netlify/build": "^28.1.9",
39
+ "@netlify/build": "^28.1.13",
40
40
  "@types/fs-extra": "^9.0.13",
41
41
  "@types/jest": "^27.4.1",
42
42
  "@types/merge-stream": "^1.1.2",