@netlify/plugin-nextjs 4.4.3 → 4.6.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.
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.configureHandlerFunctions = exports.getNextConfig = void 0;
6
+ exports.configureHandlerFunctions = exports.updateRequiredServerFiles = exports.getRequiredServerFiles = exports.getNextConfig = void 0;
7
7
  const fs_extra_1 = require("fs-extra");
8
8
  const pathe_1 = require("pathe");
9
9
  const slash_1 = __importDefault(require("slash"));
@@ -26,6 +26,24 @@ const getNextConfig = async function getNextConfig({ publish, failBuild = defaul
26
26
  }
27
27
  };
28
28
  exports.getNextConfig = getNextConfig;
29
+ /**
30
+ * Returns all of the NextJS configuration stored within 'required-server-files.json'
31
+ * To update the configuration within this file, use the 'updateRequiredServerFiles' method.
32
+ */
33
+ const getRequiredServerFiles = async (publish) => {
34
+ const configFile = (0, pathe_1.join)(publish, 'required-server-files.json');
35
+ return await (0, fs_extra_1.readJSON)(configFile);
36
+ };
37
+ exports.getRequiredServerFiles = getRequiredServerFiles;
38
+ /**
39
+ * Writes a modified configuration object to 'required-server-files.json'.
40
+ * To get the full configuration, use the 'getRequiredServerFiles' method.
41
+ */
42
+ const updateRequiredServerFiles = async (publish, modifiedConfig) => {
43
+ const configFile = (0, pathe_1.join)(publish, 'required-server-files.json');
44
+ await (0, fs_extra_1.writeJSON)(configFile, modifiedConfig);
45
+ };
46
+ exports.updateRequiredServerFiles = updateRequiredServerFiles;
29
47
  const resolveModuleRoot = (moduleName) => {
30
48
  try {
31
49
  return (0, pathe_1.dirname)((0, pathe_1.relative)(process.cwd(), require.resolve(`${moduleName}/package.json`, { paths: [process.cwd()] })));
@@ -45,7 +63,7 @@ const configureHandlerFunctions = ({ netlifyConfig, publish, ignore = [] }) => {
45
63
  (_a = netlifyConfig.functions)[functionName] || (_a[functionName] = { included_files: [], external_node_modules: [] });
46
64
  netlifyConfig.functions[functionName].node_bundler = 'nft';
47
65
  (_b = netlifyConfig.functions[functionName]).included_files || (_b.included_files = []);
48
- netlifyConfig.functions[functionName].included_files.push('.env', '.env.local', '.env.production', '.env.production.local', `${publish}/server/**`, `${publish}/serverless/**`, `${publish}/*.json`, `${publish}/BUILD_ID`, `${publish}/static/chunks/webpack-middleware*.js`, `!${publish}/server/**/*.js.nft.json`, ...ignore.map((path) => `!${(0, slash_1.default)(path)}`));
66
+ netlifyConfig.functions[functionName].included_files.push('.env', '.env.local', '.env.production', '.env.production.local', './public/locales/**', './next-i18next.config.js', `${publish}/server/**`, `${publish}/serverless/**`, `${publish}/*.json`, `${publish}/BUILD_ID`, `${publish}/static/chunks/webpack-middleware*.js`, `!${publish}/server/**/*.js.nft.json`, ...ignore.map((path) => `!${(0, slash_1.default)(path)}`));
49
67
  const nextRoot = resolveModuleRoot('next');
50
68
  if (nextRoot) {
51
69
  netlifyConfig.functions[functionName].included_files.push(`!${nextRoot}/dist/server/lib/squoosh/**/*.wasm`, `!${nextRoot}/dist/next-server/server/lib/squoosh/**/*.wasm`, `!${nextRoot}/dist/compiled/webpack/bundle4.js`, `!${nextRoot}/dist/compiled/webpack/bundle5.js`);
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.findModuleFromBase = exports.shouldSkip = exports.getPreviewRewrites = exports.getApiRewrites = exports.redirectsForNextRouteWithData = exports.redirectsForNextRoute = exports.isApiRoute = exports.routeToDataRoute = exports.netlifyRoutesForNextRouteWithData = exports.toNetlifyRoute = void 0;
6
+ exports.isNextAuthInstalled = exports.findModuleFromBase = exports.shouldSkip = exports.getPreviewRewrites = exports.getApiRewrites = exports.redirectsForNextRouteWithData = exports.redirectsForNextRoute = exports.isApiRoute = exports.routeToDataRoute = exports.netlifyRoutesForNextRouteWithData = exports.toNetlifyRoute = void 0;
7
7
  const globby_1 = __importDefault(require("globby"));
8
8
  const pathe_1 = require("pathe");
9
9
  const constants_1 = require("../constants");
@@ -134,3 +134,15 @@ const findModuleFromBase = ({ paths, candidates }) => {
134
134
  return null;
135
135
  };
136
136
  exports.findModuleFromBase = findModuleFromBase;
137
+ const isNextAuthInstalled = () => {
138
+ try {
139
+ // eslint-disable-next-line import/no-unassigned-import, import/no-unresolved, node/no-missing-require
140
+ require('next-auth');
141
+ return true;
142
+ }
143
+ catch {
144
+ // Ignore the MODULE_NOT_FOUND error
145
+ return false;
146
+ }
147
+ };
148
+ exports.isNextAuthInstalled = isNextAuthInstalled;
package/lib/index.js CHANGED
@@ -42,6 +42,12 @@ const plugin = {
42
42
  publish,
43
43
  failBuild,
44
44
  });
45
+ if ((0, utils_1.isNextAuthInstalled)()) {
46
+ console.log(`NextAuth package detected, setting NEXTAUTH_URL environment variable to ${process.env.URL}`);
47
+ const config = await (0, config_1.getRequiredServerFiles)(publish);
48
+ config.config.env.NEXTAUTH_URL = process.env.URL;
49
+ await (0, config_1.updateRequiredServerFiles)(publish, config);
50
+ }
45
51
  const buildId = (0, fs_extra_1.readFileSync)((0, path_1.join)(publish, 'BUILD_ID'), 'utf8').trim();
46
52
  (0, config_1.configureHandlerFunctions)({ netlifyConfig, ignore, publish: (0, path_1.relative)(process.cwd(), publish) });
47
53
  await (0, functions_1.generateFunctions)(constants, appDir);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/plugin-nextjs",
3
- "version": "4.4.3",
3
+ "version": "4.6.0",
4
4
  "description": "Run Next.js seamlessly on Netlify",
5
5
  "main": "lib/index.js",
6
6
  "files": [
@@ -28,6 +28,7 @@
28
28
  "tiny-glob": "^0.2.9"
29
29
  },
30
30
  "devDependencies": {
31
+ "@delucis/if-env": "^1.1.2",
31
32
  "@netlify/build": "^27.0.1",
32
33
  "@types/fs-extra": "^9.0.13",
33
34
  "@types/jest": "^27.4.1",
@@ -5,6 +5,19 @@ export interface FetchEventResult {
5
5
  waitUntil: Promise<any>
6
6
  }
7
7
 
8
+ /**
9
+ * This is how Next handles rewritten URLs.
10
+ */
11
+ export function relativizeURL(url: string | string, base: string | URL) {
12
+ const baseURL = typeof base === 'string' ? new URL(base) : base
13
+ const relative = new URL(url, base)
14
+ const origin = `${baseURL.protocol}//${baseURL.host}`
15
+ return `${relative.protocol}//${relative.host}` === origin
16
+ ? relative.toString().replace(origin, '')
17
+ : relative.toString()
18
+ }
19
+
20
+
8
21
  export const addMiddlewareHeaders = async (
9
22
  originResponse: Promise<Response> | Response,
10
23
  middlewareResponse: Response,
@@ -14,7 +27,8 @@ export const addMiddlewareHeaders = async (
14
27
  return originResponse
15
28
  }
16
29
  // We need to await the response to get the origin headers, then we can add the ones from middleware.
17
- const response = await originResponse
30
+ const res = await originResponse
31
+ const response = new Response(res.body, res)
18
32
  middlewareResponse.headers.forEach((value, key) => {
19
33
  response.headers.set(key, value)
20
34
  })
@@ -33,6 +47,14 @@ export const buildResponse = async ({
33
47
  request.headers.set('x-nf-next-middleware', 'skip')
34
48
  const rewrite = res.headers.get('x-middleware-rewrite')
35
49
  if (rewrite) {
50
+ const rewriteUrl = new URL(rewrite, request.url)
51
+ const baseUrl = new URL(request.url)
52
+ if(rewriteUrl.hostname !== baseUrl.hostname) {
53
+ // Netlify Edge Functions don't support proxying to external domains, but Next middleware does
54
+ const proxied = fetch(new Request(rewriteUrl.toString(), request))
55
+ return addMiddlewareHeaders(proxied, res)
56
+ }
57
+ res.headers.set('x-middleware-rewrite', relativizeURL(rewrite, request.url))
36
58
  return addMiddlewareHeaders(context.rewrite(rewrite), res)
37
59
  }
38
60
  if (res.headers.get('x-middleware-next') === '1') {