Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--single serves index.html even if files exist #722

Open
danielroe opened this issue Jul 29, 2022 · 5 comments
Open

--single serves index.html even if files exist #722

danielroe opened this issue Jul 29, 2022 · 5 comments

Comments

@danielroe
Copy link

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

@gamemaker1
Copy link
Contributor

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 :]

@Backspaze
Copy link

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?

@nvmnghia
Copy link

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

@Backspaze
Copy link

Backspaze commented Sep 29, 2022

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.

@dvester
Copy link

dvester commented Dec 29, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants