Skip to content

Commit 9d83815

Browse files
committed
feat: added better custom header support
1 parent 4abc04c commit 9d83815

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

plugin/src/helpers/config.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1+
import type { NetlifyConfig } from '@netlify/build'
12
import { readJSON, writeJSON } from 'fs-extra'
3+
import type { Header } from 'next/dist/lib/load-custom-routes'
24
import type { NextConfigComplete } from 'next/dist/server/config-shared'
35
import { join, dirname, relative } from 'pathe'
46
import slash from 'slash'
57

68
import { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME } from '../constants'
79

10+
const ROUTES_MANIFEST_FILE = 'routes-manifest.json'
11+
12+
type NetlifyHeaders = NetlifyConfig['headers']
13+
14+
// This is the minimal amount of typing required for now
15+
// add other properties as needed from the routes-manifest.json
16+
export type RoutesManifest = { headers: Header[] }
17+
818
export interface RequiredServerFiles {
919
version?: number
1020
config?: NextConfigComplete
@@ -13,7 +23,10 @@ export interface RequiredServerFiles {
1323
ignore?: string[]
1424
}
1525

16-
export type NextConfig = Pick<RequiredServerFiles, 'appDir' | 'ignore'> & NextConfigComplete
26+
export type NextConfig = Pick<RequiredServerFiles, 'appDir' | 'ignore'> &
27+
NextConfigComplete & {
28+
routesManifest?: RoutesManifest
29+
}
1730

1831
const defaultFailBuild = (message: string, { error }): never => {
1932
throw new Error(`${message}\n${error && error.stack}`)
@@ -24,13 +37,19 @@ export const getNextConfig = async function getNextConfig({
2437
failBuild = defaultFailBuild,
2538
}): Promise<NextConfig> {
2639
try {
27-
const { config, appDir, ignore }: RequiredServerFiles = await readJSON(join(publish, 'required-server-files.json'))
40+
const { config, appDir, ignore, files }: RequiredServerFiles = await readJSON(
41+
join(publish, 'required-server-files.json'),
42+
)
2843
if (!config) {
2944
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
3045
// @ts-ignore
3146
return failBuild('Error loading your Next config')
3247
}
33-
return { ...config, appDir, ignore }
48+
49+
const routesManifest = (await readJSON(files.find((f) => f.endsWith(ROUTES_MANIFEST_FILE)))) as RoutesManifest
50+
51+
// If you need access to other manifest files, you can add them here as well
52+
return { ...config, appDir, ignore, routesManifest }
3453
} catch (error: unknown) {
3554
return failBuild('Error loading your Next config', { error })
3655
}
@@ -108,3 +127,22 @@ export const configureHandlerFunctions = ({ netlifyConfig, publish, ignore = []
108127
})
109128
})
110129
}
130+
131+
/**
132+
* Persist NEXT.js custom headers to the Netlify configuration so the headers work with static files
133+
*
134+
* @param customHeaders - Custom headers defined in the Next.js configuration
135+
* @param netlifyHeaders - Existing headers that are already configured in the Netlify configuration
136+
*/
137+
export const generateCustomHeaders = (customHeaders: Header[] = [], netlifyHeaders: NetlifyHeaders = []) => {
138+
for (const { source, headers } of customHeaders) {
139+
netlifyHeaders.push({
140+
for: source,
141+
values: headers.reduce((builtHeaders, { key, value }) => {
142+
builtHeaders[key] = value
143+
144+
return builtHeaders
145+
}, {}),
146+
})
147+
}
148+
}

plugin/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
getRequiredServerFiles,
1313
updateRequiredServerFiles,
1414
configureHandlerFunctions,
15+
generateCustomHeaders,
1516
} from './helpers/config'
1617
import { updateConfig, writeMiddleware } from './helpers/edge'
1718
import { moveStaticPages, movePublicFiles, patchNextFiles, unpatchNextFiles } from './helpers/files'
@@ -127,6 +128,7 @@ const plugin: NetlifyPlugin = {
127128
netlifyConfig: {
128129
build: { publish },
129130
redirects,
131+
headers,
130132
},
131133
utils: {
132134
status,
@@ -152,7 +154,10 @@ const plugin: NetlifyPlugin = {
152154

153155
await checkForOldFunctions({ functions })
154156
await checkZipSize(join(FUNCTIONS_DIST, `${ODB_FUNCTION_NAME}.zip`))
155-
const { basePath, appDir } = await getNextConfig({ publish, failBuild })
157+
const { basePath, appDir, routesManifest } = await getNextConfig({ publish, failBuild })
158+
159+
generateCustomHeaders(routesManifest.headers, headers)
160+
156161
warnForProblematicUserRewrites({ basePath, redirects })
157162
warnForRootRedirects({ appDir })
158163
await unpatchNextFiles(basePath)

0 commit comments

Comments
 (0)