Skip to content

Commit 01f44e3

Browse files
taty2010nickytonline
authored andcommitted
updated tsconfig and added dynamic imports
1 parent 407da47 commit 01f44e3

18 files changed

+152
-132
lines changed

package-lock.json

+14-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/next/src/middleware/request.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Context } from '@netlify/edge-functions'
1+
// import type { Context } from '@netlify/edge-functions'
22
import type { NextURL } from 'next/dist/server/web/next-url'
33
import type { NextRequest as InternalNextRequest } from 'next/server'
44

@@ -23,7 +23,7 @@ export interface NextOptions {
2323
* Supercharge your Next middleware with Netlify Edge Functions
2424
*/
2525
export class MiddlewareRequest extends Request {
26-
context: Context
26+
context: any
2727
originalRequest: Request
2828

2929
constructor(private nextRequest: NextRequest) {

packages/runtime/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"@vercel/node-bridge": "^2.1.0",
1919
"chalk": "^4.1.2",
2020
"chokidar": "^3.5.3",
21-
"destr": "^1.1.1",
21+
"destr": "^2.0.1",
2222
"execa": "^5.1.1",
2323
"follow-redirects": "^1.15.2",
2424
"fs-extra": "^10.0.0",

packages/runtime/src/constants.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import destr from 'destr'
2-
31
export const HANDLER_FUNCTION_NAME = '___netlify-handler'
42
export const ODB_FUNCTION_NAME = '___netlify-odb-handler'
53
export const API_FUNCTION_NAME = '___netlify-api-handler'
@@ -10,8 +8,10 @@ export const HANDLER_FUNCTION_TITLE = 'Next.js SSR handler'
108
export const ODB_FUNCTION_TITLE = 'Next.js ISR handler'
119
export const API_FUNCTION_TITLE = 'Next.js API handler'
1210
export const IMAGE_FUNCTION_TITLE = 'next/image handler'
13-
// These are paths in .next that shouldn't be publicly accessible
14-
export const HIDDEN_PATHS = destr(process.env.NEXT_KEEP_METADATA_FILES)
11+
12+
const hiddenPaths = async() => {
13+
const destr = await import('destr')
14+
const paths = destr.destr(process.env.NEXT_KEEP_METADATA_FILES)
1515
? []
1616
: [
1717
'/cache',
@@ -34,6 +34,11 @@ export const HIDDEN_PATHS = destr(process.env.NEXT_KEEP_METADATA_FILES)
3434
'/required-server-files.json',
3535
'/static-manifest.json',
3636
]
37+
return paths
38+
}
39+
40+
// These are paths in .next that shouldn't be publicly accessible
41+
export const HIDDEN_PATHS = hiddenPaths()
3742

3843
export const ODB_FUNCTION_PATH = `/.netlify/builders/${ODB_FUNCTION_NAME}`
3944
export const HANDLER_FUNCTION_PATH = `/.netlify/functions/${HANDLER_FUNCTION_NAME}`

packages/runtime/src/helpers/config.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { NetlifyConfig } from '@netlify/build'
2-
import destr from 'destr'
32
import { readJSON, writeJSON } from 'fs-extra'
43
import type { Header } from 'next/dist/lib/load-custom-routes'
54
import type { NextConfigComplete } from 'next/dist/server/config-shared'
@@ -115,13 +114,13 @@ export const configureHandlerFunctions = async ({
115114
ignore: Array<string>
116115
apiLambdas: APILambda[]
117116
ssrLambdas: SSRLambda[]
118-
splitApiRoutes: boolean
117+
splitApiRoutes: any
119118
}) => {
120119
const config = await getRequiredServerFiles(publish)
121120
const files = config.files || []
122121
const cssFilesToInclude = files.filter((f) => f.startsWith(`${publish}/static/css/`))
123-
124-
if (!destr(process.env.DISABLE_IPX)) {
122+
const destr = await import('destr')
123+
if (!destr.destr(process.env.DISABLE_IPX)) {
125124
netlifyConfig.functions[IMAGE_FUNCTION_NAME] ||= {}
126125
netlifyConfig.functions[IMAGE_FUNCTION_NAME].node_bundler = 'nft'
127126
}

packages/runtime/src/helpers/edge.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { resolve, join } from 'path'
33

44
import type { NetlifyConfig, NetlifyPluginConstants } from '@netlify/build'
55
import { greenBright } from 'chalk'
6-
import destr from 'destr'
76
import { copy, copyFile, emptyDir, ensureDir, readJSON, writeJSON, writeJson } from 'fs-extra'
87
import type { PrerenderManifest } from 'next/dist/build'
98
import type { MiddlewareManifest } from 'next/dist/build/webpack/plugins/middleware-plugin'
@@ -381,9 +380,9 @@ export const writeEdgeFunctions = async ({
381380
await copy(getEdgeTemplatePath('../edge-shared'), join(edgeFunctionRoot, 'edge-shared'))
382381
await writeJSON(join(edgeFunctionRoot, 'edge-shared', 'nextConfig.json'), nextConfig)
383382
await copy(join(publish, 'prerender-manifest.json'), join(edgeFunctionRoot, 'edge-shared', 'prerender-manifest.json'))
384-
383+
const destr = await import('destr')
385384
// early return if edge is disabled
386-
if (destr(process.env.NEXT_DISABLE_NETLIFY_EDGE)) {
385+
if (destr.destr(process.env.NEXT_DISABLE_NETLIFY_EDGE)) {
387386
console.log('Environment variable NEXT_DISABLE_NETLIFY_EDGE has been set, skipping Netlify Edge Function creation.')
388387
return
389388
}
@@ -480,9 +479,9 @@ export const writeEdgeFunctions = async ({
480479
}
481480

482481
if (
483-
destr(process.env.NEXT_FORCE_EDGE_IMAGES) &&
484-
!destr(process.env.NEXT_DISABLE_EDGE_IMAGES) &&
485-
!destr(process.env.DISABLE_IPX)
482+
destr.destr(process.env.NEXT_FORCE_EDGE_IMAGES) &&
483+
!destr.destr(process.env.NEXT_DISABLE_EDGE_IMAGES) &&
484+
!destr.destr(process.env.DISABLE_IPX)
486485
) {
487486
usesEdge = true
488487
console.log(

packages/runtime/src/helpers/files.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,8 @@ export const removeMetadataFiles = async (publish: string) => {
363363
// Limit concurrent deletions to number of cpus or 2 if there is only 1
364364
const limit = pLimit(Math.max(2, cpus().length))
365365

366-
const removePromises = HIDDEN_PATHS.map((HIDDEN_PATH) => {
366+
// eslint-disable-next-line unicorn/no-await-expression-member
367+
const removePromises = (await HIDDEN_PATHS).map((HIDDEN_PATH) => {
367368
const pathToDelete = join(publish, HIDDEN_PATH)
368369
return limit(() => remove(pathToDelete))
369370
})

packages/runtime/src/helpers/flags.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import destr from 'destr'
21
import { existsSync } from 'fs-extra'
32
import { join } from 'pathe'
43

@@ -17,8 +16,9 @@ import { join } from 'pathe'
1716
*
1817
* Disabled by default. Can be overriden using the NEXT_SPLIT_API_ROUTES env var.
1918
*/
20-
export const splitApiRoutes = (featureFlags: Record<string, unknown>, publish: string): boolean => {
21-
const isEnabled = destr(process.env.NEXT_SPLIT_API_ROUTES) ?? featureFlags.next_split_api_routes ?? false
19+
export const splitApiRoutes = async (featureFlags: Record<string, unknown>, publish: string) => {
20+
const destr = await import('destr')
21+
const isEnabled = destr.destr(process.env.NEXT_SPLIT_API_ROUTES) ?? featureFlags.next_split_api_routes ?? false
2222

2323
if (isEnabled && !existsSync(join(publish, 'next-server.js.nft.json'))) {
2424
console.warn(
@@ -30,9 +30,10 @@ export const splitApiRoutes = (featureFlags: Record<string, unknown>, publish: s
3030
return isEnabled
3131
}
3232

33-
export const bundleBasedOnNftFiles = (featureFlags: Record<string, unknown>): boolean => {
33+
export const bundleBasedOnNftFiles = async (featureFlags: Record<string, unknown>) => {
34+
const destr = await import('destr')
3435
const isEnabled =
35-
destr(process.env.NEXT_BUNDLE_BASED_ON_NFT_FILES) ?? featureFlags.next_bundle_based_on_nft_files ?? false
36+
destr.destr(process.env.NEXT_BUNDLE_BASED_ON_NFT_FILES) ?? featureFlags.next_bundle_based_on_nft_files ?? false
3637

3738
return isEnabled
3839
}

packages/runtime/src/helpers/functions.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import type { NetlifyConfig, NetlifyPluginConstants } from '@netlify/build'
22
import bridgeFile from '@vercel/node-bridge'
33
import chalk from 'chalk'
4-
import destr from 'destr'
54
import { copyFile, ensureDir, existsSync, readJSON, writeFile, writeJSON, stat } from 'fs-extra'
65
import { PrerenderManifest } from 'next/dist/build'
76
import type { ImageConfigComplete, RemotePattern } from 'next/dist/shared/lib/image-config'
87
import { outdent } from 'outdent'
98
import { join, relative, resolve, dirname, basename, extname } from 'pathe'
109
import glob from 'tiny-glob'
1110

11+
12+
1213
import {
1314
HANDLER_FUNCTION_NAME,
1415
ODB_FUNCTION_NAME,
@@ -21,7 +22,6 @@ import {
2122
API_FUNCTION_NAME,
2223
LAMBDA_WARNING_SIZE,
2324
} from '../constants'
24-
import { setBlobFiles } from '../templates/blobHandler'
2525
import { getApiHandler } from '../templates/getApiHandler'
2626
import { getHandler } from '../templates/getHandler'
2727
import { getResolverForPages, getResolverForSourceFiles } from '../templates/getPageResolver'
@@ -33,6 +33,7 @@ import { writeFunctionConfiguration } from './functionsMetaData'
3333
import { pack } from './pack'
3434
import { ApiRouteType } from './types'
3535
import { getFunctionNameForPage } from './utils'
36+
import { async } from 'node-stream-zip'
3637

3738
export interface RouteConfig {
3839
functionName: string
@@ -211,8 +212,8 @@ export const setupImageFunction = async ({
211212
responseHeaders?: Record<string, string>
212213
}): Promise<void> => {
213214
const imagePath = imageconfig.path || '/_next/image'
214-
215-
if (destr(process.env.DISABLE_IPX)) {
215+
const destr = await import('destr')
216+
if (destr.destr(process.env.DISABLE_IPX)) {
216217
// If no image loader is specified, need to redirect to a 404 page since there's no
217218
// backing loader to serve local site images once deployed to Netlify
218219
if (!IS_LOCAL && imageconfig.loader === 'default') {
@@ -401,13 +402,15 @@ export const getSSRLambdas = async (publish: string, constants): Promise<SSRLamb
401402
const odbRoutes = ssrRoutes
402403

403404
const ssrDependencies = await getSSRDependencies(publish)
405+
const netliBlob = await import('../templates/blobHandler.mjs')
406+
404407
// const testDeps = [
405408
// '/Users/tatyananovell/Documents/next-runtime/demos/default/.next/server/app/blog/nick.rsc',
406409
// '/Users/tatyananovell/Documents/next-runtime/demos/default/.next/server/app/blog/nick.html',
407410
// '/Users/tatyananovell/Documents/next-runtime/demos/default/.next/server/app/blog/sarah.rsc',
408411
// ]
409412
// moving the ssrDeps to the Blob store so we can access them in templates/getHandler
410-
setBlobFiles(constants, ssrDependencies)
413+
await netliBlob.setBlobFiles(constants, ssrDependencies)
411414

412415
return [
413416
{

packages/runtime/src/helpers/redirects.ts

+37-34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { NetlifyConfig } from '@netlify/build'
22
import { yellowBright } from 'chalk'
3-
import destr from 'destr'
43
import { readJSON } from 'fs-extra'
54
import type { NextConfig } from 'next'
65
import type { PrerenderManifest, SsgRoute } from 'next/dist/build'
@@ -212,43 +211,47 @@ export const generateDynamicRewrites = ({
212211
const dynamicRewrites: NetlifyConfig['redirects'] = []
213212
const dynamicRoutesThatMatchMiddleware: Array<string> = []
214213

215-
dynamicRoutes.forEach((route) => {
216-
if (isApiRoute(route.page) || is404Route(route.page, i18n)) {
217-
return
218-
}
219-
if (route.page in prerenderedDynamicRoutes) {
220-
if (matchesMiddleware(middleware, route.page)) {
221-
dynamicRoutesThatMatchMiddleware.push(route.page)
222-
} else if (isAppDirRoute(route.page, appPathRoutes)) {
223-
dynamicRewrites.push(
224-
...redirectsForNextRoute({
225-
route: route.page,
226-
buildId,
227-
basePath,
228-
to: ODB_FUNCTION_PATH,
229-
i18n,
230-
dataRoute: prerenderedDynamicRoutes[route.page].dataRoute,
231-
withData: true,
232-
}),
233-
)
234-
} else if (
235-
prerenderedDynamicRoutes[route.page].fallback === false &&
236-
!is404Isr &&
237-
!destr(process.env.LEGACY_FALLBACK_FALSE)
238-
) {
239-
dynamicRewrites.push(...redirectsForNext404Route({ route: route.page, buildId, basePath, i18n }))
214+
const dynamicR = async() => {
215+
const destr = await import('destr')
216+
dynamicRoutes.forEach((route) => {
217+
if (isApiRoute(route.page) || is404Route(route.page, i18n)) {
218+
return
219+
}
220+
if (route.page in prerenderedDynamicRoutes) {
221+
if (matchesMiddleware(middleware, route.page)) {
222+
dynamicRoutesThatMatchMiddleware.push(route.page)
223+
} else if (isAppDirRoute(route.page, appPathRoutes)) {
224+
dynamicRewrites.push(
225+
...redirectsForNextRoute({
226+
route: route.page,
227+
buildId,
228+
basePath,
229+
to: ODB_FUNCTION_PATH,
230+
i18n,
231+
dataRoute: prerenderedDynamicRoutes[route.page].dataRoute,
232+
withData: true,
233+
}),
234+
)
235+
} else if (
236+
prerenderedDynamicRoutes[route.page].fallback === false &&
237+
!is404Isr &&
238+
!destr.destr(process.env.LEGACY_FALLBACK_FALSE)
239+
) {
240+
dynamicRewrites.push(...redirectsForNext404Route({ route: route.page, buildId, basePath, i18n }))
241+
} else {
242+
dynamicRewrites.push(
243+
...redirectsForNextRoute({ route: route.page, buildId, basePath, to: ODB_FUNCTION_PATH, i18n }),
244+
)
245+
}
240246
} else {
247+
// If the route isn't prerendered, it's SSR
241248
dynamicRewrites.push(
242-
...redirectsForNextRoute({ route: route.page, buildId, basePath, to: ODB_FUNCTION_PATH, i18n }),
249+
...redirectsForNextRoute({ route: route.page, buildId, basePath, to: HANDLER_FUNCTION_PATH, i18n }),
243250
)
244251
}
245-
} else {
246-
// If the route isn't prerendered, it's SSR
247-
dynamicRewrites.push(
248-
...redirectsForNextRoute({ route: route.page, buildId, basePath, to: HANDLER_FUNCTION_PATH, i18n }),
249-
)
250-
}
251-
})
252+
})
253+
}
254+
dynamicR()
252255
return {
253256
dynamicRoutesThatMatchMiddleware,
254257
dynamicRewrites,

packages/runtime/src/helpers/utils.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { NetlifyConfig } from '@netlify/build'
2-
import type { Header } from '@netlify/build/types/config/netlify_config'
1+
import type { NetlifyConfig, Header } from '@netlify/build'
32
import globby from 'globby'
43
import type { ExperimentalConfig } from 'next/dist/server/config-shared'
54
import type { ImageConfigComplete, RemotePattern } from 'next/dist/shared/lib/image-config'

0 commit comments

Comments
 (0)