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

Check for existence of window to support SSR #159

Open
aarongervais opened this issue Aug 25, 2020 · 3 comments
Open

Check for existence of window to support SSR #159

aarongervais opened this issue Aug 25, 2020 · 3 comments

Comments

@aarongervais
Copy link

Great polyfill, but it breaks Gatsby's build step because window is undefined. Maybe consider adding the following to the top of the polyfill function:

if (typeof window === 'undefined') { 
  return; 
}
@freddydumont
Copy link

You have to use onClientEntry with Gatsby:

var smoothscroll = require('smoothscroll-polyfill');

exports.onClientEntry = () => {
  smoothscroll.polyfill();
};

Or you can just use my gatsby plugin:

https://github.com/freddydumont/gatsby-plugin-smoothscroll

@EricWVGG
Copy link

for Next.js, you'll need a “Custom 'App'” as per https://nextjs.org/docs/advanced-features/custom-app

ex:

import { AppProps } from 'next/app'
import smoothscroll from 'smoothscroll-polyfill'

function App({ Component, pageProps }: AppProps) {
  if(typeof window !== 'undefined') {
    smoothscroll.polyfill()  
  }
  return (
    <div suppressHydrationWarning>
      {typeof window === 'undefined' ? null : <Component {...pageProps} />}
    </div>
  )
}

export default App

@percy507
Copy link

for umi.js

import { isBrowser } from 'umi';
import smoothscroll from 'smoothscroll-polyfill';

if (isBrowser()) {
  smoothscroll.polyfill();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants