Code Monkey home page Code Monkey logo

Comments (11)

AaronBeaudoin avatar AaronBeaudoin commented on May 12, 2024 2

@AndyOGo's solution looks like it wasn't tested, because with it I still get the same issue.

Here is my abridged serve.json:

{
  ...
  "rewrites": [
    {
      "source": "/some/path/:path+",
      "destination": "/:path"
    }
  ]
}

And here is my abridged curl output after running npx serve:

user@Machine project-root % curl -I http://localhost:3000/favicon.png                           
HTTP/1.1 200 OK
...

user@Machine project-root % curl -I http://localhost:3000/some/path/favicon.png      
HTTP/1.1 200 OK
...

user@Machine project-root % curl -I http://localhost:3000/assets/vendor.822f96e6.js                           
HTTP/1.1 200 OK
...

user@Machine project-root % curl -I http://localhost:3000/some/path/assets/vendor.822f96e6.js
HTTP/1.1 404 Not Found
...

from serve-handler.

erfangc avatar erfangc commented on May 12, 2024 1

This is definitely a very common task that one need to perform

Imagine you have a large organization running a commercial website call it acme.com

There are multiple React Apps, written by multiple teams, deployed to this website, for example:

  • app1
  • app2

Say the network admin has configured requests to go from acme.com/app1 to go to host1 and acme.com/app2 to host2

Where host1/host2 are machines running serve-handler ...

from serve-handler.

warren-bank avatar warren-bank commented on May 12, 2024 1

shameless self-promotion alert:
this feature is fixed in my @warren-bank/serve fork of serve

notes:

  • rewrite rules now require an engine attribute..
    which permits you to choose which methodology/library is used to:
    • match the url pathname against a pattern
    • replace capture groups

from serve-handler.

coding-watermelon avatar coding-watermelon commented on May 12, 2024

@erfangc I know that this is pretty common task. Thats why I want to do that, however, It seemed that this is not possible using serve.
Can you provide an example of how to write that correctly?

from serve-handler.

erfangc avatar erfangc commented on May 12, 2024

@PetrykowskiM I am in your boat, I am saying this is a fairly common practice so I imagine there must be a way to do it

I've tried a variety of things without much success either

from serve-handler.

coding-watermelon avatar coding-watermelon commented on May 12, 2024

Ok, good to know than! Well, would be nice if anybody else could help.

from serve-handler.

erfangc avatar erfangc commented on May 12, 2024

@PetrykowskiM I am find many things difficult with this one

first -
rewrites do not really work ... so that sucks

second -
I tried structuring the directory such that host/app/index.html corresponds to the local FS app/index.html

The second approach succeeded (but you need to disable directoryList) through the serve.json config

But then (I am assuming you are writing a SPA in Angular/React/Vue) ... then if you are using any Router(s) in the SPA, you are going to run into all sorts of unexpected problems. This is seemingly benign task is proving to be surprisingly difficult. I hope I am just being stupid here and missing something obvious

from serve-handler.

amio avatar amio commented on May 12, 2024

Seems like the proposal in #27

from serve-handler.

AndyOGo avatar AndyOGo commented on May 12, 2024

path-to-regexp supports repeated segments

const toPathRepeated = compile("/:segment+");

toPathRepeated({ segment: "foo" }); //=> "/foo"
toPathRepeated({ segment: ["a", "b", "c"] }); //=> "/a/b/c"

I think that should be the solution 🤔

{
  "rewrites": [{
    "source": "/some/frontend/:path+",
    "destination": "/:path"
  }]
}

from serve-handler.

jeffersoneagley avatar jeffersoneagley commented on May 12, 2024

Any updates here?

from serve-handler.

oguzhanakan0 avatar oguzhanakan0 commented on May 12, 2024

If you want to remove prefixes from in-app requests, continue using rewrites. That works fine.

The rewrites is intended to rewrite the url routing within the app and serve cannot know when the initial static path is requested.

If you want to serve static js/css files from a path different than /static, here is the solution.

Assume you want to serve your frontend under foo.com/frontend.

  1. In your build directory, move the static folder to frontend/static.
  2. Change index.html to point new static paths, e.g.:
    /static/js/main.xyz.js to /frontend/static/js/main.xyz.js and
    /static/css/main.xyz.css to /frontend/static/js/main.xyz.css
  3. Run serve. Make sure the request initiator chain points to /frontend/static/...:

Screenshot 2023-04-27 at 1 39 19 PM

from serve-handler.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.