Skip to content
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

Fix vica hydration bug #1018

Merged
merged 2 commits into from
Jan 26, 2025
Merged

Fix vica hydration bug #1018

merged 2 commits into from
Jan 26, 2025

Conversation

adriangohjw
Copy link
Contributor

Problem

VICA widget sometimes loading sometimes not

Error log shows https://react.dev/errors/418?args%5B%5D=
image

Suspected it's due to it being rendered SSG and causing incocrrect hydration error

Solution

Breaking Changes

  • Yes - this PR contains breaking changes
    • Details ...
  • No - this PR is backwards compatible

Bug Fixes:

tested on STG site and works

  • explicitly declare use client
  • explicitly declare for it not to be rendered during SSG

Tests

  1. rebuild a site with VICA (https://test-isomer-vica-staging.isomer.gov.sg/)
  2. try spamming reloading multiple times (soft reload, hard reload, hard reload with empty cache etc.) - the VICA widget should show up in the bottom right consistently EVERY time

@adriangohjw adriangohjw added the bug Something isn't working label Jan 23, 2025
@adriangohjw adriangohjw self-assigned this Jan 23, 2025
@adriangohjw adriangohjw requested a review from a team as a code owner January 23, 2025 07:42
@datadog-opengovsg
Copy link

datadog-opengovsg bot commented Jan 23, 2025

Datadog Report

Branch report: fix-vica-hydration-bug
Commit report: a5fc4ae
Test service: isomer-studio

✅ 0 Failed, 257 Passed, 36 Skipped, 44.9s Total Time
➡️ Test Sessions change in coverage: 1 no change

Copy link
Contributor

@seaerchin seaerchin left a comment

Choose a reason for hiding this comment

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

lgtm! jsut to double check why this bug occurs and how this fixes:

  1. previously, we rendered both VicaStylesheet + VicaWidget on the server
  2. when we sent it to the client side, chat.js altered some portions of the html? (hence causing the bug)
  3. how, we mark Widget as use client so that on the server, it doesn't do anything
  4. on client side now, it's initially null (so matches server)
  5. fetches VicaWidget since window is defined (technically this is the rsc script which then re-renders)
  6. VicaWidget loads in properly

if this isnt true, please dismiss the review!


export const VicaWidget = ({
ScriptComponent = "script",
...props
}: VicaProps) => {
// to not render during static site generation on the server
if (typeof window === "undefined") return null
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: prefer !window

@adriangohjw
Copy link
Contributor Author

lgtm! jsut to double check why this bug occurs and how this fixes:

  1. previously, we rendered both VicaStylesheet + VicaWidget on the server
  2. when we sent it to the client side, chat.js altered some portions of the html? (hence causing the bug)
  3. how, we mark Widget as use client so that on the server, it doesn't do anything
  4. on client side now, it's initially null (so matches server)
  5. fetches VicaWidget since window is defined (technically this is the rsc script which then re-renders)
  6. VicaWidget loads in properly

if this isnt true, please dismiss the review!

im not exactly very sure why this is happening because the integration doc for VICA seems to differ for each agency 😢

my observation is that on staging site, VICA widgets actually rendered before disappearing.

my suspicion is that during SSG, VicaWidget was indeed rendered during SSG and modifed the DOM after the initial render, leading to differences between the server-rendered HTML and the client-side DOM during hydration.

@adriangohjw adriangohjw merged commit b8f909a into main Jan 26, 2025
16 of 18 checks passed
@adriangohjw adriangohjw deleted the fix-vica-hydration-bug branch January 26, 2025 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants