Repeated segments should be supported by passing array to pathToRegExp.compile

Đang mở
#98 0 bình luận 2 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
45/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
javascript
Lĩnh vực
backend

Hướng nghiên cứu

Start at toTarget in src/index.js around lines 79-86 and compare the path-to-regexp compile documentation with how repeated parameters are assigned. Add coverage for repeated segments in rewrites and redirects, then update the documentation with the required destination pattern and verify the affected tests pass.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Context
  • Serving react app under /app prefix e.g. example.com/app/products
  • Installed serve and customized with serve.json
  • Used "homepage":"/app" in package.json according to CRA docs
  • Asset path is correctly inferred and generated but serve is not serving asset files correctly under build/static/...

serve.json used for above:

{
  "public": "build",
  "rewrites": [
    { "source": "/app/static/:segment*", "destination": "/static/:segment*" },
    { "source": "/app", "destination": "/index.html"}.
    { "source": "/app/**", "destination": "/index.html"}
  ]
}

After some debugging, I identified the issue to be in toTarget. We are not passing in array of parameters when we are compiling the valid path https://github.com/zeit/serve-handler/blob/e7c4e42a63e9cd59b43256bb4ca04a22a5f77994/src/index.js#L79-L86

We should be passing array according to the docs(v2.2.1 that we are using). If not it treats the whole string as a single segment value and encodes / .

Debug logs:

// applyRewrites
relativePath: "/app/static/css/2.c7bb94bc.chunk.css" //argument
rewrittenPath: "/app/%2Fcss%2F2.c7bb94bc.chunk.css" // return value

// toTarget
props: {
  "segment": "/css/2.c7bb94bc.chunk.css"
}
normalizedDest: "/static/:segment*"
toPath(props): "/static/%2Fcss%2F2.c7bb94bc.chunk.css"

This should be easily fixed by doing a split by / when we are setting the props i.e. props[name] = results[index + 1].split("/");

This will fix repeated segment for redirects as well.

Documents should also be updated with instructions on how to use repeated segment. (I figured out during debugging that destination needs * as well else the compile function will complain that the url built does not match the destination pattern 😅)

Not sure if my proposed solution is correct but it works locally for me so far. I am happy to work on this with tests and docs for repeated segment

Ngôn ngữ chính
JavaScript
Star
617
Fork
120
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của vercel/serve-handler

Tất cả issue của vercel/serve-handler

Issue tương tự

Thêm issue về JavaScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.