serve
serve copied to clipboard
Add ability to serve SPA from non root
Sometimes you need to serve the SPA from non root directory. For example let's say that I have a react app and I need to serve it on /portal, in my index html I will have paths like /portal/static/css/main.dce7bb36.chunk.css
Currently server is just returning index.html for those paths.
Any news on this ? This could be nice
shameless self-promotion alert:
this feature is available in my @warren-bank/serve
fork of serve
option 1:
- serve a directory that does contain a
portal
subdirectory - don't use the
--single
option- in
serve
(upstream), this is completely broken- it adds a redirect rule that sends all requests to
/index.html
- this rule prevents the loading of any external resources (ex: your css file)
- it adds a redirect rule that sends all requests to
- in my fork, this isn't quite what you want
- it adds a redirect rule that sends only requests that would trigger a 404 status code to
/index.html
- since you want to use a different path for your SPA,
you'll want to write your own redirect rule that is nearly identical:{ "engine": "text", "source": "statusCode:404", "destination": "/portal/index.html", "exact": true, "terminal": true }
- it adds a redirect rule that sends only requests that would trigger a 404 status code to
- in
option 2:
- serve a directory that does not contain a
portal
subdirectory - do use the
--single
option- since your SPA can now be loaded at the default path:
/index.html
- since your SPA can now be loaded at the default path:
- add a rewrite rule for static resources that ignores a leading
/portal
in requested pathnames:{ "engine": "regex", "source": "^/portal(/.*)$", "destination": "$1", "flags": "i", "terminal": true }