diff --git a/packages/gatsby-plugin-fastify/src/gatsby/header-builder.ts b/packages/gatsby-plugin-fastify/src/gatsby/header-builder.ts index 54489343c..b70c80d90 100644 --- a/packages/gatsby-plugin-fastify/src/gatsby/header-builder.ts +++ b/packages/gatsby-plugin-fastify/src/gatsby/header-builder.ts @@ -4,11 +4,12 @@ import typeOf from "just-typeof"; import { SECURITY_HEADERS, CACHING_HEADERS, IMMUTABLE_CACHING_HEADER } from "../utils/constants"; import type { PluginData } from "../utils/plugin-data"; -import type { GatsbyNodeServerConfig } from "../utils/config"; +import type { GatsbyFastifyPluginOptions } from "../utils/config"; +import { HeadersOption } from "../utils/headers"; -function deepMerge(...headers) { +function deepMerge(...headers: HeadersOption[]) { // eslint-disable-next-line unicorn/no-array-reduce - return headers.reduce((accumulator, header: { [key: string]: object }) => { + return headers.reduce((accumulator, header) => { for (let [key, value] of Object.entries(header)) { // console.log(typeOf(value)); //merge needs empty object to prevent overwriting of references use across multiple routes @@ -23,8 +24,8 @@ function deepMerge(...headers) { // program methods const applySecurityHeaders = - ({ mergeSecurityHeaders }: GatsbyNodeServerConfig) => - (headers) => { + ({ mergeSecurityHeaders }: GatsbyFastifyPluginOptions) => + (headers: HeadersOption) => { if (!mergeSecurityHeaders) { return headers; } @@ -33,8 +34,8 @@ const applySecurityHeaders = }; const applyCachingHeaders = - (pluginData: PluginData, { mergeCacheHeaders }: GatsbyNodeServerConfig) => - (headers) => { + (pluginData: PluginData, { mergeCacheHeaders }: GatsbyFastifyPluginOptions) => + (headers: HeadersOption) => { if (!mergeCacheHeaders) { return headers; } @@ -53,7 +54,7 @@ const applyCachingHeaders = const files = chunks.flatMap((chunk) => pluginData.manifest[chunk]); - const cachingHeaders = {}; + const cachingHeaders: HeadersOption = {}; for (const file of files) { if (typeof file === `string`) { @@ -63,7 +64,10 @@ const applyCachingHeaders = return deepMerge(cachingHeaders, CACHING_HEADERS, headers); }; -export function buildHeadersProgram(pluginData, pluginOptions) { +export function buildHeadersProgram( + pluginData: PluginData, + pluginOptions: GatsbyFastifyPluginOptions +) { return compose( applySecurityHeaders(pluginOptions), applyCachingHeaders(pluginData, pluginOptions) diff --git a/packages/gatsby-plugin-fastify/src/utils/config.ts b/packages/gatsby-plugin-fastify/src/utils/config.ts index 741d1399c..383b24346 100644 --- a/packages/gatsby-plugin-fastify/src/utils/config.ts +++ b/packages/gatsby-plugin-fastify/src/utils/config.ts @@ -10,6 +10,7 @@ import type { FastifyServerOptions } from "fastify"; import { PathConfig } from "../plugins/client-routes"; import { CONFIG_FILE_NAME, CONFIG_FILE_PATH } from "./constants"; import { buildPrefixer } from "./plugin-data"; +import { HeadersOption } from "./headers"; let config: Partial = {}; @@ -22,15 +23,12 @@ export interface GatsbyFastifyPluginOptions extends PluginOptions { imageCdn: boolean; }; fastify: FastifyServerOptions; + headers: HeadersOption; } export interface GatsbyNodeServerConfig extends GatsbyFastifyPluginOptions { clientSideRoutes: NoUndefinedField[]; serverSideRoutes: ServerSideRoute[]; - headers: { - [path: string]: { - [name: string]: string; - }; - }; + headers: HeadersOption; mergeCacheHeaders?: boolean; mergeSecurityHeaders?: boolean; redirects: IRedirect[]; diff --git a/packages/gatsby-plugin-fastify/src/utils/headers.ts b/packages/gatsby-plugin-fastify/src/utils/headers.ts index df43ca0b4..f4471e4d3 100644 --- a/packages/gatsby-plugin-fastify/src/utils/headers.ts +++ b/packages/gatsby-plugin-fastify/src/utils/headers.ts @@ -12,6 +12,12 @@ function appendHeader({ name, value }: { name: string; value: string }, reply: F } } +export type HeadersOption = { + [key: string]: { + [key: string]: string; + }; +}; + export type Modules = | "DSG" | "SSR"