Provide method for defining page level headers & ability to transform headers #38528
Unanswered
techfg
asked this question in
Ideas / Feature Requests
Replies: 1 comment
-
Tagging #38542 for cross-reference |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Background
Adapters are a great path forward for Gatsby, however the current functionality compared to previous methods (e.g., gatsby-plugin-gatsby-cloud, gatsby-plugin-netlify) is not yet at the same level. For example, these two plugins offer
allPageHeaders
andtransformHeaders
properties simplifying the ability to create a header for pages only and then transforming special cases for any header on any path. With the migration to Netlify from Gatsby Cloud, and for those already on Netlify (since Netlify is the only adapter currently), losing the functionality can have significant effects.Currently, the Gatsby Config
headers
property is an array of path level headers meaning that everypage
must have an explicit entry. This is incredibly cumbersome for any site let alone sites with a lot of pages. Route matching doesn't work to identify "pages only" unfortunately (at least as far as I can tell - please correct me if I'm wrong).Additionally, there are situations where the final header value may not be known until farther down in the build process. For example, I generate a Content Security Policy (CSP) with hashes and the hashes aren't able to be determined until the final html is known.
Proposal
Add ability to specify headers that will be applied to all pages and the ability to transform the headers just before the adapter persists them (in the
adapt
phase).Potential Approaches:
headers
A general approach to any of the above would be:
createHeaders
(fromcreateHeadersMatcher
) is called on a page, addallPageHeaders
to the list of defaultHeaders (modifications here and here in both cases we can easily identify apage
)createHeaders
(fromcreateHeadersMatcher
) call the transform function (or invoke thevalue
if it's a function) if defined (requires a few adjustments here.There are other approaches (e.g., adding a special
source
value inHeader
such as::allPages
), but the above seem the most straightforward and least invasive. Personally, I prefer combining #1 & #2 (transformHeaders for all headers for a path & value function for individual header values) for maximum flexibility, however #1 matches currently functionality of the existing plugin ecosystem and provides all the necessary functionality (there are some perf & consumer configuration trade-offs between #1 & #2 worth considering though). Would also consider making the transformHeaders (or value function) asynchronous as it may need to obtain resources to derive the value(s) (e.g., read a file of cached CSP hashes 😉).Workaround
Below is a solution that should address the equivalent of
allPageHeaders
andtransformHeaders
for SSG pages and redirects - it will not solve for functions or DSG/SSR pages as adapters are not responsible for their headers (at least as far as I can tell).By creating a custom adapter and composing the Netlify adapter, we can intercept the
adapt
call, add in page headers & call transform before passing on the context with the modified routeManfiest togatsby-adapter-netlify
. Getting close to back to the functionality ingatsby-plugin-gatsby-cloud
&gatsby-plugin-netlify
.Warning
This code has not been fully tested for functionality, performance, etc., it is just a proof-of-concept - use at your own risk.
Note
As mentioned above, this will not work for functions or DSG/SSR pages.
gatsby-config.ts
Thoughts/input welcome!
Beta Was this translation helpful? Give feedback.
All reactions