@next-community/adapter-vercel 0.0.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/dist/index.js +11211 -0
- package/dist/node-handler.js +260 -0
- package/package.json +38 -0
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var node_handler_exports = {};
|
|
20
|
+
__export(node_handler_exports, {
|
|
21
|
+
getHandlerSource: () => getHandlerSource
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(node_handler_exports);
|
|
24
|
+
const getHandlerSource = (ctx) => `
|
|
25
|
+
require('next/dist/server/node-environment');
|
|
26
|
+
require('next/dist/server/node-polyfill-crypto');
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
// this can fail to install if styled-jsx is not discoverable
|
|
30
|
+
// but this is tolerable as the require-hook is handling edge cases
|
|
31
|
+
require('next/dist/server/require-hook');
|
|
32
|
+
} catch (_) {}
|
|
33
|
+
|
|
34
|
+
process.chdir(__dirname);
|
|
35
|
+
|
|
36
|
+
module.exports = (${ctx.isMiddleware ? () => {
|
|
37
|
+
const path = require("path");
|
|
38
|
+
const relativeDistDir = process.env.__PRIVATE_RELATIVE_DIST_DIR;
|
|
39
|
+
const SYMBOL_FOR_REQ_CONTEXT = Symbol.for("@vercel/request-context");
|
|
40
|
+
function getRequestContext() {
|
|
41
|
+
const fromSymbol = globalThis;
|
|
42
|
+
return fromSymbol[SYMBOL_FOR_REQ_CONTEXT]?.get?.() ?? {};
|
|
43
|
+
}
|
|
44
|
+
return async function handler(request) {
|
|
45
|
+
console.log("middleware handler", request);
|
|
46
|
+
let middlewareHandler = await require("./" + path.posix.join(relativeDistDir, "server", "middleware.js"));
|
|
47
|
+
middlewareHandler = middlewareHandler.handler || middlewareHandler;
|
|
48
|
+
const context = getRequestContext();
|
|
49
|
+
const response = await middlewareHandler(request, {
|
|
50
|
+
waitUntil: context.waitUntil,
|
|
51
|
+
requestMeta: {
|
|
52
|
+
// we use '.' for relative project dir since we process.chdir
|
|
53
|
+
// to the same directory as the handler file so everything is
|
|
54
|
+
// relative to that/project dir
|
|
55
|
+
relativeProjectDir: "."
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
return response;
|
|
59
|
+
};
|
|
60
|
+
} : (() => {
|
|
61
|
+
const path = require("path");
|
|
62
|
+
const relativeDistDir = process.env.__PRIVATE_RELATIVE_DIST_DIR;
|
|
63
|
+
const prerenderFallbackFalseMap = process.env.__PRIVATE_PRERENDER_FALLBACK_MAP;
|
|
64
|
+
const {
|
|
65
|
+
dynamicRoutes: dynamicRoutesRaw,
|
|
66
|
+
staticRoutes: staticRoutesRaw,
|
|
67
|
+
i18n
|
|
68
|
+
} = require("./" + path.posix.join(relativeDistDir, "routes-manifest.json"));
|
|
69
|
+
const hydrateRoutesManifestItem = (item) => {
|
|
70
|
+
return {
|
|
71
|
+
...item,
|
|
72
|
+
regex: new RegExp(item.regex)
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
const dynamicRoutes = dynamicRoutesRaw.map(hydrateRoutesManifestItem);
|
|
76
|
+
const staticRoutes = staticRoutesRaw.map(hydrateRoutesManifestItem);
|
|
77
|
+
let appPathRoutesManifest = {};
|
|
78
|
+
try {
|
|
79
|
+
appPathRoutesManifest = require("./" + path.posix.join(
|
|
80
|
+
relativeDistDir,
|
|
81
|
+
"app-path-routes-manifest.json"
|
|
82
|
+
));
|
|
83
|
+
} catch (_) {
|
|
84
|
+
}
|
|
85
|
+
const inversedAppRoutesManifest = Object.entries(
|
|
86
|
+
appPathRoutesManifest
|
|
87
|
+
).reduce(
|
|
88
|
+
(manifest, [originalKey, normalizedKey]) => {
|
|
89
|
+
manifest[normalizedKey] = originalKey;
|
|
90
|
+
return manifest;
|
|
91
|
+
},
|
|
92
|
+
{}
|
|
93
|
+
);
|
|
94
|
+
function normalizeLocalePath(pathname, locales) {
|
|
95
|
+
if (!locales) return { pathname };
|
|
96
|
+
const lowercasedLocales = locales.map(
|
|
97
|
+
(locale) => locale.toLowerCase()
|
|
98
|
+
);
|
|
99
|
+
const segments = pathname.split("/", 2);
|
|
100
|
+
if (!segments[1]) return { pathname };
|
|
101
|
+
const segment = segments[1].toLowerCase();
|
|
102
|
+
const index = lowercasedLocales.indexOf(segment);
|
|
103
|
+
if (index < 0) return { pathname };
|
|
104
|
+
const detectedLocale = locales[index];
|
|
105
|
+
pathname = pathname.slice(detectedLocale.length + 1) || "/";
|
|
106
|
+
return { pathname, locale: detectedLocale };
|
|
107
|
+
}
|
|
108
|
+
function normalizeDataPath(pathname) {
|
|
109
|
+
if (!(pathname || "/").startsWith("/_next/data")) {
|
|
110
|
+
return pathname;
|
|
111
|
+
}
|
|
112
|
+
pathname = pathname.replace(/\/_next\/data\/[^/]{1,}/, "").replace(/\.json$/, "");
|
|
113
|
+
if (pathname === "/index") {
|
|
114
|
+
return "/";
|
|
115
|
+
}
|
|
116
|
+
return pathname;
|
|
117
|
+
}
|
|
118
|
+
function matchUrlToPage(req, urlPathname) {
|
|
119
|
+
urlPathname = normalizeDataPath(urlPathname);
|
|
120
|
+
console.log("before normalize", urlPathname);
|
|
121
|
+
for (const suffixRegex of [
|
|
122
|
+
/\.segments(\/.*)\.segment\.rsc$/,
|
|
123
|
+
/\.rsc$/
|
|
124
|
+
]) {
|
|
125
|
+
urlPathname = urlPathname.replace(suffixRegex, "");
|
|
126
|
+
}
|
|
127
|
+
const urlPathnameWithLocale = urlPathname;
|
|
128
|
+
const normalizeResult = normalizeLocalePath(
|
|
129
|
+
urlPathname,
|
|
130
|
+
i18n?.locales
|
|
131
|
+
);
|
|
132
|
+
urlPathname = normalizeResult.pathname;
|
|
133
|
+
console.log("after normalize", normalizeResult);
|
|
134
|
+
urlPathname = urlPathname.replace(/\/$/, "") || "/";
|
|
135
|
+
for (const route of [...staticRoutes, ...dynamicRoutes]) {
|
|
136
|
+
if (route.regex.test(urlPathname)) {
|
|
137
|
+
const fallbackFalseMap = prerenderFallbackFalseMap[route.page];
|
|
138
|
+
if (fallbackFalseMap && !(fallbackFalseMap.includes(urlPathname) || fallbackFalseMap.includes(urlPathnameWithLocale))) {
|
|
139
|
+
console.log("fallback: false but not prerendered", {
|
|
140
|
+
page: route.page,
|
|
141
|
+
urlPathname,
|
|
142
|
+
urlPathnameWithLocale,
|
|
143
|
+
paths: Object.values(fallbackFalseMap)
|
|
144
|
+
});
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
console.log("matched route", route, urlPathname);
|
|
148
|
+
return {
|
|
149
|
+
matchedPathname: inversedAppRoutesManifest[route.page] || route.page,
|
|
150
|
+
locale: normalizeResult.locale
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return {
|
|
155
|
+
matchedPathname: inversedAppRoutesManifest[urlPathname] || urlPathname,
|
|
156
|
+
locale: normalizeResult.locale
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
const SYMBOL_FOR_REQ_CONTEXT = Symbol.for("@vercel/request-context");
|
|
160
|
+
function getRequestContext() {
|
|
161
|
+
const fromSymbol = globalThis;
|
|
162
|
+
return fromSymbol[SYMBOL_FOR_REQ_CONTEXT]?.get?.() ?? {};
|
|
163
|
+
}
|
|
164
|
+
const RouterServerContextSymbol = Symbol.for(
|
|
165
|
+
"@next/router-server-methods"
|
|
166
|
+
);
|
|
167
|
+
const routerServerGlobal = globalThis;
|
|
168
|
+
if (!routerServerGlobal[RouterServerContextSymbol]) {
|
|
169
|
+
routerServerGlobal[RouterServerContextSymbol] = {};
|
|
170
|
+
}
|
|
171
|
+
routerServerGlobal[RouterServerContextSymbol]["."] = {
|
|
172
|
+
async render404(req, res) {
|
|
173
|
+
let mod;
|
|
174
|
+
try {
|
|
175
|
+
mod = await require("./" + path.posix.join(
|
|
176
|
+
relativeDistDir,
|
|
177
|
+
"server",
|
|
178
|
+
"pages",
|
|
179
|
+
`404.js`
|
|
180
|
+
));
|
|
181
|
+
console.log("using 404.js for render404");
|
|
182
|
+
} catch (_) {
|
|
183
|
+
mod = await require("./" + path.posix.join(
|
|
184
|
+
relativeDistDir,
|
|
185
|
+
"server",
|
|
186
|
+
"pages",
|
|
187
|
+
`_error.js`
|
|
188
|
+
));
|
|
189
|
+
console.log("using _error for render404");
|
|
190
|
+
}
|
|
191
|
+
res.statusCode = 404;
|
|
192
|
+
if (mod) {
|
|
193
|
+
await mod.handler(req, res, {
|
|
194
|
+
waitUntil: getRequestContext().waitUntil
|
|
195
|
+
});
|
|
196
|
+
} else {
|
|
197
|
+
console.log(
|
|
198
|
+
"failed to find 404 module",
|
|
199
|
+
await require("fs").promises.readdir(
|
|
200
|
+
path.posix.join(relativeDistDir, "server", "pages")
|
|
201
|
+
).catch((err) => err)
|
|
202
|
+
);
|
|
203
|
+
res.end("This page could not be found");
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
return async function handler(req, res) {
|
|
208
|
+
try {
|
|
209
|
+
const parsedUrl = new URL(req.url || "/", "http://n");
|
|
210
|
+
let urlPathname = req.headers["x-matched-path"];
|
|
211
|
+
if (typeof urlPathname !== "string") {
|
|
212
|
+
console.log("no x-matched-path", { url: req.url });
|
|
213
|
+
urlPathname = parsedUrl.pathname || "/";
|
|
214
|
+
}
|
|
215
|
+
const { matchedPathname: page, locale } = matchUrlToPage(
|
|
216
|
+
req,
|
|
217
|
+
urlPathname
|
|
218
|
+
);
|
|
219
|
+
const isAppDir = page.match(/\/(page|route)$/);
|
|
220
|
+
console.log("invoking handler", {
|
|
221
|
+
page,
|
|
222
|
+
url: req.url,
|
|
223
|
+
matchedPath: req.headers["x-matched-path"]
|
|
224
|
+
});
|
|
225
|
+
const mod = await require("./" + path.posix.join(
|
|
226
|
+
relativeDistDir,
|
|
227
|
+
"server",
|
|
228
|
+
isAppDir ? "app" : "pages",
|
|
229
|
+
`${page === "/" ? "index" : page}.js`
|
|
230
|
+
));
|
|
231
|
+
await mod.handler(req, res, {
|
|
232
|
+
waitUntil: getRequestContext().waitUntil,
|
|
233
|
+
requestMeta: {
|
|
234
|
+
minimalMode: true,
|
|
235
|
+
// we use '.' for relative project dir since we process.chdir
|
|
236
|
+
// to the same directory as the handler file so everything is
|
|
237
|
+
// relative to that/project dir
|
|
238
|
+
relativeProjectDir: ".",
|
|
239
|
+
locale
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
} catch (error) {
|
|
243
|
+
console.error(`Failed to handle ${req.url}`, error);
|
|
244
|
+
throw error;
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
}).toString()})()`.replaceAll(
|
|
248
|
+
"process.env.__PRIVATE_RELATIVE_DIST_DIR",
|
|
249
|
+
`"${ctx.projectRelativeDistDir}"`
|
|
250
|
+
).replaceAll(
|
|
251
|
+
"process.env.__PRIVATE_PRERENDER_FALLBACK_MAP",
|
|
252
|
+
JSON.stringify(ctx.prerenderFallbackFalseMap)
|
|
253
|
+
).replaceAll(
|
|
254
|
+
"process.env.__PRIVATE_NEXT_CONFIG",
|
|
255
|
+
JSON.stringify(ctx.nextConfig)
|
|
256
|
+
);
|
|
257
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
258
|
+
0 && (module.exports = {
|
|
259
|
+
getHandlerSource
|
|
260
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@next-community/adapter-vercel",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"type": "commonjs",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"devDependencies": {
|
|
10
|
+
"@types/bytes": "3.1.1",
|
|
11
|
+
"@types/convert-source-map": "1.5.2",
|
|
12
|
+
"@types/fs-extra": "8.0.0",
|
|
13
|
+
"@types/node": "14.18.33",
|
|
14
|
+
"@types/picomatch": "2.3.3",
|
|
15
|
+
"@types/webpack-sources": "3.2.0",
|
|
16
|
+
"@vercel/build-utils": "12.2.2",
|
|
17
|
+
"@vercel/routing-utils": "5.2.1",
|
|
18
|
+
"async-sema": "3.1.1",
|
|
19
|
+
"bytes": "3.1.2",
|
|
20
|
+
"convert-source-map": "1.8.0",
|
|
21
|
+
"esbuild": "0.25.10",
|
|
22
|
+
"fs-extra": "11.2.0",
|
|
23
|
+
"next": "16.1.1-canary.18",
|
|
24
|
+
"picomatch": "4.0.1",
|
|
25
|
+
"source-map": "0.7.4",
|
|
26
|
+
"webpack-sources": "3.2.3"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"next": ">= 16.1.1-canary.18"
|
|
30
|
+
},
|
|
31
|
+
"engines": {
|
|
32
|
+
"node": ">= 20.6.0"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "rm -rf dist; tsc --noEmit && node --experimental-transform-types build.mts",
|
|
36
|
+
"test": "vitest"
|
|
37
|
+
}
|
|
38
|
+
}
|