mirror of
https://github.com/supabase/supabase.git
synced 2026-12-03 23:18:57 +00:00
Drops the remaining Next-era shims and aliases that only existed for the dual runtime (FE-3106). Part 14 of the stack. **Depends on:** #53222 **Changed:** - ~45 files – import Sentry from `@sentry/react` directly instead of the aliased `@sentry/nextjs` - `hooks/ui/usePreventNavigationOnUnsavedChanges.ts` – is now the TanStack `useBlocker` implementation (previously swapped in by a Vite alias) - `components/interfaces/App/RouteValidationWrapper.tsx` – `next/router` → `useNavigate` + the leaf route's `fullPath` (`/project/$ref/sql/$id`) for the exempt-route and last-visited checks - `hooks/misc/withAuth.tsx` – no `getLayout` forwarding - `TemplateEditor.test.tsx` – renders inside a router for the unsaved-changes blocker **Removed:** - `compat/sentry-nextjs.ts` and its Vite alias, the `usePreventNavigationOnUnsavedChanges` alias and Next hook, `types/next.ts` (`NextPageWithLayout`), the `@sentry/nextjs` dependency The `compat/next/*` shims for `link`, `router`, `head`, `image`, `navigation`, `dynamic` and `script` stay: about 300 components outside `routes/**` still import them. Addresses [FE-3106](https://linear.app/supabase/issue/FE-3106/move-page-bodies-into-route-files-and-delete-pages). ## To test - Trigger an error (e.g. a failing query) – it reaches Sentry as before - Edit an email template or edge function and navigate away – the unsaved-changes prompt appears - Open an org you don't belong to – toast and redirect home; open a SQL snippet, leave and come back – last snippet is restored --------- Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
64 lines
3.0 KiB
TypeScript
64 lines
3.0 KiB
TypeScript
import {
|
|
sentryGlobalFunctionMiddleware,
|
|
sentryGlobalRequestMiddleware,
|
|
} from '@sentry/tanstackstart-react'
|
|
import { createMiddleware, createStart } from '@tanstack/react-start'
|
|
|
|
import { isCrossOriginRequest } from '@/lib/api/cross-origin'
|
|
import { BASE_PATH, IS_PLATFORM } from '@/lib/constants'
|
|
import { isHostedSupportedApiPath } from '@/lib/hosted-api-allowlist'
|
|
|
|
// Self-hosted-only API routes must 404 in platform (hosted) mode, via a
|
|
// global request middleware using the allowlist in lib/hosted-api-allowlist.ts. On Vercel every `/api/*` request
|
|
// still reaches the function (pages are a static shell), so this covers all
|
|
// API routes from one place.
|
|
|
|
const platformApiGuard = createMiddleware({ type: 'request' }).server(({ request, next }) => {
|
|
const { pathname } = new URL(request.url)
|
|
// Path relative to the configured basePath — mirrors Next's basePath-
|
|
// relative middleware matcher.
|
|
const relativePath =
|
|
BASE_PATH && pathname.startsWith(BASE_PATH) ? pathname.slice(BASE_PATH.length) : pathname
|
|
|
|
if (IS_PLATFORM && relativePath.startsWith('/api/') && !isHostedSupportedApiPath(relativePath)) {
|
|
return Response.json(
|
|
{ success: false, message: 'Endpoint not supported on hosted' },
|
|
{ status: 404 }
|
|
)
|
|
}
|
|
|
|
return next()
|
|
})
|
|
|
|
// Self-hosted Studio has no session of its own, so a page on another site could
|
|
// otherwise submit requests to it with the browser's cached dashboard credentials,
|
|
// or with none at all on a local CLI stack (CSRF). Hosted authenticates with
|
|
// tokens in headers, which browsers don't attach on their own. This covers every
|
|
// server request, including API routes and server functions.
|
|
const crossOriginGuard = createMiddleware({ type: 'request' }).server(({ request, next }) => {
|
|
if (!IS_PLATFORM && isCrossOriginRequest(request)) {
|
|
return Response.json({ message: 'Cross-origin request blocked' }, { status: 403 })
|
|
}
|
|
|
|
return next()
|
|
})
|
|
|
|
// Vercel's standard request pin keeps server functions compatible with the
|
|
// current tab. Documents and the deployment check stay unpinned, so a reload
|
|
// gets the latest version. Vite only defines the ID when protection is enabled.
|
|
const deploymentMiddleware = createMiddleware({ type: 'function' }).client(({ next }) => {
|
|
const deploymentId = process.env.NEXT_PUBLIC_VERCEL_DEPLOYMENT_ID
|
|
return next({ headers: deploymentId ? { 'x-deployment-id': deploymentId } : {} })
|
|
})
|
|
|
|
// Sentry's global middlewares go at the FRONT so they wrap the whole request /
|
|
// server-function lifecycle — including errors that downstream code swallows
|
|
// into a 500, which manual capture calls never see. The SDK's
|
|
// Vite plugin can auto-wrap these arrays instead, but we disable that
|
|
// (`autoInstrumentMiddleware: false` in vite.config.ts) and wire them
|
|
// explicitly so the instrumentation is visible in source.
|
|
export const startInstance = createStart(() => ({
|
|
requestMiddleware: [sentryGlobalRequestMiddleware, platformApiGuard, crossOriginGuard],
|
|
functionMiddleware: [sentryGlobalFunctionMiddleware, deploymentMiddleware],
|
|
}))
|