indie-stack icon indicating copy to clipboard operation
indie-stack copied to clipboard

Remix and Cypress hydration problem

Open triptec opened this issue 2 years ago • 8 comments

Have you experienced this bug with the latest version of the template?

Yes

Steps to Reproduce

Setup the template and run npm test:e2e:dev and run the smoke test in either chrome or electron and open the console.

Expected Behavior

That there shouldn't be any hydration probelms

Actual Behavior

The console is filled with: react-dom.development.js:86 Warning: Expected server HTML to contain a matching <meta> in <head>. at meta at head at html at App at RemixRoute (http://localhost:3000/build/_shared/chunk-DRRZ7FOQ.js:7074:3) at RenderedRoute (http://localhost:3000/build/_shared/chunk-DRRZ7FOQ.js:4543:5) at RenderErrorBoundary (http://localhost:3000/build/_shared/chunk-DRRZ7FOQ.js:4932:9) at Routes (http://localhost:3000/build/_shared/chunk-DRRZ7FOQ.js:4803:5) at Router (http://localhost:3000/build/_shared/chunk-DRRZ7FOQ.js:4750:15) at RouterProvider (http://localhost:3000/build/_shared/chunk-DRRZ7FOQ.js:4699:5) at RemixErrorBoundary (http://localhost:3000/build/_shared/chunk-DRRZ7FOQ.js:6693:5) at RemixBrowser (http://localhost:3000/build/_shared/chunk-DRRZ7FOQ.js:8138:55) printWarning @ react-dom.development.js:86 error @ react-dom.development.js:60 warnForInsertedHydratedElement @ react-dom.development.js:10499 didNotFindHydratableInstance @ react-dom.development.js:11458 warnNonhydratedInstance @ react-dom.development.js:12385 tryToClaimNextHydratableInstance @ react-dom.development.js:12534 updateHostComponent @ react-dom.development.js:19902 beginWork @ react-dom.development.js:21618 beginWork$1 @ react-dom.development.js:27426 performUnitOfWork @ react-dom.development.js:26560 workLoopConcurrent @ react-dom.development.js:26543 renderRootConcurrent @ react-dom.development.js:26505 performConcurrentWorkOnRoot @ react-dom.development.js:25738 workLoop @ scheduler.development.js:266 flushWork @ scheduler.development.js:239 performWorkUntilDeadline @ scheduler.development.js:533 react-dom.development.js:12507 Uncaught Error: Hydration failed because the initial UI does not match what was rendered on the server. at throwOnHydrationMismatch (react-dom.development.js:12507:9) at tryToClaimNextHydratableInstance (react-dom.development.js:12535:7) at updateHostComponent (react-dom.development.js:19902:5) at beginWork (react-dom.development.js:21618:14) at HTMLUnknownElement.callCallback2 (react-dom.development.js:4164:14) at Object.invokeGuardedCallbackDev (react-dom.development.js:4213:16) at invokeGuardedCallback (react-dom.development.js:4277:31) at beginWork$1 (react-dom.development.js:27451:7) at performUnitOfWork (react-dom.development.js:26560:12) at workLoopConcurrent (react-dom.development.js:26543:5)

triptec avatar Apr 18 '23 21:04 triptec

I've been debugging this as it makes our e2e test flaky due to re-renders and with that we get detached elements. Here's some discussion discussion

Could the problem be that cypress injects quite a lot of stuff into the html? https://github.com/cypress-io/cypress/blob/master/packages/proxy/lib/http/response-middleware.ts#L540

That's my prime suspect as I don't have any problems with hydration outside of cypress.

triptec avatar Apr 18 '23 21:04 triptec

If I disable the code injection cypress (skip the MaybeInjectHtml middleware) does I don't get any hydration errors, that also breaks cypress.

triptec avatar Apr 19 '23 00:04 triptec

Did you ever fix this?

ConorTighe avatar Jul 17 '23 08:07 ConorTighe

No and not sure if there’s a simple solution either, I’ve since moved on from cypress to playwright.

triptec avatar Jul 17 '23 15:07 triptec

@triptec I have encountered the same issue. No workaround right now, will consider moving on from cypress

awweather avatar Jul 28 '23 17:07 awweather

The issue is not tied to the indie stack actually, but I'm not sure the issues here have the same visibility as those in the remix main repository. Still no answer from the dev team 😢

Should we move the issue to the main repo ? 🤷🏻‍♂️

charesbast avatar Aug 03 '23 09:08 charesbast

Relates to https://github.com/remix-run/remix/issues/4822

I think this can be closed

antonkri97 avatar Oct 05 '23 19:10 antonkri97

Some workaround I found to avoid issues caused by the dom being trashed by the hydration while the test is running.

// cypress/support/commands.ts
Cypress.Commands.overwrite('visit', (originalFn, ...args) => {
  originalFn(...args);
  cy.get('#__is_hydrated__').should('exist');
});
// app/root.tsx
import { ClientOnly } from 'remix-utils/client-only';

export default function App() {
  return (
    <>
      <YourAppCode />
      {/* Avoid workaround from being in the production build */}
      {process.env.NODE_ENV === 'development' ? (
        <ClientOnly>{() => <div id="__is_hydrated__"></div>}</ClientOnly>
      ) : null}
    </>
  );
}

Aebrathia avatar Aug 15 '24 14:08 Aebrathia