import { isDevelopmentHost, noindexDevelopmentHosts } from '../shared/developmentHosts.js' import { readEnv } from '../shared/envName.js' /** * Whole-site noindex for development, staging, preview and test copies. Then robots.txt disallows everything, * every page gets `robots: noindex, nofollow`, buildSecurityHeaders adds `X-Robots-Tag: noindex, nofollow` to * every response (files and API too), and IndexNow sends nothing. * * Two ways in: * - `IAPL_NOINDEX=1` in that environment's env (the old spelling IPAL_NOINDEX is still read); * - by host, with no env: the site's address (`NEXT_PUBLIC_SERVER_URL`) matches a development pattern — * `iapl({ developmentHosts })`, built in `*.dev.netscale.pl`. A development copy indexed by Google is a * duplicate of the real site, and nothing else keeps it out. * `IAPL_NOINDEX=0` (or `false`, `no`, `off`) says otherwise for one environment; `iapl({ developmentNoindex: * false })` switches the host rule off for the project. Production never sets the variable and is on no * development host. */ /** The variable's name before 1.17.0. Still read; kept under this constant so `process.env[SITE_NOINDEX_ENV]` in a project means what it meant. */ export const SITE_NOINDEX_ENV = 'IPAL_NOINDEX' /** The variable's name since 1.17.0. Read it with `isSiteNoindex()` or `readEnv(IAPL_SITE_NOINDEX_ENV)`, which fall back to the old one. */ export const IAPL_SITE_NOINDEX_ENV = 'IAPL_NOINDEX' /** What the variable says: `true` (1 / true / yes), `false` (0 / false / no / off), or `undefined` when it says nothing. */ export function noindexEnv(): boolean | undefined { const v = readEnv(IAPL_SITE_NOINDEX_ENV)?.trim().toLowerCase() if (v === '1' || v === 'true' || v === 'yes') {return true} if (v === '0' || v === 'false' || v === 'no' || v === 'off') {return false} return undefined } export type SiteNoindexArgs = { /** The development patterns to go by; `false` = the variable alone decides. Default: the plugin's list. */ developmentHosts?: false | readonly string[] /** The site's address or host. Default: `NEXT_PUBLIC_SERVER_URL`. */ host?: null | string } /** Which signal makes the site noindex: the variable, its development host, or nothing. */ export function siteNoindexSignal(args: SiteNoindexArgs = {}): 'development-host' | 'env' | null { const env = noindexEnv() if (env !== undefined) {return env ? 'env' : null} const patterns = args.developmentHosts ?? noindexDevelopmentHosts() if (patterns === false) {return null} return isDevelopmentHost(args.host || process.env.NEXT_PUBLIC_SERVER_URL, patterns) ? 'development-host' : null } export function isSiteNoindex(args: SiteNoindexArgs = {}): boolean { return siteNoindexSignal(args) !== null }