Skip to content

Commit

Permalink
fix(feedback): Add FeedbackWidget to SSR (#7870)
Browse files Browse the repository at this point in the history
* fix(feedback): Add `FeedbackWidget` to SSR

Follow-up to #7761 -- fixes hydration warnings/errors due to mismatched component tree.
  • Loading branch information
billyvg authored Sep 18, 2023
1 parent 6e18e96 commit 2ac1631
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
4 changes: 2 additions & 2 deletions gatsby-browser.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React from 'react';
import {GatsbyBrowser} from 'gatsby';

import {FeedbackWidget} from 'sentry-docs/components/feedback/feedbackWidget';
import {FeedbackWidgetLoader} from 'sentry-docs/components/feedback/feedbackWidgetLoader';
import PageContext from 'sentry-docs/components/pageContext';

export const wrapPageElement: GatsbyBrowser['wrapPageElement'] = ({
element,
props: {pageContext},
}) => <PageContext.Provider value={pageContext}>
<FeedbackWidget />
<FeedbackWidgetLoader />
{element}
</PageContext.Provider>

Expand Down
8 changes: 7 additions & 1 deletion gatsby-ssr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import React from 'react';
import {GatsbySSR} from 'gatsby';

import {FeedbackWidgetLoader} from 'sentry-docs/components/feedback/feedbackWidgetLoader';
import {PageContext} from 'sentry-docs/components/pageContext';

const sentryEnvironment = process.env.GATSBY_ENV || process.env.NODE_ENV || 'development';
Expand All @@ -12,7 +13,12 @@ const sentryLoaderUrl = process.env.SENTRY_LOADER_URL;
export const wrapPageElement: GatsbySSR['wrapPageElement'] = ({
element,
props: {pageContext},
}) => <PageContext.Provider value={pageContext}>{element}</PageContext.Provider>;
}) => (
<PageContext.Provider value={pageContext}>
<FeedbackWidgetLoader />
{element}
</PageContext.Provider>
);

export const onPreRenderHTML: GatsbySSR['onPreRenderHTML'] = ({getHeadComponents}) => {
if (process.env.NODE_ENV !== 'production') {
Expand Down
2 changes: 1 addition & 1 deletion src/components/feedback/feedbackWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async function sendFeedback(
return response;
}

export function FeedbackWidget() {
export default function FeedbackWidget() {
const [open, setOpen] = useState(false);
const [showSuccessMessage, setShowSuccessMessage] = useState(false);

Expand Down
20 changes: 20 additions & 0 deletions src/components/feedback/feedbackWidgetLoader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React, {lazy} from 'react';

const FeedbackWidget = lazy(() => import('./feedbackWidget'));

/**
* Only render FeedbackWidget on client as it needs access to `window`
*/
export function FeedbackWidgetLoader() {
const isSSR = typeof window === 'undefined';

return (
<React.Fragment>
{!isSSR && (
<React.Suspense fallback={<div />}>
<FeedbackWidget />
</React.Suspense>
)}
</React.Fragment>
);
}

1 comment on commit 2ac1631

@vercel
Copy link

@vercel vercel bot commented on 2ac1631 Sep 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

sentry-docs – ./

sentry-docs-git-master.sentry.dev
sentry-docs.sentry.dev
docs.sentry.io

Please sign in to comment.