@@ -4,7 +4,7 @@ import { resolve, join } from 'path'
44import type { NetlifyConfig , NetlifyPluginConstants } from '@netlify/build'
55import { greenBright } from 'chalk'
66import 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'
88import type { PrerenderManifest } from 'next/dist/build'
99import type { MiddlewareManifest } from 'next/dist/build/webpack/plugins/middleware-plugin'
1010import type { RouteHas } from 'next/dist/lib/load-custom-routes'
@@ -68,7 +68,7 @@ export interface FunctionManifest {
6868
6969const maybeLoadJson = < T > ( path : string ) : Promise < T > | null => {
7070 if ( existsSync ( path ) ) {
71- return readJson ( path )
71+ return readJSON ( path )
7272 }
7373}
7474export const isAppDirRoute = ( route : string , appPathRoutesManifest : Record < string , string > | null ) : boolean =>
@@ -88,60 +88,6 @@ export const loadPrerenderManifest = (netlifyConfig: NetlifyConfig): Promise<Pre
8888 */
8989const sanitizeName = ( name : string ) => `next_${ name . replace ( / \W / g, '_' ) } `
9090
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/std@0.175.0/encoding/base64.ts";
98-
99- import { AsyncLocalStorage } from "https://deno.land/std@0.175.0/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-
14591// Slightly different spacing in different versions!
14692const IMPORT_UNSUPPORTED = [
14793 `Object.defineProperty(globalThis,"__import_unsupported"` ,
@@ -158,7 +104,10 @@ const getMiddlewareBundle = async ({
158104 netlifyConfig : NetlifyConfig
159105} ) : Promise < string > => {
160106 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 ]
162111
163112 chunks . push ( `export const _DEFINITION = ${ JSON . stringify ( edgeFunctionDefinition ) } ` )
164113
0 commit comments