Skip to content

refactor: use memoize and MemoizeCache from @netlify/dev-utils #7427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 0 additions & 43 deletions src/lib/functions/memoized-build.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file was deleted.

4 changes: 2 additions & 2 deletions src/lib/functions/netlify-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { basename, extname } from 'path'
import { version as nodeVersion } from 'process'

import type { ExtendedRoute, Route } from '@netlify/zip-it-and-ship-it'
import type { MemoizeCache } from '@netlify/dev-utils'
import CronParser from 'cron-parser'
import semver from 'semver'

Expand All @@ -12,7 +13,6 @@ import { type BlobsContextWithEdgeAccess, getBlobsEventProperty } from '../blobs
import type { ServerSettings } from '../../utils/types.js'

import type { BaseBuildResult, InvokeFunctionResult, Runtime } from './runtimes/index.js'
import type { BuildCommandCache } from './memoized-build.js'

export interface InvocationError {
errorMessage: string
Expand Down Expand Up @@ -181,7 +181,7 @@ export default class NetlifyFunction<BuildResult extends BaseBuildResult> {
//
// - `srcFilesDiff`: Files that were added and removed since the last time
// the function was built.
async build({ cache }: { cache?: BuildCommandCache<Record<string, unknown>> }) {
async build({ cache }: { cache?: MemoizeCache<Record<string, unknown>> }) {
const buildFunction = await this.runtime.getBuildFunction({
config: this.config,
directory: this.directory,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/functions/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { basename, extname, isAbsolute, join, resolve } from 'path'
import { env } from 'process'

import { type ListedFunction, listFunctions, type Manifest } from '@netlify/zip-it-and-ship-it'
import type { MemoizeCache } from '@netlify/dev-utils'
import extractZip from 'extract-zip'

import {
Expand All @@ -26,7 +27,6 @@ import type { ServerSettings } from '../../utils/types.js'

import NetlifyFunction from './netlify-function.js'
import runtimes, { type BaseBuildResult } from './runtimes/index.js'
import type { BuildCommandCache } from './memoized-build.js'

export const DEFAULT_FUNCTION_URL_EXPRESSION = /^\/.netlify\/(functions|builders)\/([^/]+).*/
const TYPES_PACKAGE = '@netlify/functions'
Expand Down Expand Up @@ -67,7 +67,7 @@ export class FunctionsRegistry {
*/
private blobsContext: BlobsContextWithEdgeAccess

private buildCommandCache?: BuildCommandCache<Record<string, unknown>>
private buildCommandCache?: MemoizeCache<Record<string, unknown>>
private capabilities: {
backgroundFunctions?: boolean
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/functions/runtimes/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ExtendedRoute, Route } from '@netlify/zip-it-and-ship-it'
import type { MemoizeCache } from '@netlify/dev-utils'

import type { BuildCommandCache } from '../memoized-build.js'
import type NetlifyFunction from '../netlify-function.js'
import type { NormalizedCachedConfigConfig } from '../../../utils/command-helpers.js'

Expand Down Expand Up @@ -37,7 +37,7 @@ export type GetBuildFunctionOpts<BuildResult extends BaseBuildResult> = {
export type BuildFunction<
BuildResult extends BaseBuildResult,
CacheEntry extends Record<string, unknown> = Record<string, unknown>,
> = ({ cache }: { cache?: BuildCommandCache<CacheEntry> }) => Promise<BuildResult>
> = ({ cache }: { cache?: MemoizeCache<CacheEntry> }) => Promise<BuildResult>
export type GetBuildFunction<
BuildResult extends BaseBuildResult,
CacheEntry extends Record<string, unknown> = Record<string, unknown>,
Expand Down
8 changes: 4 additions & 4 deletions src/lib/functions/runtimes/js/builders/zisi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import path from 'path'
import { ARCHIVE_FORMAT, zipFunction, listFunction, type FunctionResult } from '@netlify/zip-it-and-ship-it'
// TODO(serhalp): Export this type from zisi
import type { FeatureFlags } from '@netlify/zip-it-and-ship-it/dist/feature_flags.js'
import { type MemoizeCache, memoize } from '@netlify/dev-utils'
import decache from 'decache'
import { readPackageUp } from 'read-package-up'
import sourceMapSupport from 'source-map-support'
Expand All @@ -13,7 +14,6 @@ import { NETLIFYDEVERR, type NormalizedCachedConfigConfig } from '../../../../..
import { SERVE_FUNCTIONS_FOLDER } from '../../../../../utils/functions/functions.js'
import { getPathInProject } from '../../../../settings.js'
import { type NormalizedFunctionsConfig, normalizeFunctionsConfig } from '../../../config.js'
import { type BuildCommandCache, memoizedBuild } from '../../../memoized-build.js'
import type NetlifyFunction from '../../../netlify-function.js'
import type { BaseBuildResult } from '../../index.js'
import type { JsBuildResult } from '../index.js'
Expand Down Expand Up @@ -46,7 +46,7 @@ const buildFunction = async ({
projectRoot,
targetDirectory,
}: {
cache: BuildCommandCache<FunctionResult>
cache: MemoizeCache<FunctionResult>
config: NormalizedFunctionsConfig
directory?: string | undefined
featureFlags: FeatureFlags
Expand Down Expand Up @@ -84,7 +84,7 @@ const buildFunction = async ({
routes,
runtimeAPIVersion,
schedule,
} = await memoizedBuild({
} = await memoize({
cache,
cacheKey: `zisi-${entryPath}`,
command: async () => {
Expand Down Expand Up @@ -229,7 +229,7 @@ export default async function detectZisiBuilder({

const targetDirectory = await getTargetDirectory({ projectRoot, errorExit })

const build = async ({ cache = {} }: { cache?: BuildCommandCache<FunctionResult> }) =>
const build = async ({ cache = {} }: { cache?: MemoizeCache<FunctionResult> }) =>
buildFunction({
cache,
config: functionsConfig,
Expand Down
Loading