2

I'm trying to serve a React application from a sub-folder. It's currently a very simple SPA running on the root URL (e.g. http://myserver:3000) and I'd like to serve it from a sub-folder (e.g. http://myserver:3000/newroot).

I've spent the day trying many things, most of them revolving around the use of a "homepage" variable in my package.json:

{
  "name": "cra_test",
  "version": "0.1.0",
  "private": true,
  "homepage": "/newroot",
...

I've found many people (and docs) reporting this as a solution but when I do so and rebuild then rerun my app (npm run build and serve -s build) it yields nothing but a blank page with the following error in the browser console: screenshot (couldn't embed it to the post).

Pardon the literal french on the first two warnings, it reads:

The script at [...] was loaded while it's MIME type (text/html) isn't a valid JavaScript MIME type

These two errors exclusively appear when I use "homepage" in my package.json. For testing purposes, I've reproduced all those steps on a freshly created (with create-react-app) app, the issue is the same, so it's most likely not due to coding errors.

After hours of trial and error, I'm basically clueless as to how to solve this. I'd be grateful for any insight on the issue or for any alternative solution to the initial problem (serving the app from a sub-folder).

Sudiukil
  • 31
  • 1
  • 3
  • I found a well written guide here: https://skryvets.com/blog/2018/09/20/an-elegant-solution-of-deploying-react-app-into-a-subdirectory/#an-overview-of-the-more-elegant-solution-with-base-url-element – AndTheGodsMadeLove Aug 26 '19 at 16:20
  • @AndTheGodsMadeLove Tried it earlier, not working (it uses the homepage variable and thus causes the same issue). – Sudiukil Aug 27 '19 at 00:05
  • you followed all the steps? 1) Convert all absolute links to relative ones - 2) Set homepage in the package.json to /subdir/ - 3) Add in your index.html - 4) Adjust the router // also u need to inject the history into the router – AndTheGodsMadeLove Aug 27 '19 at 00:44
  • I did, and as I said, simply adding the homepage variable to the package.json triggers the issue, so the rest is irrelevant. – Sudiukil Aug 27 '19 at 07:49
  • I'm facing the exact same issue right now. Any solution? – bavaza Dec 03 '21 at 17:46
  • I had a similar issue, and resolved it as discussed here: https://stackoverflow.com/q/70218890/499721 – bavaza Dec 05 '21 at 06:50

0 Answers0