withFairGardenIndicators
Adds the rewrites, the redirects, the headers of the
stylesheet indicators, and Accept-CH for the
client hints they read.
withFairGardenIndicators(
nextConfig: NextConfig,
indicators: Indicators,
options?: PluginOptions
): NextConfig
Everything else in the config is returned as it was.
// next.config.ts
import type { NextConfig } from 'next'
import { withFairGardenIndicators } from '@fairgarden/indicators/withFairGardenIndicators'
import { indicators } from './lib/indicators.ts'
const nextConfig: NextConfig = {
rewrites: async () => ({
beforeFiles: [{ source: '/oidc/:path*', destination: '/api/oidc/:path*' }],
}),
}
export default withFairGardenIndicators(nextConfig, indicators)
The app's own beforeFiles rewrites run first, then the chain; the app's
redirects come first too. A bare rewrites array is treated as
afterFiles, as Next does.
The headers are the cache policy of each stylesheet, after the app's own
headers, and on every page a preload of every global stylesheet and an
Accept-CH, before them. Of the headers entries that match a request,
Next keeps the last value for each header. So the cache policy always
applies, and a Link or Accept-CH that the app's own headers set on a
page replaces the plugin's. Such an app should list the stylesheets or
hints in its own header, or turn the plugin's off. The preload headers
React adds while rendering are appended, so they are unaffected.
Options
| Field | Default | Meaning |
|---|---|---|
root | the directory of the calling next.config | where to look for public/ and app/ |
detectExclusions | true | look at the app for paths to leave alone |
hardFlags | {} | route trees gated by a cookie or header. See Hard flags |
preloadStylesheets | true | name the global stylesheets in a Link: rel=preload header on every page; one declared global: false, or reading a client hint the browser sends only when asked, is never preloaded. See Stylesheets |
clientHints | true | ask for the client hints the indicators and hard flags read, with Accept-CH on every page; 'critical' also names the path's in Critical-CH. See Indicators |
root defaults to the config file's directory rather than the working
directory, because a monolith loads an app's config from the monolith's own
directory and would otherwise inspect the wrong app.
Hard flags
withFairGardenIndicators(nextConfig, indicators, {
hardFlags: {
beta: { values: [process.env.BETA_COOKIE ?? ''] },
preview: { header: 'x-preview', values: { any: 'pv-.*' } },
},
})
Each key is a route segment injected after [flags] when its source
matches, and a name for a directory under app/[locale]/[prefs]/[flags]/.
cookie, header or query picks the source (the cookie of the same name
by default) and values are literals or patterns, as for an indicator. They
are given here rather than to createIndicators so the values stay out of
the browser bundle; see Hard flags for what the
rewrites do with them.
What it detects
With detectExclusions on, the plugin reads public/ and the top of app/
(or src/app/) and keeps the paths it finds there out of the locale
rewrite: static files, top-level routes such as api/, and the metadata
files Next serves under another extension. See
Rewrites for the list. Set it to false to name every
excluded path in the config's exclude instead.
Why a separate entry point
Next loads next.config.ts with Node itself, and this function reads the
file system, so it lives apart from @fairgarden/indicators: the main entry
is what lib/indicators.ts imports, and that module is bundled for the
client too. The .ts extension on the local import is for the same loader,
which resolves a relative import only with one; set
allowImportingTsExtensions in the app's tsconfig.json.
In a monolith
Nothing here goes into env, and rewrites, redirects and headers are what
a monolith merges, so the result mounts like any app's config. The preload
too: the monolith moves the paths a Link header names along with the
sources. Wrap it with withMonolithicPortability as usual:
export default withMonolithicPortability(withFairGardenIndicators(nextConfig, indicators))