Files
supabase/apps/kb/astro.config.mjs
Sean Oliver 845ed5a1e3 feat(kb): add cookie consent banner (#52342)
## Problem

kb has no cookie consent banner, so PostHog pageviews have to wait. Part
of GROWTH-1352.

## Solution

- `ConsentIsland` mounts the www/docs consent toast as a `client:only`
island.
- Shared consent code reads `process.env.NEXT_PUBLIC_*`, which Vite
doesn't provide in the browser, so `astro.config.mjs` defines it for the
client only.
- `consent.tsx`, `ConsentToast`, and `PrivacySettings` import specific
`common` modules instead of the barrel, which cuts the island from 652
KB to 96 KB.
- A footer Privacy Settings link opens the same dialog, like docs.

<img width="400" height="150" alt="kb-consent-banner"
src="https://github.com/user-attachments/assets/c32a57ce-89e9-4842-89e4-67d03dafcd7c"
/>

kb previews auto-accept consent like www and docs
(`NEXT_PUBLIC_ENVIRONMENT=staging`). Production needs
`NEXT_PUBLIC_IS_PLATFORM` and `NEXT_PUBLIC_USERCENTRICS_RULESET_ID`
before the banner shows.

Testing: on `astro dev` (toast forced on), the banner renders, and
Privacy settings opens from both the toast and the footer. Accept and
opt out are unverified until production.

## Review instructions

1. The kb, www, and docs previews load with no console errors.
2. On the kb preview, the footer Privacy Settings link opens the dialog.

## Checklist

Check all before review:

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [x] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
2026-10-27 15:36:15 -07:00

83 lines
2.8 KiB
JavaScript

// @ts-check
import { createRequire } from 'node:module'
import path from 'node:path'
import { unified } from '@astrojs/markdown-remark'
import mdx from '@astrojs/mdx'
import react from '@astrojs/react'
import sitemap from '@astrojs/sitemap'
import tailwindcss from '@tailwindcss/vite'
import { defineConfig } from 'astro/config'
import { BASE_PATH } from './src/lib/constants.ts'
import rehypeAdmonitions from './src/lib/mdx/rehype-admonitions.js'
import { sharedCodeBlock } from './src/lib/mdx/shiki-code-block.ts'
import { codeBlockTheme } from 'ui-patterns/CodeBlock/CodeBlock.theme'
// Absolute dir of lodash-es, for the lodash alias below (same fix
// apps/studio/vite.config.ts uses). `packages/ui`'s clipboard util does
// `import { noop } from 'lodash'` — under Vite's dev-mode SSR module runner,
// that CJS import evaluates as pure ESM (no module/exports/require), so the
// named import binds to undefined. lodash-es is real ESM and the same
// version, so the alias fixes the import without changes to the shared `ui`
// package. The client build also uses the alias, so the code block controls
// island includes only the lodash code that it uses.
const lodashEsDir = path.dirname(createRequire(import.meta.url).resolve('lodash-es/package.json'))
// Not typed as `import('vite').Plugin` — kb's own `vite` (from the pnpm
// catalog) and Astro's internal vite dependency resolve to distinct type
// instances, and comparing them blows TS's structural-comparison stack depth.
const lodashEs = {
name: 'kb-lodash-es',
enforce: 'pre',
/**
* @param {string} source
*/
resolveId(source) {
if (source === 'lodash') return path.join(lodashEsDir, 'lodash.js')
const subpath = source.match(/^lodash\/(.+?)(\.js)?$/)
if (subpath) return path.join(lodashEsDir, `${subpath[1]}.js`)
},
}
const nextPublicEnv = Object.fromEntries(
Object.entries(process.env).filter(([key]) => key.startsWith('NEXT_PUBLIC_'))
)
// https://astro.build/config
export default defineConfig({
site: 'https://supabase.com',
base: BASE_PATH,
trailingSlash: 'never',
integrations: [
react(), // rehype-admonitions is a custom rehype plugin and Astro's default pipeline is
// satteri, so .mdx gets its own unified processor. Plain .md stays on satteri.
mdx({
processor: unified({
rehypePlugins: [rehypeAdmonitions],
}),
}),
sitemap(),
],
vite: {
// Client only: a top-level `define` would also replace `process.env` in server code that reads SUPABASE_SERVICE_ROLE_KEY.
environments: {
client: {
define: {
'process.env': JSON.stringify(nextPublicEnv),
},
},
},
ssr: {
noExternal: ['lodash'],
},
plugins: [tailwindcss(), lodashEs],
logLevel: 'error',
},
markdown: {
shikiConfig: {
theme: codeBlockTheme,
transformers: [sharedCodeBlock],
},
},
})