question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rendering wrong HTML on hard refresh of app

See original GitHub issue

Hey, pretty strange issue I’m seeing.

When I hard refresh my app on a client only path (like id’d item detail pages), the page renders incorrectly with artifacts from the 404 page that had initially redirected it (see below). My 404 page has a spinner is an intermediary until the client side path resolves to the new page. On this hard refresh, for some reason, high level wrapping div styling is being left on the container causing my app to render improperly.

This only happens when distributed to Cloudfront/S3. It does not happen on a soft refresh. It does not happen at all in local development.

Left is with the artifacts of the 404 page, right is what is supposed to be rendered for that page.

Screen Shot 2020-10-19 at 9 34 18 PM

Let me know what other information I can provide for this. It’s pretty strange… Any help would be appreciated. Something is going wrong it seems in the redirect from the 404 page to resolving to the correct route as the app initiates.

========

App is distributed on S3 + CloudFront

Config

    {
      resolve: `gatsby-plugin-s3`,
      options: {
        bucketName: process.env.GATSBY_APP_URL,
        protocol: 'https',
        hostname: process.env.GATSBY_APP_URL,
        generateMatchPathRewrites: false,
      },
    },

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
jarizcommented, Oct 20, 2020

Yep, interested to see if you have the same results with npx gatsby build && npx gatsby serve. During local development, gatsby runs as an SPA only, so this sounds like a typical client & server render mismatch.

With react it is important to keep in mind that, when hydrating, it expects the server side and client side to return the same HTML. If the 2 differ, things can get very unstable.

You need to make sure the client & server render the same result, and use useEffect to do any clientside specific things. In the past react used to warn about this and do a full rerender if the HTML it’s hydrating does not match the client side render, but in recent versions it does not do these checks anymore for the sake of performance.

0reactions
MartinDawsoncommented, Mar 17, 2021

I figured out the issue and @jariz was correct.

In this case where the html is rendered incorrectly it’s an issue with our own code. The problem lies in the SSR being different than the client side.

In my case it was the wrong layouts rendering for client routes.

For example, I am using gatsby-plugin-layout and here was my layouts code before:

return ( <Container maxWidth={false} key={0}> <Header /> {isLoaded ? children : null} <TTTabs /> </Container> );

Here it is after (working):

return isLoaded ? ( <Container maxWidth={false} key={0}> <Header /> {children} <TTTabs /> </Container> ) : null;

Horrible bug to fix as gatsby doesn’t warn.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Page not found when hard refresh - Reactjs - Stack Overflow
I'm developing ReactJs Application.I'm getting page not found (404) error when i hard refresh the build page.but its work fine (page routing) ...
Read more >
Cache Busting a React App - DEV Community ‍ ‍
Force clear cache and hard reload when there's a version mismatch. Every time the app is loaded, we check for the latest version....
Read more >
Slow rendering - Android Developers
If your app suffers from slow UI rendering, then the system is forced to skip frames and the user will perceive stuttering in...
Read more >
Handling common JavaScript problems - MDN Web Docs
For example, in bad-for-loop.html (see source code), we loop through ... Refresh the page in the browser, and you will get an output...
Read more >
How to Solve the Chunk Load Error in JavaScript - Rollbar
If you are encountering this error as a user of an application, the most simple way to resolve it is to clear your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found