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

Scroll to top on page refresh #10422

Closed
ashleybeshir opened this issue Dec 11, 2018 · 5 comments
Closed

Scroll to top on page refresh #10422

ashleybeshir opened this issue Dec 11, 2018 · 5 comments
Assignees
Labels
type: question or discussion Issue discussing or asking a question about Gatsby

Comments

@ashleybeshir
Copy link

Summary

When the page refreshes i want it to scroll to top of the page. I added window.scrollTo(0, 0); into both componentDidUpdate and componentDidMount. So it should be doing it ? but when page refreshes it still stays at the same Y scroll position

Relevant information

Their are few other tickets for this issue in gatsby 1 but not sure if it been fixed in gatsby 2

Environment (if relevant)

System:
OS: Windows 10
CPU: (8) x64 AMD FX(tm)-8350 Eight-Core Processor
Binaries:
npm: 6.4.1 - C:\Program Files\nodejs\npm.CMD
Browsers:
Edge: 42.17134.1.0
npmPackages:
gatsby: ^2.0.19 => 2.0.19
gatsby-image: ^2.0.15 => 2.0.15
gatsby-plugin-emotion: ^2.0.6 => 2.0.6
gatsby-plugin-manifest: ^2.0.5 => 2.0.5
gatsby-plugin-no-sourcemaps: ^2.0.1 => 2.0.1
gatsby-plugin-offline: ^2.0.5 => 2.0.5
gatsby-plugin-react-helmet: ^3.0.0 => 3.0.0
gatsby-plugin-sharp: ^2.0.7 => 2.0.7
gatsby-source-filesystem: ^2.0.4 => 2.0.4
gatsby-transformer-sharp: ^2.1.4 => 2.1.4

@sidharthachatterjee sidharthachatterjee added the status: needs reproduction This issue needs a simplified reproduction of the bug for further troubleshooting. label Dec 12, 2018
@sidharthachatterjee sidharthachatterjee self-assigned this Dec 12, 2018
@sidharthachatterjee
Copy link
Contributor

sidharthachatterjee commented Dec 12, 2018

This was fixed in #8359

Can you link to a minimal reproduction for this?

@ashleybeshir
Copy link
Author

Hello @sidharthachatterjee ,
it is reproducible in the latest 'gatsby new {sitename}'.
i changed the index page component into a class. The code for it should be underneath this line.

import React from 'react'
import { Link } from 'gatsby'

import Layout from '../components/layout'
import Image from '../components/image'
import SEO from '../components/seo'

class IndexPage extends React.Component {
  constructor(props)
  {
    super(props);
    //window.scrollTo(0,0);
  }

  componentDidUpdate()
  {
    //window.scrollTo(0,0);
    console.log("test-update");
  }

  componentDidMount()
  {
    window.scrollTo(0,0);
    console.log("test-mount");
  }
  render() {
    return <Layout>
        <SEO title="Home" keywords={['gatsby', 'application', 'react']} />
        <h1>Hi people</h1>
        <p>Welcome to your new Gatsby site.</p>
        <p>Now go build something great.</p>
        <div style={{ maxWidth: '300px', marginBottom: '1.45rem' }}>
          <Image />
        </div>
        <div style={{ maxWidth: '300px', marginBottom: '1.45rem' }}>
          <Image />
        </div>
        <div style={{ maxWidth: '300px', marginBottom: '1.45rem' }}>
          <Image />
        </div>
        <div style={{ maxWidth: '300px', marginBottom: '1.45rem' }}>
          <Image />
        </div>
        <div style={{ maxWidth: '300px', marginBottom: '1.45rem' }}>
          <Image />
        </div>
        <div style={{ maxWidth: '300px', marginBottom: '1.45rem' }}>
          <Image />
        </div>
        <Link to="/page-2/">Go to page 2</Link>
      </Layout>;
  }

}

export default IndexPage

@sidharthachatterjee sidharthachatterjee added type: question or discussion Issue discussing or asking a question about Gatsby and removed status: needs reproduction This issue needs a simplified reproduction of the bug for further troubleshooting. labels Dec 12, 2018
@sidharthachatterjee
Copy link
Contributor

sidharthachatterjee commented Dec 12, 2018

Hey @ashleybeshir

What you're seeing is intended scroll behaviour. Gatsby maintains the scroll position of a page and restores it after refresh.

The window.scrollTo is not working likely because of how it is interacting with Gatsby's own scroll management code (which you can find here)

A cleaner and more reliable way to manage scroll behaviour is by using the shouldUpdateScroll browser API.

@ashleybeshir
Copy link
Author

@sidharthachatterjee Thank you will have a look at them now

@sidharthachatterjee
Copy link
Contributor

@ashleybeshir You're welcome 🙂

I'm closing this in the mean time but please feel free to reopen this if there is anything else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question or discussion Issue discussing or asking a question about Gatsby
Projects
None yet
Development

No branches or pull requests

2 participants