Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Edge function redirect if slug doesnt exist (#33479)

Joshen Lim3167c1f803e4
diff --git a/apps/studio/components/layouts/FunctionsLayout/FunctionsLayout.tsx b/apps/studio/components/layouts/FunctionsLayout/FunctionsLayout.tsx
index 5a0236642a2..98e96f2e616 100644
--- a/apps/studio/components/layouts/FunctionsLayout/FunctionsLayout.tsx
+++ b/apps/studio/components/layouts/FunctionsLayout/FunctionsLayout.tsx
@@ -1,6 +1,6 @@
import { PermissionAction } from '@supabase/shared-types/out/constants'
import Link from 'next/link'
-import type { PropsWithChildren } from 'react'
+import { useEffect, type PropsWithChildren } from 'react'
import { useParams } from 'common'
import { useIsAPIDocsSidePanelEnabled } from 'components/interfaces/App/FeaturePreview/FeaturePreviewContext'
@@ -12,6 +12,8 @@ import { useEdgeFunctionsQuery } from 'data/edge-functions/edge-functions-query'
import { useCheckPermissions } from 'hooks/misc/useCheckPermissions'
import { withAuth } from 'hooks/misc/withAuth'
import { Code } from 'lucide-react'
+import { useRouter } from 'next/router'
+import { toast } from 'sonner'
import { Button, cn } from 'ui'
import FunctionsNav from '../../interfaces/Functions/FunctionsNav'
import ProjectLayout from '../ProjectLayout/ProjectLayout'
@@ -21,12 +23,36 @@ interface FunctionsLayoutProps {
}
const FunctionsLayout = ({ title, children }: PropsWithChildren<FunctionsLayoutProps>) => {
+ const router = useRouter()
const { functionSlug, ref } = useParams()
const isNewAPIDocsEnabled = useIsAPIDocsSidePanelEnabled()
+
const { data: functions, isLoading } = useEdgeFunctionsQuery({ projectRef: ref })
- const { data: selectedFunction } = useEdgeFunctionQuery({ projectRef: ref, slug: functionSlug })
+ const {
+ data: selectedFunction,
+ error,
+ isError,
+ } = useEdgeFunctionQuery({ projectRef: ref, slug: functionSlug })
const canReadFunctions = useCheckPermissions(PermissionAction.FUNCTIONS_READ, '*')
+
+ const name = selectedFunction?.name || ''
+ const hasFunctions = (functions ?? []).length > 0
+ const centered = !hasFunctions
+
+ useEffect(() => {
+ let cancel = false
+
+ if (!!functionSlug && isError && error.code === 404 && !cancel) {
+ toast('Edge function cannot be found in your project')
+ router.push(`/project/${ref}/functions`)
+ }
+
+ return () => {
+ cancel = true
+ }
+ }, [isError])
+
if (!canReadFunctions) {
return (
<ProjectLayout title={title || 'Edge Functions'} product="Edge Functions">
@@ -35,10 +61,6 @@ const FunctionsLayout = ({ title, children }: PropsWithChildren<FunctionsLayoutP
)
}
- const name = selectedFunction?.name || ''
- const hasFunctions = (functions ?? []).length > 0
- const centered = !hasFunctions
-
return (
<ProjectLayout isLoading={isLoading} title={title || 'Edge Functions'} product="Edge Functions">
{centered ? (
diff --git a/apps/studio/data/edge-functions/edge-function-query.ts b/apps/studio/data/edge-functions/edge-function-query.ts
index ad6f54dee2a..c8d14919498 100644
--- a/apps/studio/data/edge-functions/edge-function-query.ts
+++ b/apps/studio/data/edge-functions/edge-function-query.ts
@@ -2,6 +2,7 @@ import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import { components } from 'api-types'
import { get, handleError } from 'data/fetchers'
import { IS_PLATFORM } from 'lib/constants'
+import { ResponseError } from 'types'
import { edgeFunctionsKeys } from './keys'
export type EdgeFunctionVariables = {
@@ -28,7 +29,7 @@ export async function getEdgeFunction(
}
export type EdgeFunctionData = Awaited<ReturnType<typeof getEdgeFunction>>
-export type EdgeFunctionError = unknown
+export type EdgeFunctionError = ResponseError
export const useEdgeFunctionQuery = <TData = EdgeFunctionData>(
{ projectRef, slug }: EdgeFunctionVariables,