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

Client-side routing? #18

Closed
Jpunt opened this issue Jan 10, 2017 · 11 comments
Closed

Client-side routing? #18

Jpunt opened this issue Jan 10, 2017 · 11 comments

Comments

@Jpunt
Copy link

Jpunt commented Jan 10, 2017

How did you fixed client-side routing? I use react-router, which works fine in development, because webpack is running a dev-server. But in production/on now/using serve, it works until I refresh the page when I'm on a route. This makes sense, because that route does not point to a file on disk. What's the best way of fixing this?

@xkawi
Copy link
Owner

xkawi commented Jan 11, 2017

@Jpunt If u refresh the page, you probably need a server code to handle the routes and pass it to the client code for react router to handle. For instance, you can use express for the server code and use react router match function to handle the incoming routes and pass it down to the client side (this is just my thought, probably need to give it a shot myself to see if it works this way). Think of it like an isomorphic react app.

In fact, if you are refering to the latest commits, create-react-app-now does not fix the page refresh issue (it will throw Not Found if you refresh the page), but if you navigate within the page itself it works fine since it's client code.

On a side note, if you are not required to use CRA, do checkout NextJS (if you haven't of course) as it offers better API imo and works pretty well with Now.

@Jpunt
Copy link
Author

Jpunt commented Jan 11, 2017

Thanks. The trick was the amazing --single or -s option of serve. This makes it behave as a single page app 👍

@Jpunt Jpunt closed this as completed Jan 11, 2017
@RamiroIsBack
Copy link

hey , I know this is already closed but I still have that problem and I don't understand that solution Jpunt found where do you set that --single ? Is it in your server where you host your app?

@xkawi
Copy link
Owner

xkawi commented Nov 16, 2017

@RamiroIsBack You use --single when you are running the code using serve. Refer to https://github.com/xkawi/create-react-app-now/blob/master/package.json "now-start" command.

Additionally, refer create-react-app official documentation regarding the deployment: https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#deployment

@RamiroIsBack
Copy link

Thanks for pointing me in the right direction, going through the official documentation regarding the deployment, says that serve is used for environments using Node, and all the examples and docs about it are using Node. My app is pure react front-end and I'm using firebase for the back-end tasks.
-Do you see another way to make the routing work in production?
-Or does it means that I have to set a back-end for it in Node, so I can use serve to make my web-app work as a single page app caching all the front-end routing and pointing it to index.html? did I get it right?

thanks for the time and effort @xkawi

@xkawi
Copy link
Owner

xkawi commented Nov 16, 2017 via email

@RamiroIsBack
Copy link

that was perfect explanation, and it works perfect locally through serve, cos I installed it globally on my laptop, but when I upload my build content to my hosting server and is out there , refreshing the page on one of the routes keeps on giving me:

Not Found
The requested URL /contact was not found on this server.

Any idea how can I make it work as it works locally through serve?

@xkawi
Copy link
Owner

xkawi commented Nov 17, 2017

@RamiroIsBack what is your hosting provider? as you probably need to check how you can deploy a nodejs app into it, means projects that has package.json in it.

if you use Now, it's probably will just work, provided that you have:
"now-start": "server -s ./build"
as one of the npm script in package.json

@RamiroIsBack
Copy link

All right, I had the app on heroku for testing and their servers can handle routing on the client side no problem, but now my clients hosting provider doesn't, and as you suggested I asked them if I could deploy a nodejs app but you have to pay for that service and it's out of budget so...

Since I only have 5 routes on my single page app what I did is to write an html page for each of them and a .js file that controls and redirect them to index.html, this way, I at least catch what suppose to be working on my end, and the 404 is well handled from the server for mistakes or misspellings

I want to thank you @xkawi again for your help!

@xkawi
Copy link
Owner

xkawi commented Nov 17, 2017

@RamiroIsBack Anytime. Glad to hear that it’s working on your end now. 👍😊

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

3 participants