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

serving dist directory through nginx fails #214

Closed
jeongl opened this issue Jul 26, 2016 · 17 comments
Closed

serving dist directory through nginx fails #214

jeongl opened this issue Jul 26, 2016 · 17 comments

Comments

@jeongl
Copy link

jeongl commented Jul 26, 2016

I've been trying to deploy my app the past couple of days using nginx. It definitely works when using express and pointing the dist folder, using: app.use(express.static('dist'));

The problem is when serving it through nginx using a location block. I have the following location block

  location /myblog {
    alias /app/blog/dist;
    index index.html index.htm;
  }

It hits the route, loads the index.html, .js & .css, but it renders an empty app. Any thoughts??

  <body style="background:#fff;font-family:Helvetica"><div id="app">
    <!-- react-empty: 1 --></div>
    <script type="text/javascript" src="/main.746946c56bcce138a405.js"></script>
  </body>
@coryhouse
Copy link
Owner

Sorry, I'm not strong in NGINX. I'd suggest posting on StackOverflow since your question is simple and not specific to Slingshot. Your real question is how to serve static files via NGINX.

@jeongl
Copy link
Author

jeongl commented Jul 26, 2016

@coryhouse - I think this could be a webpack issue and it's related to this repo because of how it's configured. I get the same result when I go into the the dist folder and click index.html.

That's the same way nginx would serve the file - is this ultimately a pure webpack issue, or is it related to this repo?

@coryhouse
Copy link
Owner

The filepaths written to index.html have a leading slash. This means you need to serve the site from the root. When you open index.html via the browser, it will open it at the full file path using the file protocol instead of http. For example.

file:///Users/coryhouse/Desktop/projects/react-slingshot/dist/index.html

When serving from this nested path, script references like /main.js will fail.

That said, if you need to serve the app from a nested path, you can look into changing the public path in webpack.config.prod.js: https://github.com/coryhouse/react-slingshot/blob/master/webpack.config.prod.js#L23

@jeongl
Copy link
Author

jeongl commented Jul 26, 2016

I have manually just changed the index.html to point to the right .js and .css - it loads the scripts fine, but still empty.

@coryhouse
Copy link
Owner

Open your browser tools - I bet you're getting a 404.

@jeongl
Copy link
Author

jeongl commented Jul 26, 2016

No - i swear to you : ) Both the .js and .css load - I know because I saw it load in the dev tools.

@coryhouse
Copy link
Owner

If it doesn't load, you should see a JS error or network error in your browser console

@jeongl
Copy link
Author

jeongl commented Jul 26, 2016

So it loads the index.html, js and css (200) responses in the console. The DOM is empty.

If i keep refreshing the page, intermittently it will throw an error saying it failed to parse the .css.map

@jeongl
Copy link
Author

jeongl commented Jul 26, 2016

But why does it work when served through express is what i don't understand

@kwelch
Copy link
Collaborator

kwelch commented Jul 26, 2016

Do you have a demo of this or is a public url? That way we can take a look at it. Nearly impossible to troubleshoot an issue like this without being able to re-produce.

@jeongl
Copy link
Author

jeongl commented Jul 26, 2016

This is a problem with the example app that ships as well - if you build the directory with the publicPath as './' it will load the index.html, css , and js, but not the rest of the files. Parts of the dom are missing too.

@jeongl
Copy link
Author

jeongl commented Jul 26, 2016

Can we get this reopened after you can confirm??

@coryhouse
Copy link
Owner

I've confirmed it works just fine when served from web root. (which is as designed)

Steps to reproduce:

  1. npm run build
  2. npm install -g http-server
  3. cd dist
  4. http-server

Step 4 will open a lightweight http server that serves files from dist. The app works fine for me. So this proves it works fine with a plain webserver that simply serves static files.

@felipedm
Copy link

felipedm commented Oct 7, 2016

@coryhouse I believe the issue here is browserHistory. @jeongl could you try switching to hashHistory on your index.js? I think it will solve the problem you are having.

You can also configure NGINX to use browserHistory https://github.com/ReactTraining/react-router/blob/master/docs/guides/Histories.md#browserhistory

I see browserHistory is also mentioned on #218

@coryhouse
Copy link
Owner

I'm open to it, but am unclear exactly what changes are required, so a PR would help me understand if it makes sense. Thanks for the offer!

@ConstantineStehoff
Copy link

I have the same issue when running on Apache. It just shows a blank page and no errors

@ConstantineStehoff
Copy link

The issue is with the default route. You need to add a default route to links in the router and then it works. So, this issue is not related to react sligshot

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

5 participants