serve icon indicating copy to clipboard operation
serve copied to clipboard

Testing local deployments for GitHub pages doesn't work since no `basePath` can be set

Open hybridherbst opened this issue 1 year ago • 6 comments

Description

serve is great! However, there are a number of scenarios that can't be tested with it, for example vite or sveltekit builds with basePath set or deployments to GitHub pages which also require a basePath.

I found this old issue but I think there are more than enough usecases, it's not just an "edge case", so thought it's worth bringing up again: Related to https://github.com/vercel/serve/issues/184

Library version

14.2.1

Node version

v18.13.0

hybridherbst avatar Sep 14 '23 20:09 hybridherbst

I would also love this, but not sure if anyone is looking into it :)

danantal avatar Feb 01 '24 15:02 danantal

This can be achieved with a symbolic link. For example, if my public folder is named dist, I would call:

ln -snf "`pwd`/dist" dist/<basePath> && serve dist

The pwd call is needed in order to create the link with an absolute path.

dsogari avatar Mar 08 '24 17:03 dsogari

Hm, maybe I’m misunderstanding what you mean — the files would still be served without the basePath as part of the URL, right?

hybridherbst avatar Mar 09 '24 08:03 hybridherbst

@hybridherbst They would be served both with and w/o the base path. For testing local deployments (which is what we're trying to achieve), this should be enough.

dsogari avatar Mar 09 '24 10:03 dsogari

I see. So it would explicitly not surface issues related to wrong base path usage (e.g. an accidental reference to toplevel instead of the base path) since the same files would be at root level as well.

hybridherbst avatar Mar 11 '24 07:03 hybridherbst

I came across the same thing and, in my case I was using Vite. I ended up creating a custom handler and adding some rewrites configs, it's working fine so far.

Example:

const handler = require("serve-handler");
const http = require("http");

const basePath = 'example';
const PUBLIC_PATH = "build";

const server = http.createServer((request, response) => {
  return handler(request, response, {
    public: PUBLIC_PATH,
    rewrites: [
      {
        source: `/${basePath}/:assets/*.js`,
        destination: `/:assets/:0.js`,
      },
      {
        source: `/${basePath}/:assets/*.svg`,
        destination: `/:assets/:0.svg`,
      },
      { source: `/${basePath}/**`, destination: "/index.html" },
    ],
  });
});

server.listen(3000, () => {
  console.log("Running at http://localhost:3000");
});

ivmarcos avatar May 15 '24 14:05 ivmarcos