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

Document Fast Refresh requirements and troubleshooting #13104

Closed
amiralies opened this issue May 19, 2020 · 20 comments · Fixed by #14273
Closed

Document Fast Refresh requirements and troubleshooting #13104

amiralies opened this issue May 19, 2020 · 20 comments · Fixed by #14273
Assignees
Milestone

Comments

@amiralies
Copy link
Contributor

Bug report

Describe the bug

Fast refresh is not working when using named exports alongside default export.

To Reproduce

  • Setup new next project using next init
  • Go to pages/index.js and remove export default from your function component
export default function Home() {...}
// to
function Home() {...}
  • export something alongside your component as default
// end of the file
let a = 2;
export { a, Home as default};

Expected behavior

Fast refresh should work

System information

  • OS: Linux
  • Browser Chromium latest
  • Version of Next.js: 9.4.1
  • Version of Node.js: 12.16.3
@amiralies
Copy link
Contributor Author

You may ask why are you exporting something other than your component as default.
Bucklescript exports let bindings by default.

@Timer
Copy link
Member

Timer commented May 19, 2020

It's expected that Fast Refresh is disabled if you export something that is not a React Component (even a scalar value as shown above). We'll document this in the docs.

@Izhaki
Copy link

Izhaki commented May 19, 2020

Related: #13024

@thadwoodman
Copy link

@Timer sorry if this isn't the right place for this question, but is it possible to disable Fast Refresh globally in next.config.js? Thanks!

@Timer
Copy link
Member

Timer commented May 19, 2020

@thadwoodman it is not possible to disable Fast Refresh. Can you elaborate why you'd like to do this?

@Timer Timer added this to the 9.4.2 milestone May 19, 2020
@Izhaki
Copy link

Izhaki commented May 19, 2020

I had the very same question and the reason was that full reload is disastrous for us.

But I'm pretty sure that if your code comply with (the currently unclear) guidelines, you will not have full reloads. This leaves you with the same behaviour as hmr (before Fast Refresh was introduced), yet with the benefits of Fast Refresh.

@thadwoodman
Copy link

Can you elaborate why you'd like to do this?

@Timer sure! I'm trying to troubleshoot an error I'm getting on worker in my local dev environment:

Uncaught ReferenceError: $RefreshSig$ is not defined

After some Googling it seems like this is related to React Refresh and I only started seeing this error after upgrading to Next.js 9.4. I should note that there are other people on my team using the same build and not getting this error.

I was just looking for an easy way to turn off Fast Refresh to troubleshoot the issue.

@thadwoodman
Copy link

@Timer looks like issue persists even when I use downgrade Next.js to 9.3, so probably not an issue with Fast Refresh.

@amiralies
Copy link
Contributor Author

Okay, workaround for reason-react (bucklescript) projects is to make an interface file (.rei or .mli) and annotate your default binding:

let default: Js.t({.}) => React.element;

@Timer Timer modified the milestones: 9.4.2, 9.4.3 May 20, 2020
@ryyppy
Copy link

ryyppy commented May 21, 2020

@Timer I am not sure if I fully understand this: So Fast Refresh will only work if my file only exports a default that is a React component, and that has a function with a name?

So exporting a default that is a React component, plus exporting some other named exports is not allowed? That's essential what a ReasonReact module file would do

// pages/somePage.re

let foo = "hello world";
[@react.component]
let default = () => { <div> </div>};

when transpiled to ES6 will look like this:

// pages/somePage.bs.js

import * as React from "react";

function Foo$default(Props) {
  return React.createElement("div", undefined);
}

var foo = "hello world";

var $$default = Foo$default;

export {
  foo ,
  $$default ,
  $$default as default, 
}

@Timer Timer modified the milestones: 9.4.3, 9.4.4 May 28, 2020
@Vadorequest
Copy link
Contributor

It's expected that Fast Refresh is disabled if you export something that is not a React Component (even a scalar value as shown above). We'll document this in the docs.

How can this be? When using export const getStaticProps it gets exported but it's not a React component either. 🤔 Or is it because it's caught by Next.js internals and not actually exported?

Documenting it would be great, I've seen a few setup where people export scalars, like string and such to know whether a page uses SSG or SSR. Doing so would break fast refresh on all pages. 😢

@dohomi
Copy link

dohomi commented May 29, 2020

I'd like to know the reason of it too. I'm using TypeScript and exporting types in my components and not sure if that triggers a hard refresh? And how about to work with external libraries which export named components not default exports?

@Vadorequest
Copy link
Contributor

@dohomi Exporting TS types won't trigger a hard refresh, because they're stripped from the bundled JS. Also, it's easy to know if a hard refresh is triggered, because the whole page refreshes.

@dohomi
Copy link

dohomi commented May 29, 2020

@Vadorequest yes I just look for the reason behind it.. I have over 50 components and searching for the needle which forces a page reload instead of fast-refresh.. it would be great if the log would output the files involved which triggers the reload.

@Timer
Copy link
Member

Timer commented May 29, 2020

There's no need to find the "needle in the haystack", your components can export things that aren't valid react components as long as at least one component in the parent tree is valid (no additional exports).

@dohomi
Copy link

dohomi commented May 31, 2020

@Timer thanks for clarifying this

@amiralies
Copy link
Contributor Author

Seems like it's happening when the page has an exported config I.E {amp: 'hybrid'} too.
@Timer Is this expected behaviour?

@Timer
Copy link
Member

Timer commented Jun 9, 2020

Exporting config does seem like a bug. Can you open a new issue @amiralies?

@Timer Timer modified the milestones: 9.4.5, 9.x.x Jun 10, 2020
@Timer Timer changed the title Fast refresh not working when using named exports Document Fast Refresh requirements and troubleshooting Jun 10, 2020
@Timer Timer removed this from the 9.x.x milestone Jun 15, 2020
@Timer Timer added this to the iteration 3 milestone Jun 15, 2020
@Timer Timer self-assigned this Jun 16, 2020
@dohomi
Copy link

dohomi commented Jun 18, 2020

@Timer one quick question about this topic: I handle provider/context usually in one file. Like a default export for the Provider and the useContext hook for the particular context. This would lead into a full reload because its a hook and a component being exported, right? That might be the case for me why I am facing the reload

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants