0

I'm trying to serve a production build of a React app(Typescript), booted with create-react-app.

I'm following the official guide: https://create-react-app.dev/docs/deployment/

There's nothing unique about my setup. This is the server.js file, located in the root directory(above src):

app.use(express.static(path.join(__dirname, 'build')));


app.get('/*', function (req, res) {
    res.sendFile(path.join(__dirname, 'build', 'index.html'));
  });
 app.listen(8000);

The production files are created within a folder called build. An example of the paths generated:

  <script src="./static/js/main.0ae46692.chunk.js"></script>

When i navigate to localhost:8000/, everything is served fine. The initial request serves the index.html, and the script requests serve the correct files.

But, when i try to navigate(from the browser) to something like localhost:8000/todos, all script requests return index.html.

I do not see anything "special" about my setup, and do not understand what's going on. Am i missing something in the guide? It clearly states that app.get('/*',...) fixes the issue.

Any help will be greatly appreciated.

2
  • This sounds like the issue is the script tags are generated with relative paths, because it works when you make a request to the root /, but not anything else. Could you try setting the "homepage" to "localhost:8000"? Commented Sep 3, 2021 at 18:02
  • Hmm that did the trick. Also, when i did homepage:"/", it works fine. Thank you. Commented Sep 3, 2021 at 18:21

1 Answer 1

1

Copying the comment into an answer here so it can be marked.

This sounds like the issue is the script tags are generated with relative paths, because it works when you make a request to the root /, but not anything else. Could you try setting the "homepage" to "localhost:8000"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.