serve icon indicating copy to clipboard operation
serve copied to clipboard

`--single` serves `index.html` even if files exist

Open danielroe opened this issue 3 years ago • 5 comments

Description

The description for --single is: Rewrite all not-found requests to index.html.

However, if page.html exists, /page.html redirects to /page which is then served with index.html.

Here is a sandbox.

Library version

14.0.1

Node version

16.14.2

danielroe avatar Jul 29 '22 10:07 danielroe

I think this intended behaviour, but it makes more sense to serve it only if the requested page does not exist. This would mean adding a single option to the configuration of serve-handler instead of just adding ** to the redirects list.

I'd be willing to make a PR that addresses this if needed :]

gamemaker1 avatar Jul 29 '22 14:07 gamemaker1

Correct me if I'm wrong here but entering the code below in serve.json should be equivalent of running serve -s right?

{
  "rewrites": [
    { "source": "/**", "destination": "/index.html" }    
  ]
}

In any case, I was about to post an issue regarding this when I found this issue. I've got a lab set up at https://gitlab.com/Backspaze/navigo-lab-2 which is published at https://navigo-lab-2.lovgren.io/. Download and run it locally with serve -s if you want another example of this issue. It's a lab for a SPA I'm working on and initially I was working on it locally using serve and thought that this issue was caused by the JS router I'm using (Navigo). Imagine my surprise when I uploaded the project to GitLab Pages and everything was working fine there...

@gamemaker1 Do you have any further information regarding this issue?

Backspaze avatar Sep 25 '22 17:09 Backspaze

-s forces my .well-known/apple-app-site-association back to index.html. Is there any solution for this?

nvmnghia avatar Sep 29 '22 02:09 nvmnghia

For those interested in an alternative while this issue is being worked on, I tried lwsjs/local-web-server which worked fine for my use case. It doesn't behave exactly like the web server in GitLab Pages but the difference is minor enough that it doesn't cause any real issues for me.

Backspaze avatar Sep 29 '22 16:09 Backspaze

The fix for this is to create the apple-app-site-association as apple-app-site-association.json and then create a rewrite rule inside of a file in your public directory called serve.json. The contents of that file should look like this.

{
  "rewrites": [
    {
      "source": "!.well-known/**",
      "destination": "index.html"
    },
    {
      "source": ".well-known/apple-app-site-association",
      "destination": ".well-known/apple-app-site-association.json"
    }
  ]
}

Then when you start your static file server, you don't use the -s or --single argument. You start it using the following command.

serve -c serve.json

The first item will send all requests that don't exist inside of .well-known to index.html, and the second rewrite will send requests for that specific URL to the apple-app-site-association.json file and assign a content-type: application/json header to it. All other files with file extensions should be served as normal, ignoring any rewrite rules.

dvester avatar Dec 29 '22 16:12 dvester