@@ -4,7 +4,7 @@ import { resolve, join } from 'path'
4
4
import type { NetlifyConfig , NetlifyPluginConstants } from '@netlify/build'
5
5
import { greenBright } from 'chalk'
6
6
import destr from 'destr'
7
- import { copy , copyFile , emptyDir , ensureDir , readJSON , readJson , writeJSON , writeJson } from 'fs-extra'
7
+ import { copy , copyFile , emptyDir , ensureDir , readJSON , writeJSON , writeJson } from 'fs-extra'
8
8
import type { PrerenderManifest } from 'next/dist/build'
9
9
import type { MiddlewareManifest } from 'next/dist/build/webpack/plugins/middleware-plugin'
10
10
import type { RouteHas } from 'next/dist/lib/load-custom-routes'
@@ -68,7 +68,7 @@ export interface FunctionManifest {
68
68
69
69
const maybeLoadJson = < T > ( path : string ) : Promise < T > | null => {
70
70
if ( existsSync ( path ) ) {
71
- return readJson ( path )
71
+ return readJSON ( path )
72
72
}
73
73
}
74
74
export const isAppDirRoute = ( route : string , appPathRoutesManifest : Record < string , string > | null ) : boolean =>
@@ -88,60 +88,6 @@ export const loadPrerenderManifest = (netlifyConfig: NetlifyConfig): Promise<Pre
88
88
*/
89
89
const sanitizeName = ( name : string ) => `next_${ name . replace ( / \W / g, '_' ) } `
90
90
91
- /**
92
- * Initialization added to the top of the edge function bundle
93
- */
94
- const preamble = /* js */ `
95
- import {
96
- decode as _base64Decode,
97
- } from "https://deno.land/[email protected] /encoding/base64.ts";
98
-
99
- import { AsyncLocalStorage } from "https://deno.land/[email protected] /node/async_hooks.ts";
100
-
101
- // Deno defines "window", but naughty libraries think this means it's a browser
102
- delete globalThis.window
103
- globalThis.process = { env: {...Deno.env.toObject(), NEXT_RUNTIME: 'edge', 'NEXT_PRIVATE_MINIMAL_MODE': '1' } }
104
- globalThis.EdgeRuntime = "netlify-edge"
105
- let _ENTRIES = {}
106
-
107
- // Next.js expects this as a global
108
- globalThis.AsyncLocalStorage = AsyncLocalStorage
109
-
110
- // Next.js uses this extension to the Headers API implemented by Cloudflare workerd
111
- if(!('getAll' in Headers.prototype)) {
112
- Headers.prototype.getAll = function getAll(name) {
113
- name = name.toLowerCase();
114
- if (name !== "set-cookie") {
115
- throw new Error("Headers.getAll is only supported for Set-Cookie");
116
- }
117
- return [...this.entries()]
118
- .filter(([key]) => key === name)
119
- .map(([, value]) => value);
120
- };
121
- }
122
- // Next uses blob: urls to refer to local assets, so we need to intercept these
123
- const _fetch = globalThis.fetch
124
- const fetch = async (url, init) => {
125
- try {
126
- if (typeof url === 'object' && url.href?.startsWith('blob:')) {
127
- const key = url.href.slice(5)
128
- if (key in _ASSETS) {
129
- return new Response(_base64Decode(_ASSETS[key]))
130
- }
131
- }
132
- return await _fetch(url, init)
133
- } catch (error) {
134
- console.error(error)
135
- throw error
136
- }
137
- }
138
-
139
- // Next edge runtime uses "self" as a function-scoped global-like object, but some of the older polyfills expect it to equal globalThis
140
- // See https://nextjs.org/docs/basic-features/supported-browsers-features#polyfills
141
- const self = { ...globalThis, fetch }
142
-
143
- `
144
-
145
91
// Slightly different spacing in different versions!
146
92
const IMPORT_UNSUPPORTED = [
147
93
`Object.defineProperty(globalThis,"__import_unsupported"` ,
@@ -158,7 +104,10 @@ const getMiddlewareBundle = async ({
158
104
netlifyConfig : NetlifyConfig
159
105
} ) : Promise < string > => {
160
106
const { publish } = netlifyConfig . build
161
- const chunks : Array < string > = [ preamble ]
107
+
108
+ const shims = await fs . readFile ( getEdgeTemplatePath ( 'shims.js' ) , 'utf8' )
109
+
110
+ const chunks : Array < string > = [ shims ]
162
111
163
112
chunks . push ( `export const _DEFINITION = ${ JSON . stringify ( edgeFunctionDefinition ) } ` )
164
113
0 commit comments