@netlify/plugin-nextjs 4.30.2 → 4.30.3-als.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.
@@ -36,12 +36,18 @@ const preamble = /* js */ `
36
36
  import {
37
37
  decode as _base64Decode,
38
38
  } from "https://deno.land/std@0.159.0/encoding/base64.ts";
39
+
40
+ import { AsyncLocalStorage } from "https://raw.githubusercontent.com/crowlKats/deno_std/asynclocalstorage/node/async_hooks.ts";
41
+
39
42
  // Deno defines "window", but naughty libraries think this means it's a browser
40
43
  delete globalThis.window
41
44
  globalThis.process = { env: {...Deno.env.toObject(), NEXT_RUNTIME: 'edge', 'NEXT_PRIVATE_MINIMAL_MODE': '1' } }
42
45
  globalThis.EdgeRuntime = "netlify-edge"
43
46
  let _ENTRIES = {}
44
47
 
48
+ // Next.js expects this as a global
49
+ globalThis.AsyncLocalStorage = AsyncLocalStorage
50
+
45
51
  // Next.js uses this extension to the Headers API implemented by Cloudflare workerd
46
52
  if(!('getAll' in Headers.prototype)) {
47
53
  Headers.prototype.getAll = function getAll(name) {
@@ -87,6 +93,7 @@ const IMPORT_UNSUPPORTED = [
87
93
  const getMiddlewareBundle = async ({ edgeFunctionDefinition, netlifyConfig, }) => {
88
94
  const { publish } = netlifyConfig.build;
89
95
  const chunks = [preamble];
96
+ chunks.push(`export const _DEFINITION = ${JSON.stringify(edgeFunctionDefinition)}`);
90
97
  if ('wasm' in edgeFunctionDefinition) {
91
98
  for (const { name, filePath } of edgeFunctionDefinition.wasm) {
92
99
  const wasm = await fs_1.promises.readFile((0, path_1.join)(publish, filePath));
@@ -289,13 +296,17 @@ const writeEdgeFunctions = async ({ netlifyConfig, routesManifest, }) => {
289
296
  });
290
297
  manifest.functions.push(...matchers.map((matcher) => middlewareMatcherToEdgeFunctionDefinition(matcher, functionName)));
291
298
  }
299
+ // Functions (i.e. not middleware, but edge SSR and API routes)
292
300
  if (typeof middlewareManifest.functions === 'object') {
293
301
  // When using the app dir, we also need to check if the EF matches a page
294
302
  const appPathRoutesManifest = await (0, exports.loadAppPathRoutesManifest)(netlifyConfig);
303
+ // A map of all route pages to their page regex. This is used for pages dir and appDir.
295
304
  const pageRegexMap = new Map([...(routesManifest.dynamicRoutes || []), ...(routesManifest.staticRoutes || [])].map((route) => [
296
305
  route.page,
297
306
  route.regex,
298
307
  ]));
308
+ // Create a map of pages-dir routes to their data route regex (appDir uses the same route as the HTML)
309
+ const dataRoutesMap = new Map([...(routesManifest.dataRoutes || [])].map((route) => [route.page, route.dataRouteRegex]));
299
310
  for (const edgeFunctionDefinition of Object.values(middlewareManifest.functions)) {
300
311
  usesEdge = true;
301
312
  const functionName = sanitizeName(edgeFunctionDefinition.name);
@@ -317,6 +328,16 @@ const writeEdgeFunctions = async ({ netlifyConfig, routesManifest, }) => {
317
328
  // cache: "manual" is currently experimental, so we restrict it to sites that use experimental appDir
318
329
  cache: usesAppDir ? 'manual' : undefined,
319
330
  });
331
+ // pages-dir page routes also have a data route. If there's a match, add an entry mapping that to the function too
332
+ const dataRoute = dataRoutesMap.get(edgeFunctionDefinition.page);
333
+ if (dataRoute) {
334
+ manifest.functions.push({
335
+ function: functionName,
336
+ name: edgeFunctionDefinition.name,
337
+ pattern: dataRoute,
338
+ cache: usesAppDir ? 'manual' : undefined,
339
+ });
340
+ }
320
341
  }
321
342
  }
322
343
  if (usesEdge) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/plugin-nextjs",
3
- "version": "4.30.2",
3
+ "version": "4.30.3-als.0",
4
4
  "description": "Run Next.js seamlessly on Netlify",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -41,7 +41,7 @@
41
41
  "@types/jest": "^27.4.1",
42
42
  "@types/merge-stream": "^1.1.2",
43
43
  "@types/node": "^17.0.25",
44
- "next": "^13.0.7",
44
+ "next": "^13.1.5",
45
45
  "npm-run-all": "^4.1.5",
46
46
  "typescript": "^4.6.3"
47
47
  },
@@ -1,7 +1,8 @@
1
1
  /**
2
2
  * This placeholder is replaced with the compiled Next.js bundle at build time
3
3
  * @param {Object} props
4
- * @param {import("./runtime.ts").RequestData} props.request
4
+ * @param {import("./function-runtime.ts").RequestData} props.request
5
5
  * @returns {Promise<import("../edge-shared/utils.ts").FetchEventResult>}
6
6
  */
7
7
  export default async ({ request }) => {}
8
+ export const _DEFINITION = { env: [], files: [], page: 'placeholder', name: 'pages_placeholder', matchers: [] }
@@ -1,6 +1,6 @@
1
1
  import type { Context } from 'https://edge.netlify.com'
2
2
  // Available at build time
3
- import edgeFunction from './bundle.js'
3
+ import { _DEFINITION as edgeFunctionDefinition, default as edgeFunction } from './bundle.js'
4
4
  import { buildNextRequest, buildResponse, redirectTrailingSlash } from '../edge-shared/utils.ts'
5
5
  import nextConfig from '../edge-shared/nextConfig.json' assert { type: 'json' }
6
6
 
@@ -11,6 +11,7 @@ const handler = async (req: Request, context: Context) => {
11
11
  return redirect
12
12
  }
13
13
  const request = buildNextRequest(req, context, nextConfig)
14
+ request.headers['x-matched-path'] ||= edgeFunctionDefinition.page
14
15
  try {
15
16
  const result = await edgeFunction({ request })
16
17
  return buildResponse({ result, request: req, context })