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

with-reasonml example errors when using 6.0.0-canary.6 #4202

Closed
1 task done
tmepple opened this issue Apr 23, 2018 · 5 comments
Closed
1 task done

with-reasonml example errors when using 6.0.0-canary.6 #4202

tmepple opened this issue Apr 23, 2018 · 5 comments

Comments

@tmepple
Copy link

tmepple commented Apr 23, 2018

This example works fine when using the stable version v5.1 but causes an error when upgrading to the latest canary. It looks like it might be related to the new Babel v7 beta or the babel-plugin-bucklescript that is used but I couldn't isolate/reproduce the problem in the other repo.

  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

The with-reasonml example should work in v6 canary as it did under v5.x

Current Behavior

Steps to Reproduce (for bugs)

  1. yarn create next-app --example with-reasonml with-reasonml-app
  2. cd with-reasonml-app
  3. yarn && yarn dev
  4. Navigate to localhost:3000 It works!
  5. Bump version of next in package.json to 6.0.0-canary.6 then run yarn and yarn dev again
  6. Navigate to localhost:3000 and get the following error:

screen shot 2018-04-23 at 5 14 01 pm

Context

Was wanting to test the new _app.js functionality to see if it makes using getInitialProps to load Apollo queries easier to use for pages implemented in ReasonML. Currently it's difficult/impossible to bind to the static getInitialProps from Reason pages.

Your Environment

Tech Version
next 6.0.0-canary.6
node 9.4.0
OS MacOS 10.13.4
browser Chrome v65
@tmepple
Copy link
Author

tmepple commented Apr 24, 2018

Digging deeper I was able to identify an issue in babel-plugin-bucklescript where for some reason filenameRelative is being passed into the plugin as undefined when using Babel v7. I couldn't find the change in Babel causing it as it seems like it should still work but since this is not an issue with NextJS, closing it here.

I will send a PR to babel-plugin-bucklescript so it works with Babel v7.

@tmepple tmepple closed this as completed Apr 24, 2018
@Enalmada
Copy link
Contributor

Enalmada commented Nov 15, 2018

@tmepple Are you still using reasonml with next.js? I have a reasonml page I need to pass the context into to know what the query parameters are in the url of that page. Have you figured out how to use getInitialProps with ReasonML and nextjs reason pages?

(My "plan B" is leaving my _app.js as plain javascript and then passing stuff from that getInitialProps to <Component someParam=manualStuff {...pageProps} /> which can go in as a parameter to make.)

@tmepple
Copy link
Author

tmepple commented Nov 15, 2018

@Enalmada

I was able to get context in getInitialProps in a pure ReasonML page following the instructions here: #4217 (comment)

Here is an example of accessing the context to detect if we are rendering on the server and passing a boolean into your ReasonReact component. Hope this helps!

let make = (~onServer, _children) => {
/* component code goes here */ 
};

let default =
  ReasonReact.wrapReasonForJs(~component, jsProps => make(~onServer=jsProps##onServer, [||]));

let getInitialProps = context =>
  Js.Promise.make((~resolve, ~reject as _) => {
    let onServer =
      switch (Js.Nullable.toOption(context##req)) {
      | None => false
      | Some(_) => true
      };
    resolve(. {"onServer": onServer});
  });

let inject = [%bs.raw {| (cls, fn) => cls.getInitialProps = fn |}];

inject(default, getInitialProps);

@Enalmada
Copy link
Contributor

Wow...that works! Thank you so much. I will get that into the example since it is likely to be something next reasonml users may eventually need to do and implementation is a bit complex for new users to work out on their own.

Speaking of next with reasonml, would you happen to be able to provide any feedback about this: #5049.

@tmepple
Copy link
Author

tmepple commented Nov 17, 2018

HI @Enalmada I looked at that other issue and not enough time right now to test the hypothesis but maybe it would work if you define a mutable ref(), feed that to initial state, then mutate it (using :=) in the reducer? Or maybe use a handler instead to mutate the counter variable in Counter.re external to the state? Hope this gives you some things to try.

https://reasonml.github.io/docs/en/mutation
https://reasonml.github.io/reason-react/docs/en/react-ref (note this may be confusing since it is talking about react ref to get a reference to the DOM but it also uses a normal ocaml ref() and shows the use of handlers)
https://reasonml.github.io/reason-react/docs/en/callback-handlers

timneutkens pushed a commit that referenced this issue Jan 2, 2019
The current reasonml needs an example of how to do getInitialProps.  tmepple posted the only known way of doing it in [this comment](#4202 (comment)).   It is unlikely reasonml users are going to discover that comment or figure it out on their own so having it in an example is critical. 

Also, dependencies updated.  After updating dependencies, I get this error:
```
ModuleBuildError: Module build failed (from ./node_modules/next/dist/build/webpack/loaders/next-babel-loader.js):
[1] Error: Cannot find module '@babel/core'
[1]  babel-loader@8 requires Babel 7.x (the package '@babel/core'). If you'd like to use Babel 6.x ('babel-core'), you should install 'babel-loader@7'.
```
Seems like @babel/core is required as peer dependency per [this comment](babel/gulp-babel#124 (comment)) so I added it.

The way to do getInitialProps has changed in 7.0.2 canary so this also has a comment in the code about what change is required to get that working.
@lock lock bot locked as resolved and limited conversation to collaborators Nov 17, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants