@netlify/plugin-nextjs 4.27.3 → 4.28.2-appdir.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.
- package/lib/helpers/edge.js +61 -11
- package/lib/helpers/files.js +10 -8
- package/lib/helpers/functions.js +4 -1
- package/lib/index.js +5 -3
- package/lib/templates/getApiHandler.js +1 -1
- package/lib/templates/getHandler.js +5 -7
- package/lib/templates/getPageResolver.js +13 -8
- package/lib/templates/handlerUtils.js +14 -7
- package/package.json +3 -3
- package/src/templates/edge/bundle.js +1 -1
- package/src/templates/edge/runtime.ts +13 -7
- package/src/templates/edge-shared/utils.ts +4 -1
package/lib/helpers/edge.js
CHANGED
|
@@ -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.writeEdgeFunctions = exports.writeDevEdgeFunction = exports.cleanupEdgeFunctions = exports.loadMiddlewareManifest = void 0;
|
|
6
|
+
exports.writeEdgeFunctions = exports.writeDevEdgeFunction = exports.cleanupEdgeFunctions = exports.loadAppPathRoutesManifest = exports.loadMiddlewareManifest = void 0;
|
|
7
7
|
/* eslint-disable max-lines */
|
|
8
8
|
const fs_1 = require("fs");
|
|
9
9
|
const path_1 = require("path");
|
|
@@ -13,14 +13,15 @@ const fs_extra_1 = require("fs-extra");
|
|
|
13
13
|
const outdent_1 = require("outdent");
|
|
14
14
|
const config_1 = require("./config");
|
|
15
15
|
const matchers_1 = require("./matchers");
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
return null;
|
|
16
|
+
const maybeLoadJson = (path) => {
|
|
17
|
+
if ((0, fs_1.existsSync)(path)) {
|
|
18
|
+
return (0, fs_extra_1.readJson)(path);
|
|
20
19
|
}
|
|
21
|
-
return (0, fs_extra_1.readJson)(middlewarePath);
|
|
22
20
|
};
|
|
21
|
+
const loadMiddlewareManifest = (netlifyConfig) => maybeLoadJson((0, path_1.resolve)(netlifyConfig.build.publish, 'server', 'middleware-manifest.json'));
|
|
23
22
|
exports.loadMiddlewareManifest = loadMiddlewareManifest;
|
|
23
|
+
const loadAppPathRoutesManifest = (netlifyConfig) => maybeLoadJson((0, path_1.resolve)(netlifyConfig.build.publish, 'app-path-routes-manifest.json'));
|
|
24
|
+
exports.loadAppPathRoutesManifest = loadAppPathRoutesManifest;
|
|
24
25
|
/**
|
|
25
26
|
* Convert the Next middleware name into a valid Edge Function name
|
|
26
27
|
*/
|
|
@@ -29,13 +30,33 @@ const sanitizeName = (name) => `next_${name.replace(/\W/g, '_')}`;
|
|
|
29
30
|
* Initialization added to the top of the edge function bundle
|
|
30
31
|
*/
|
|
31
32
|
const preamble = /* js */ `
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
import {
|
|
34
|
+
decode as _base64Decode,
|
|
35
|
+
} from "https://deno.land/std@0.159.0/encoding/base64.ts";
|
|
35
36
|
// Deno defines "window", but naughty libraries think this means it's a browser
|
|
36
37
|
delete globalThis.window
|
|
38
|
+
globalThis.process = { env: {...Deno.env.toObject(), NEXT_RUNTIME: 'edge', 'NEXT_PRIVATE_MINIMAL_MODE': '1' } }
|
|
37
39
|
// Next uses "self" as a function-scoped global-like object
|
|
38
40
|
const self = {}
|
|
41
|
+
let _ENTRIES = {}
|
|
42
|
+
|
|
43
|
+
// Next uses blob: urls to refer to local assets, so we need to intercept these
|
|
44
|
+
const _fetch = globalThis.fetch
|
|
45
|
+
const fetch = async (url, init) => {
|
|
46
|
+
try {
|
|
47
|
+
if (typeof url === 'object' && url.href?.startsWith('blob:')) {
|
|
48
|
+
const key = url.href.slice(5)
|
|
49
|
+
if (key in _ASSETS) {
|
|
50
|
+
return new Response(_base64Decode(_ASSETS[key]))
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return await _fetch(url, init)
|
|
54
|
+
} catch (error) {
|
|
55
|
+
console.error(error)
|
|
56
|
+
throw error
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
39
60
|
`;
|
|
40
61
|
// Slightly different spacing in different versions!
|
|
41
62
|
const IMPORT_UNSUPPORTED = [
|
|
@@ -48,6 +69,19 @@ const IMPORT_UNSUPPORTED = [
|
|
|
48
69
|
const getMiddlewareBundle = async ({ edgeFunctionDefinition, netlifyConfig, }) => {
|
|
49
70
|
const { publish } = netlifyConfig.build;
|
|
50
71
|
const chunks = [preamble];
|
|
72
|
+
if ('wasm' in edgeFunctionDefinition) {
|
|
73
|
+
for (const { name, filePath } of edgeFunctionDefinition.wasm) {
|
|
74
|
+
const wasm = await fs_1.promises.readFile((0, path_1.join)(publish, filePath));
|
|
75
|
+
chunks.push(`const ${name} = _base64Decode(${JSON.stringify(wasm.toString('base64'))}).buffer`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if ('assets' in edgeFunctionDefinition) {
|
|
79
|
+
chunks.push(`const _ASSETS = {}`);
|
|
80
|
+
for (const { name, filePath } of edgeFunctionDefinition.assets) {
|
|
81
|
+
const wasm = await fs_1.promises.readFile((0, path_1.join)(publish, filePath));
|
|
82
|
+
chunks.push(`_ASSETS[${JSON.stringify(name)}] = ${JSON.stringify(wasm.toString('base64'))}`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
51
85
|
for (const file of edgeFunctionDefinition.files) {
|
|
52
86
|
const filePath = (0, path_1.join)(publish, file);
|
|
53
87
|
let data = await fs_1.promises.readFile(filePath, 'utf8');
|
|
@@ -62,7 +96,7 @@ const getMiddlewareBundle = async ({ edgeFunctionDefinition, netlifyConfig, }) =
|
|
|
62
96
|
};
|
|
63
97
|
const getEdgeTemplatePath = (file) => (0, path_1.join)(__dirname, '..', '..', 'src', 'templates', 'edge', file);
|
|
64
98
|
const copyEdgeSourceFile = ({ file, target, edgeFunctionDir, }) => fs_1.promises.copyFile(getEdgeTemplatePath(file), (0, path_1.join)(edgeFunctionDir, target !== null && target !== void 0 ? target : file));
|
|
65
|
-
const writeEdgeFunction = async ({ edgeFunctionDefinition, edgeFunctionRoot, netlifyConfig, nextConfig, }) => {
|
|
99
|
+
const writeEdgeFunction = async ({ edgeFunctionDefinition, edgeFunctionRoot, netlifyConfig, pageRegexMap, appPathRoutesManifest = {}, nextConfig, }) => {
|
|
66
100
|
const name = sanitizeName(edgeFunctionDefinition.name);
|
|
67
101
|
const edgeFunctionDir = (0, path_1.join)(edgeFunctionRoot, name);
|
|
68
102
|
const bundle = await getMiddlewareBundle({
|
|
@@ -90,6 +124,14 @@ const writeEdgeFunction = async ({ edgeFunctionDefinition, edgeFunctionRoot, net
|
|
|
90
124
|
else {
|
|
91
125
|
matchers.push(...edgeFunctionDefinition.matchers);
|
|
92
126
|
}
|
|
127
|
+
// If the EF matches a page, it's an app dir page so needs a matcher too
|
|
128
|
+
// The object will be empty if appDir isn't enabled in the Next config
|
|
129
|
+
if (pageRegexMap && edgeFunctionDefinition.page in appPathRoutesManifest) {
|
|
130
|
+
const regexp = pageRegexMap.get(appPathRoutesManifest[edgeFunctionDefinition.page]);
|
|
131
|
+
if (regexp) {
|
|
132
|
+
matchers.push({ regexp });
|
|
133
|
+
}
|
|
134
|
+
}
|
|
93
135
|
await (0, fs_extra_1.writeJson)((0, path_1.join)(edgeFunctionDir, 'matchers.json'), matchers);
|
|
94
136
|
// We add a defintion for each matching path
|
|
95
137
|
return matchers.map((matcher) => {
|
|
@@ -122,7 +164,7 @@ exports.writeDevEdgeFunction = writeDevEdgeFunction;
|
|
|
122
164
|
/**
|
|
123
165
|
* Writes Edge Functions for the Next middleware
|
|
124
166
|
*/
|
|
125
|
-
const writeEdgeFunctions = async (netlifyConfig) => {
|
|
167
|
+
const writeEdgeFunctions = async ({ netlifyConfig, routesManifest, }) => {
|
|
126
168
|
const manifest = {
|
|
127
169
|
functions: [],
|
|
128
170
|
version: 1,
|
|
@@ -169,12 +211,20 @@ const writeEdgeFunctions = async (netlifyConfig) => {
|
|
|
169
211
|
// Older versions of the manifest format don't have the functions field
|
|
170
212
|
// No, the version field was not incremented
|
|
171
213
|
if (typeof middlewareManifest.functions === 'object') {
|
|
214
|
+
// When using the app dir, we also need to check if the EF matches a page
|
|
215
|
+
const appPathRoutesManifest = await (0, exports.loadAppPathRoutesManifest)(netlifyConfig);
|
|
216
|
+
const pageRegexMap = new Map([...(routesManifest.dynamicRoutes || []), ...(routesManifest.staticRoutes || [])].map((route) => [
|
|
217
|
+
route.page,
|
|
218
|
+
route.regex,
|
|
219
|
+
]));
|
|
172
220
|
for (const edgeFunctionDefinition of Object.values(middlewareManifest.functions)) {
|
|
173
221
|
usesEdge = true;
|
|
174
222
|
const functionDefinitions = await writeEdgeFunction({
|
|
175
223
|
edgeFunctionDefinition,
|
|
176
224
|
edgeFunctionRoot,
|
|
177
225
|
netlifyConfig,
|
|
226
|
+
pageRegexMap,
|
|
227
|
+
appPathRoutesManifest,
|
|
178
228
|
nextConfig,
|
|
179
229
|
});
|
|
180
230
|
manifest.functions.push(...functionDefinitions);
|
package/lib/helpers/files.js
CHANGED
|
@@ -70,7 +70,6 @@ exports.getMiddleware = getMiddleware;
|
|
|
70
70
|
const moveStaticPages = async ({ netlifyConfig, target, i18n, basePath, }) => {
|
|
71
71
|
console.log('Moving static page files to serve from CDN...');
|
|
72
72
|
const outputDir = (0, pathe_1.join)(netlifyConfig.build.publish, target === 'server' ? 'server' : 'serverless');
|
|
73
|
-
const root = (0, pathe_1.join)(outputDir, 'pages');
|
|
74
73
|
const buildId = (0, fs_extra_1.readFileSync)((0, pathe_1.join)(netlifyConfig.build.publish, 'BUILD_ID'), 'utf8').trim();
|
|
75
74
|
const dataDir = (0, pathe_1.join)('_next', 'data', buildId);
|
|
76
75
|
await (0, fs_extra_1.ensureDir)((0, pathe_1.join)(netlifyConfig.build.publish, dataDir));
|
|
@@ -92,14 +91,17 @@ const moveStaticPages = async ({ netlifyConfig, target, i18n, basePath, }) => {
|
|
|
92
91
|
}
|
|
93
92
|
}
|
|
94
93
|
});
|
|
95
|
-
|
|
94
|
+
let fileCount = 0;
|
|
96
95
|
const filesManifest = {};
|
|
97
96
|
const moveFile = async (file) => {
|
|
97
|
+
// Strip the initial 'app' or 'pages' directory from the output path
|
|
98
|
+
const pathname = file.split('/').slice(1).join('/');
|
|
99
|
+
// .rsc data files go next to the html file
|
|
98
100
|
const isData = file.endsWith('.json');
|
|
99
|
-
const source = (0, pathe_1.join)(
|
|
100
|
-
const targetFile = isData ? (0, pathe_1.join)(dataDir,
|
|
101
|
+
const source = (0, pathe_1.join)(outputDir, file);
|
|
102
|
+
const targetFile = isData ? (0, pathe_1.join)(dataDir, pathname) : pathname;
|
|
101
103
|
const targetPath = basePath ? (0, pathe_1.join)(basePath, targetFile) : targetFile;
|
|
102
|
-
|
|
104
|
+
fileCount += 1;
|
|
103
105
|
filesManifest[file] = targetPath;
|
|
104
106
|
const dest = (0, pathe_1.join)(netlifyConfig.build.publish, targetPath);
|
|
105
107
|
try {
|
|
@@ -110,8 +112,8 @@ const moveStaticPages = async ({ netlifyConfig, target, i18n, basePath, }) => {
|
|
|
110
112
|
}
|
|
111
113
|
};
|
|
112
114
|
// Move all static files, except error documents and nft manifests
|
|
113
|
-
const pages = await (0, globby_1.default)(['
|
|
114
|
-
cwd:
|
|
115
|
+
const pages = await (0, globby_1.default)(['{app,pages}/**/*.{html,json,rsc}', '!**/(500|404|*.js.nft).{html,json}'], {
|
|
116
|
+
cwd: outputDir,
|
|
115
117
|
dot: true,
|
|
116
118
|
});
|
|
117
119
|
const matchingMiddleware = new Set();
|
|
@@ -149,7 +151,7 @@ const moveStaticPages = async ({ netlifyConfig, target, i18n, basePath, }) => {
|
|
|
149
151
|
return limit(moveFile, filePath);
|
|
150
152
|
});
|
|
151
153
|
await Promise.all(promises);
|
|
152
|
-
console.log(`Moved ${
|
|
154
|
+
console.log(`Moved ${fileCount} files`);
|
|
153
155
|
if (matchedPages.size !== 0) {
|
|
154
156
|
console.log((0, chalk_1.yellowBright)((0, outdent_1.outdent) `
|
|
155
157
|
Skipped moving ${matchedPages.size} ${matchedPages.size === 1 ? 'file because it matches' : 'files because they match'} middleware, so cannot be deployed to the CDN and will be served from the origin instead.
|
package/lib/helpers/functions.js
CHANGED
|
@@ -20,10 +20,13 @@ const utils_1 = require("./utils");
|
|
|
20
20
|
const generateFunctions = async ({ FUNCTIONS_SRC = constants_1.DEFAULT_FUNCTIONS_SRC, INTERNAL_FUNCTIONS_SRC, PUBLISH_DIR }, appDir, apiRoutes) => {
|
|
21
21
|
const publish = (0, pathe_1.resolve)(PUBLISH_DIR);
|
|
22
22
|
const functionsDir = (0, pathe_1.resolve)(INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC);
|
|
23
|
-
console.log({ functionsDir });
|
|
24
23
|
const functionDir = (0, pathe_1.join)(functionsDir, constants_1.HANDLER_FUNCTION_NAME);
|
|
25
24
|
const publishDir = (0, pathe_1.relative)(functionDir, publish);
|
|
26
25
|
for (const { route, config, compiled } of apiRoutes) {
|
|
26
|
+
// Don't write a lambda if the runtime is edge
|
|
27
|
+
if (config.runtime === 'experimental-edge') {
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
27
30
|
const apiHandlerSource = await (0, getApiHandler_1.getApiHandler)({
|
|
28
31
|
page: route,
|
|
29
32
|
config,
|
package/lib/index.js
CHANGED
|
@@ -44,7 +44,7 @@ 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, routesManifest } = await (0, config_1.getNextConfig)({
|
|
48
48
|
publish,
|
|
49
49
|
failBuild,
|
|
50
50
|
});
|
|
@@ -82,7 +82,9 @@ const plugin = {
|
|
|
82
82
|
await (0, config_1.updateRequiredServerFiles)(publish, config);
|
|
83
83
|
}
|
|
84
84
|
else {
|
|
85
|
-
|
|
85
|
+
// Using the deploy prime url in production leads to issues because the unique deploy ID is part of the generated URL
|
|
86
|
+
// and will not match the expected URL in the callback URL of an OAuth application.
|
|
87
|
+
const nextAuthUrl = `${process.env.CONTEXT === 'production' ? process.env.URL : process.env.DEPLOY_PRIME_URL}${basePath}`;
|
|
86
88
|
console.log(`NextAuth package detected, setting NEXTAUTH_URL environment variable to ${nextAuthUrl}`);
|
|
87
89
|
config.config.env.NEXTAUTH_URL = nextAuthUrl;
|
|
88
90
|
await (0, config_1.updateRequiredServerFiles)(publish, config);
|
|
@@ -116,7 +118,7 @@ const plugin = {
|
|
|
116
118
|
buildId,
|
|
117
119
|
apiRoutes,
|
|
118
120
|
});
|
|
119
|
-
await (0, edge_1.writeEdgeFunctions)(netlifyConfig);
|
|
121
|
+
await (0, edge_1.writeEdgeFunctions)({ netlifyConfig, routesManifest });
|
|
120
122
|
},
|
|
121
123
|
async onPostBuild({ netlifyConfig: { build: { publish }, redirects, headers, }, utils: { status, cache, functions, build: { failBuild }, }, constants: { FUNCTIONS_DIST }, }) {
|
|
122
124
|
await (0, cache_1.saveCache)({ cache, publish });
|
|
@@ -107,7 +107,7 @@ const getApiHandler = ({ page, config, publishDir = '../../../.next', appDir = '
|
|
|
107
107
|
const { config } = require("${publishDir}/required-server-files.json")
|
|
108
108
|
let staticManifest
|
|
109
109
|
const path = require("path");
|
|
110
|
-
const pageRoot = path.resolve(path.join(__dirname, "${publishDir}", "
|
|
110
|
+
const pageRoot = path.resolve(path.join(__dirname, "${publishDir}", "server"));
|
|
111
111
|
const handler = (${makeHandler.toString()})(config, "${appDir}", pageRoot, ${JSON.stringify(page)})
|
|
112
112
|
exports.handler = ${config.type === "experimental-scheduled" /* ApiRouteType.SCHEDULED */ ? `schedule(${JSON.stringify(config.schedule)}, handler);` : 'handler'}
|
|
113
113
|
`;
|
|
@@ -15,6 +15,7 @@ const { augmentFsModule, getMaxAge, getMultiValueHeaders, getNextServer } = requ
|
|
|
15
15
|
// eslint-disable-next-line max-params
|
|
16
16
|
const makeHandler = (conf, app, pageRoot, staticManifest = [], mode = 'ssr') => {
|
|
17
17
|
var _a;
|
|
18
|
+
console.log('makeHandler', process.env);
|
|
18
19
|
// Change working directory into the site root, unless using Nx, which moves the
|
|
19
20
|
// dist directory and handles this itself
|
|
20
21
|
const dir = path.resolve(__dirname, app);
|
|
@@ -37,9 +38,7 @@ const makeHandler = (conf, app, pageRoot, staticManifest = [], mode = 'ssr') =>
|
|
|
37
38
|
for (const [key, value] of Object.entries(conf.env)) {
|
|
38
39
|
process.env[key] = String(value);
|
|
39
40
|
}
|
|
40
|
-
|
|
41
|
-
let base;
|
|
42
|
-
augmentFsModule({ promises, staticManifest, pageRoot, getBase: () => base });
|
|
41
|
+
augmentFsModule({ promises, staticManifest, pageRoot });
|
|
43
42
|
// We memoize this because it can be shared between requests, but don't instantiate it until
|
|
44
43
|
// the first request because we need the host and port.
|
|
45
44
|
let bridge;
|
|
@@ -49,9 +48,6 @@ const makeHandler = (conf, app, pageRoot, staticManifest = [], mode = 'ssr') =>
|
|
|
49
48
|
}
|
|
50
49
|
const url = new URL(event.rawUrl);
|
|
51
50
|
const port = Number.parseInt(url.port) || 80;
|
|
52
|
-
const { host } = event.headers;
|
|
53
|
-
const protocol = event.headers['x-forwarded-proto'] || 'http';
|
|
54
|
-
base = `${protocol}://${host}`;
|
|
55
51
|
const NextServer = getNextServer();
|
|
56
52
|
const nextServer = new NextServer({
|
|
57
53
|
conf,
|
|
@@ -76,6 +72,8 @@ const makeHandler = (conf, app, pageRoot, staticManifest = [], mode = 'ssr') =>
|
|
|
76
72
|
};
|
|
77
73
|
return async function handler(event, context) {
|
|
78
74
|
var _a, _b, _c;
|
|
75
|
+
const baseUrl = process.env.CONTEXT === 'production' ? process.env.URL : process.env.DEPLOY_PRIME_URL;
|
|
76
|
+
console.log('Using baseUrl', baseUrl, process.env);
|
|
79
77
|
let requestMode = mode;
|
|
80
78
|
// Ensure that paths are encoded - but don't double-encode them
|
|
81
79
|
event.path = new URL(event.rawUrl).pathname;
|
|
@@ -142,7 +140,7 @@ const getHandler = ({ isODB = false, publishDir = '../../../.next', appDir = '..
|
|
|
142
140
|
staticManifest = require("${publishDir}/static-manifest.json")
|
|
143
141
|
} catch {}
|
|
144
142
|
const path = require("path");
|
|
145
|
-
const pageRoot = path.resolve(path.join(__dirname, "${publishDir}",
|
|
143
|
+
const pageRoot = path.resolve(path.join(__dirname, "${publishDir}", "server"));
|
|
146
144
|
exports.handler = ${isODB
|
|
147
145
|
? `builder((${makeHandler.toString()})(config, "${appDir}", pageRoot, staticManifest, 'odb'));`
|
|
148
146
|
: `(${makeHandler.toString()})(config, "${appDir}", pageRoot, staticManifest, 'ssr');`}
|
|
@@ -5,26 +5,31 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.getSinglePageResolver = exports.getPageResolver = void 0;
|
|
7
7
|
const path_1 = require("path");
|
|
8
|
+
const fs_extra_1 = require("fs-extra");
|
|
9
|
+
const globby_1 = __importDefault(require("globby"));
|
|
8
10
|
const outdent_1 = require("outdent");
|
|
9
11
|
const pathe_1 = require("pathe");
|
|
10
12
|
const slash_1 = __importDefault(require("slash"));
|
|
11
|
-
const tiny_glob_1 = __importDefault(require("tiny-glob"));
|
|
12
13
|
const constants_1 = require("../constants");
|
|
13
14
|
const files_1 = require("../helpers/files");
|
|
14
15
|
// Generate a file full of require.resolve() calls for all the pages in the
|
|
15
16
|
// build. This is used by the nft bundler to find all the pages.
|
|
16
17
|
const getPageResolver = async ({ publish, target }) => {
|
|
17
|
-
const functionDir = path_1.
|
|
18
|
-
const root = path_1.
|
|
19
|
-
const pages = await (0,
|
|
18
|
+
const functionDir = (0, path_1.resolve)((0, path_1.join)('.netlify', 'functions', constants_1.HANDLER_FUNCTION_NAME));
|
|
19
|
+
const root = (0, path_1.resolve)((0, slash_1.default)(publish), target === 'server' ? 'server' : 'serverless');
|
|
20
|
+
const pages = await (0, globby_1.default)('{pages,app}/**/*.js.nft.json', {
|
|
20
21
|
cwd: root,
|
|
21
22
|
dot: true,
|
|
22
23
|
});
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
.
|
|
24
|
+
const dependencies = await Promise.all(pages.map(async (page) => {
|
|
25
|
+
const dir = (0, path_1.dirname)(page);
|
|
26
|
+
const { files } = await (0, fs_extra_1.readJSON)((0, path_1.join)(root, page));
|
|
27
|
+
return files === null || files === void 0 ? void 0 : files.map((file) => (0, path_1.resolve)(root, dir, file));
|
|
28
|
+
}));
|
|
29
|
+
const deduped = [...new Set(dependencies.flat())];
|
|
30
|
+
const pageFiles = deduped.map((file) => `require.resolve('${(0, pathe_1.relative)(functionDir, file)}')`).sort();
|
|
26
31
|
return (0, outdent_1.outdent) `
|
|
27
|
-
// This file is purely to allow nft to know about these pages.
|
|
32
|
+
// This file is purely to allow nft to know about these pages.
|
|
28
33
|
exports.resolvePages = () => {
|
|
29
34
|
try {
|
|
30
35
|
${pageFiles.join('\n ')}
|
|
@@ -20,7 +20,12 @@ const downloadFile = async (url, destination) => {
|
|
|
20
20
|
console.log(`Downloading ${url} to ${destination}`);
|
|
21
21
|
const httpx = url.startsWith('https') ? https_1.default : http_1.default;
|
|
22
22
|
await new Promise((resolve, reject) => {
|
|
23
|
-
const req = httpx.get(url, {
|
|
23
|
+
const req = httpx.get(url, {
|
|
24
|
+
timeout: 10000,
|
|
25
|
+
headers: {
|
|
26
|
+
'x-nf-next-asset-req': 'true',
|
|
27
|
+
},
|
|
28
|
+
}, (response) => {
|
|
24
29
|
if (response.statusCode < 200 || response.statusCode > 299) {
|
|
25
30
|
reject(new Error(`Failed to download ${url}: ${response.statusCode} ${response.statusMessage || ''}`));
|
|
26
31
|
return;
|
|
@@ -76,7 +81,7 @@ exports.getMultiValueHeaders = getMultiValueHeaders;
|
|
|
76
81
|
/**
|
|
77
82
|
* Monkey-patch the fs module to download missing files from the CDN
|
|
78
83
|
*/
|
|
79
|
-
const augmentFsModule = ({ promises, staticManifest, pageRoot,
|
|
84
|
+
const augmentFsModule = ({ promises, staticManifest, pageRoot, }) => {
|
|
80
85
|
// Only do this if we have some static files moved to the CDN
|
|
81
86
|
if (staticManifest.length === 0) {
|
|
82
87
|
return;
|
|
@@ -94,22 +99,24 @@ const augmentFsModule = ({ promises, staticManifest, pageRoot, getBase, }) => {
|
|
|
94
99
|
const statsOrig = promises.stat;
|
|
95
100
|
// ...then money-patch it to see if it's requesting a CDN file
|
|
96
101
|
promises.readFile = (async (file, options) => {
|
|
97
|
-
|
|
102
|
+
// In production use the public URL (e.g. https://example.com). Otherwise use the deploy URL, e.g. https://deploy-preview-123--example.netlify.app
|
|
103
|
+
const baseUrl = process.env.CONTEXT === 'production' ? process.env.URL : process.env.DEPLOY_PRIME_URL;
|
|
104
|
+
console.log('Using baseUrl', baseUrl);
|
|
98
105
|
// We only care about page files
|
|
99
106
|
if (file.startsWith(pageRoot)) {
|
|
100
|
-
// We only want the part after
|
|
107
|
+
// We only want the part after `.next/server/`
|
|
101
108
|
const filePath = file.slice(pageRoot.length + 1);
|
|
102
109
|
// Is it in the CDN and not local?
|
|
103
110
|
if (staticFiles.has(filePath) && !(0, fs_1.existsSync)(file)) {
|
|
104
111
|
// This name is safe to use, because it's one that was already created by Next
|
|
105
112
|
const cacheFile = path_1.default.join(cacheDir, filePath);
|
|
106
|
-
const url = `${
|
|
113
|
+
const url = `${baseUrl}/${staticFiles.get(filePath)}`;
|
|
107
114
|
// If it's already downloading we can wait for it to finish
|
|
108
115
|
if (downloadPromises.has(url)) {
|
|
109
116
|
await downloadPromises.get(url);
|
|
110
117
|
}
|
|
111
118
|
// Have we already cached it? We download every time if running locally to avoid staleness
|
|
112
|
-
if ((!(0, fs_1.existsSync)(cacheFile) || process.env.NETLIFY_DEV) &&
|
|
119
|
+
if ((!(0, fs_1.existsSync)(cacheFile) || process.env.NETLIFY_DEV) && baseUrl) {
|
|
113
120
|
await promises.mkdir(path_1.default.dirname(cacheFile), { recursive: true });
|
|
114
121
|
try {
|
|
115
122
|
// Append the path to our host and we can load it like a regular page
|
|
@@ -130,7 +137,7 @@ const augmentFsModule = ({ promises, staticManifest, pageRoot, getBase, }) => {
|
|
|
130
137
|
promises.stat = ((file, options) => {
|
|
131
138
|
// We only care about page files
|
|
132
139
|
if (file.startsWith(pageRoot)) {
|
|
133
|
-
// We only want the part after `
|
|
140
|
+
// We only want the part after `.next/server`
|
|
134
141
|
const cacheFile = path_1.default.join(cacheDir, file.slice(pageRoot.length + 1));
|
|
135
142
|
if ((0, fs_1.existsSync)(cacheFile)) {
|
|
136
143
|
return statsOrig(cacheFile, options);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@netlify/plugin-nextjs",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.28.2-appdir.0",
|
|
4
4
|
"description": "Run Next.js seamlessly on Netlify",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
@@ -35,12 +35,12 @@
|
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@delucis/if-env": "^1.1.2",
|
|
38
|
-
"@netlify/build": "^28.1.
|
|
38
|
+
"@netlify/build": "^28.1.2",
|
|
39
39
|
"@types/fs-extra": "^9.0.13",
|
|
40
40
|
"@types/jest": "^27.4.1",
|
|
41
41
|
"@types/merge-stream": "^1.1.2",
|
|
42
42
|
"@types/node": "^17.0.25",
|
|
43
|
-
"next": "^
|
|
43
|
+
"next": "^13.0.0",
|
|
44
44
|
"npm-run-all": "^4.1.5",
|
|
45
45
|
"typescript": "^4.6.3"
|
|
46
46
|
},
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
* This placeholder is replaced with the compiled Next.js bundle at build time
|
|
3
3
|
* @param {Object} props
|
|
4
4
|
* @param {import("./runtime.ts").RequestData} props.request
|
|
5
|
-
* @returns {Promise<import("
|
|
5
|
+
* @returns {Promise<import("../edge-shared/utils.ts").FetchEventResult>}
|
|
6
6
|
*/
|
|
7
7
|
export default async ({ request }) => {}
|
|
@@ -13,6 +13,12 @@ export interface FetchEventResult {
|
|
|
13
13
|
waitUntil: Promise<any>
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
export interface I18NConfig {
|
|
17
|
+
defaultLocale: string
|
|
18
|
+
localeDetection?: false
|
|
19
|
+
locales: string[]
|
|
20
|
+
}
|
|
21
|
+
|
|
16
22
|
export interface RequestData {
|
|
17
23
|
geo?: {
|
|
18
24
|
city?: string
|
|
@@ -26,14 +32,14 @@ export interface RequestData {
|
|
|
26
32
|
method: string
|
|
27
33
|
nextConfig?: {
|
|
28
34
|
basePath?: string
|
|
29
|
-
i18n?:
|
|
35
|
+
i18n?: I18NConfig | null
|
|
30
36
|
trailingSlash?: boolean
|
|
31
37
|
}
|
|
32
38
|
page?: {
|
|
33
39
|
name?: string
|
|
34
40
|
params?: { [key: string]: string }
|
|
35
41
|
}
|
|
36
|
-
url:
|
|
42
|
+
url: string
|
|
37
43
|
body?: ReadableStream<Uint8Array>
|
|
38
44
|
}
|
|
39
45
|
|
|
@@ -50,10 +56,10 @@ declare global {
|
|
|
50
56
|
globalThis.NFRequestContextMap ||= new Map()
|
|
51
57
|
|
|
52
58
|
const handler = async (req: Request, context: Context) => {
|
|
53
|
-
if (Deno.env.get('NETLIFY_DEV')) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
59
|
+
// if (Deno.env.get('NETLIFY_DEV')) {
|
|
60
|
+
// // Don't run in dev
|
|
61
|
+
// return
|
|
62
|
+
// }
|
|
57
63
|
|
|
58
64
|
const url = new URL(req.url)
|
|
59
65
|
|
|
@@ -82,7 +88,7 @@ const handler = async (req: Request, context: Context) => {
|
|
|
82
88
|
const request: RequestData = {
|
|
83
89
|
headers: Object.fromEntries(req.headers.entries()),
|
|
84
90
|
geo,
|
|
85
|
-
url,
|
|
91
|
+
url: req.url,
|
|
86
92
|
method: req.method,
|
|
87
93
|
ip: context.ip,
|
|
88
94
|
body: req.body ?? undefined,
|
|
@@ -83,7 +83,10 @@ export const buildResponse = async ({
|
|
|
83
83
|
const transformed = response.dataTransforms.reduce((prev, transform) => {
|
|
84
84
|
return transform(prev)
|
|
85
85
|
}, props)
|
|
86
|
-
|
|
86
|
+
const body = JSON.stringify(transformed)
|
|
87
|
+
const headers = new Headers(response.headers)
|
|
88
|
+
headers.set('content-length', String(body.length))
|
|
89
|
+
return new Response(body, { ...response, headers })
|
|
87
90
|
}
|
|
88
91
|
// This var will hold the contents of the script tag
|
|
89
92
|
let buffer = ''
|