-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: pass pathname to replaceRenderer and onPreRenderHTML SSR APIs #9792
Conversation
For some situations (e.g. implementing AMP pages), a user may need to conditionally remove or add elements in the head or body of a page. Normally, this could be done when the page or node is created. But, styles and scripts may be added later in the lifecycle. The onRenderBody SSR API provides a pathname argument that could be used but does not allow the removal of existing elements. Using replaceHeadComponents or replaceBodyComponents in onPreRenderHTML would seem to be ideal. But, this API doesn't expose the pathname argument. The following example for combining inline style elements in the head of the document is not currently possible but would be if pathname were exposed. import React from 'react' import Helmet from 'react-helmet' export const onPreRenderHTML = ( { getHeadComponents, replaceHeadComponents, pathname }, pluginOptions ) => { const headComponents = getHeadComponents() const styles = headComponents.reduce((str, x) => { if (x.type === 'style') { str += x.props.dangerouslySetInnerHTML.__html } return str }, '') const regex = new RegExp(pluginOptions.pathRegex || '\/amp\/') if (regex.test(pathname)) { replaceHeadComponents([ <Helmet> <style id="amp-custom"> {styles} </style> </Helmet>, ...headComponents.filter(x => x.type !== 'style'), ]) } } I'm not sure if this needs to be submitted as an RFC or not. It does slightly modify an API but it seems very minor.
I think it's perfectly reasonable thing to do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @jafaircl
Holy buckets, @jafaircl — we just merged your PR to Gatsby! 💪💜 Gatsby is built by awesome people like you. Let us say “thanks” in two ways:
If there’s anything we can do to help, please don’t hesitate to reach out to us: tweet at @gatsbyjs and we’ll come a-runnin’. Thanks again! |
Update the docs for this change: #9792. It's not a breaking change. It also adds the pathname to `onRenderBody` and `onPreRenderHTML` in developer.
For some situations (e.g. implementing AMP pages), a user may need to conditionally remove or add elements in the head or body of a page. Normally, this could be done when the page or node is created. But, styles and scripts may be added later in the lifecycle. The
onRenderBody
SSR API provides apathname
argument that could be used but does not allow the removal of existing elements. UsingreplaceHeadComponents
orreplaceBodyComponents
inonPreRenderHTML
would seem to be ideal. But, this API doesn't expose thepathname
argument.The following example for combining inline style elements in the head of the document is not currently possible but would be if pathname were exposed.
I'm not sure if this needs to be submitted as an RFC or not. It does slightly modify an API but it seems very minor.