Skip to content

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

chore: speed up preview builds (#21411)

Charisaa3c896e4839
diff --git a/apps/docs/lib/constants.ts b/apps/docs/lib/constants.ts
index 3aa55d2717d..813e45149b6 100644
--- a/apps/docs/lib/constants.ts
+++ b/apps/docs/lib/constants.ts
@@ -1,4 +1,5 @@
export const IS_PLATFORM = process.env.NEXT_PUBLIC_IS_PLATFORM === 'true'
+export const IS_PREVIEW = process.env.VERCEL_ENV === 'preview'
export const LOCAL_SUPABASE = process.env.NEXT_PUBLIC_LOCAL_SUPABASE === 'true'
export const API_URL = (
process.env.NODE_ENV === 'development'
diff --git a/apps/docs/pages/_app.tsx b/apps/docs/pages/_app.tsx
index 83852f73b22..b1c048b72a4 100644
--- a/apps/docs/pages/_app.tsx
+++ b/apps/docs/pages/_app.tsx
@@ -16,13 +16,38 @@ import { useConsent } from 'ui-patterns/ConsentToast'
import Favicons from '~/components/Favicons'
import { IPv4DeprecationBanner } from '~/components/IPv4DeprecationBanner'
import SiteLayout from '~/layouts/SiteLayout'
-import { IS_PLATFORM } from '~/lib/constants'
+import { IS_PLATFORM, IS_PREVIEW } from '~/lib/constants'
import { unauthedAllowedPost } from '~/lib/fetch/fetchWrappers'
import { useRootQueryClient } from '~/lib/fetch/queryClient'
import { LOCAL_STORAGE_KEYS, remove } from '~/lib/storage'
import { useOnLogout } from '~/lib/userAuth'
import { AppPropsWithLayout } from '~/types'
+/**
+ * Preview builds don't need to be statically generated to optimize performance.
+ * This (somewhat hacky) way of shortcutting preview builds cuts their build
+ * time and speeds up the feedback loop for previewing docs changes in Vercel.
+ *
+ * This technically breaks the Rules of Hooks to avoid an unnecessary full-app
+ * rerender in prod, but this is fine because IS_PREVIEW will never change on
+ * you within a single build.
+ */
+function ShortcutPreviewBuild({ children }: PropsWithChildren) {
+ if (IS_PREVIEW) {
+ // eslint-disable-next-line react-hooks/rules-of-hooks
+ const [isMounted, setIsMounted] = useState(false)
+
+ // eslint-disable-next-line react-hooks/rules-of-hooks
+ useEffect(() => {
+ setIsMounted(true)
+ }, [])
+
+ return isMounted ? children : null
+ }
+
+ return children
+}
+
/**
*
* !!! IMPORTANT !!!
@@ -189,7 +214,7 @@ function MyApp({ Component, pageProps }: AppPropsWithLayout) {
}, [router])
return (
- <>
+ <ShortcutPreviewBuild>
<QueryClientProvider client={queryClient}>
<Favicons />
<AuthContainer>
@@ -210,7 +235,7 @@ function MyApp({ Component, pageProps }: AppPropsWithLayout) {
</SignOutHandler>
</AuthContainer>
</QueryClientProvider>
- </>
+ </ShortcutPreviewBuild>
)
}