serve icon indicating copy to clipboard operation
serve copied to clipboard

Add ability to serve SPA from non root

Open jbojcic1 opened this issue 5 years ago • 2 comments

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.

jbojcic1 avatar Sep 11 '19 09:09 jbojcic1

Any news on this ? This could be nice

TomPradat avatar Feb 12 '20 13:02 TomPradat

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)
    • 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
          }
        

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
  • 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
      }
    

warren-bank avatar Feb 16 '22 08:02 warren-bank