I have a React 16 single-page application (created with create-react-app) that gets compiled into the build directory of my Sails 1.0 application, I've updated .sailsrc to point Sails at that directory for loading static assets instead, and when I load localhost:1337 everything works perfectly: the React application is loaded and it pulls data from the endpoints of my Sails app.
The problem is that I'm using React Router (with regular URL paths, not HashRouter) and so anything that outside of the main index.html page that should hit React Router instead gets a 404 from Sails. I've come across a number of answers from the past few years around this:
- Sails.js Single Page Application (SPA) — redirect all missing/unused routes to a single controller action
- Simple page app routes to same view or controller SailsJS
- Sails js route static html
I don't want to redirect to a view or a controller, I want to simply send the static build/index.html file for any 404 responses. As for the last question in that list, I haven't been able to figure out how to wrangle that into what I want to do.
I have been able to do it by adding api/responses/notFound.js with the following content:
const fs = require('fs');
const content = fs.readFileSync('build/index.html');
module.exports = function notFound () {
return this.res.send(content.toString());
};
But that seems suuuuuper hacky. :P Is there any nicer way of accomplishing this?