feat: Configurable normalizeHref

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

Maintainer thường phản hồi trong vòng 1 ngày

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

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
typescript
Lĩnh vực
build-system

Hướng nghiên cứu

Bắt đầu bằng cách xác định crawlAnchorsForNextUrls và standardNormalizeHref, sau đó kiểm tra cách các tùy chọn của prerenderConfig được định nghĩa và sử dụng. Xác nhận hook normalizeHref có thể cấu hình nên tương tác như thế nào với hành vi dấu gạch chéo ở cuối hiện có, đồng thời bổ sung coverage cho thấy hành vi đã cấu hình được áp dụng trong khi hành vi mặc định vẫn không thay đổi.

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

Mô tả

Feature: Want this? Upvote it!
Prerequisites
Describe the Feature Request

In order to prerender pages it would be great to configure the behaviour of normalizing the href.

Describe the Use Case

I have hrefs with and without trailing slash. The only thing which can be configured is using trailing slash or not. If setting trailing slash to true all href will have a trailing slash in the end, if set to false all href won`t have a trailing slash. I would suggest to add the possibility make this configurable. Either by adding a the possibility to do something like trailingSlash: 'source' in the config or by adding the possibility to overwrite the standardNormalizeHref function.

Describe Preferred Solution

option number 1

const standardNormalizeHref = (prerenderConfig, diagnostics, url) => {
  try {
    if (url != null && typeof url.href === 'string') {
      let href = url.href.trim();
      if (prerenderConfig.trailingSlash === true) {
        // url should have a trailing slash
        if (!href.endsWith('/')) {
          const parts = url.pathname.split('/');
          const lastPart = parts[parts.length - 1];
          if (!lastPart.includes('.')) {
            // does not end with a slash and last part does not have a dot
            href += '/';
          }
        }
      }
      else if (prerenderConfig.trailingSlash === false ){
        // url should NOT have a trailing slash
        if (href.endsWith('/') && url.pathname !== '/') {
          // this has a trailing slash and it's not the root path
          href = href.slice(0, -1);
        }
      }
      return href;
    }
  }
  catch (e) {
    catchError(diagnostics, e);
  }
  return null;
};

option number 2

const crawlAnchorsForNextUrls = (prerenderConfig, diagnostics, baseUrl, currentUrl, parsedAnchors) => {
  if (!Array.isArray(parsedAnchors) || parsedAnchors.length === 0) {
    return [];
  }
  const basePathParts = baseUrl.pathname.split('/');
  // filterAnchor(): filter which anchors to actually crawl
  // normalizeUrl(): normalize href strings into URL objects
  // filterUrl(): filter which urls to actually crawl
  // normalizeHref(): normalize URL objects into href strings
  return parsedAnchors
    .filter((anchor) => {
    // filter which anchors to actually crawl
    if (typeof prerenderConfig.filterAnchor === 'function') {
      // user filterAnchor()
      try {
        const userFilterAnchor = prerenderConfig.filterAnchor(anchor, currentUrl);
        if (userFilterAnchor === false) {
          return false;
        }
      }
      catch (e) {
        // user filterAnchor() error
        catchError(diagnostics, e);
        return false;
      }
    }
    // standard filterAnchor()
    return standardFilterAnchor(diagnostics, anchor);
  })
    .map((anchor) => {
    // normalize href strings into URL objects
    if (typeof prerenderConfig.normalizeUrl === 'function') {
      try {
        // user normalizeUrl()
        const userNormalizedUrl = prerenderConfig.normalizeUrl(anchor.href, currentUrl);
        // standard normalizeUrl(), after user normalized
        return standardNormalizeUrl(diagnostics, userNormalizedUrl.href, currentUrl);
      }
      catch (e) {
        // user normalizeUrl() error
        catchError(diagnostics, e);
      }
    }
    // standard normalizeUrl(), no user normalized
    return standardNormalizeUrl(diagnostics, anchor.href, currentUrl);
  })
    .filter((url) => {
    // filter which urls to actually crawl
    if (typeof prerenderConfig.filterUrl === 'function') {
      // user filterUrl()
      try {
        const userFilterUrl = prerenderConfig.filterUrl(url, currentUrl);
        if (userFilterUrl === false) {
          return false;
        }
      }
      catch (e) {
        // user filterUrl() error
        catchError(diagnostics, e);
        return false;
      }
    }
    // standard filterUrl()
    return standardFilterUrl(diagnostics, url, currentUrl, basePathParts);
  })
    .map((url) => {
    

if (typeof prerenderConfig.normalizeHref === 'function') {
      try {
        // user normalizeHref()
        return  prerenderConfig.normalizeHref(prerenderConfig, diagnostics, url);
      }
      catch (e) {
        // user normalizeUrl() error
        catchError(diagnostics, e);
      }
    }
// standard normalize href
    // normalize URL objects into href strings
    return standardNormalizeHref(prerenderConfig, diagnostics, url);
  })
    .reduce((hrefs, href) => {
    // remove any duplicate hrefs from the array
    if (!hrefs.includes(href)) {
      hrefs.push(href);
    }
    return hrefs;
  }, [])
    .sort((a, b) => {
    // sort the hrefs so the urls with the least amount
    // of directories are first, then by alphabetical
    const partsA = a.split('/').length;
    const partsB = b.split('/').length;
    if (partsA < partsB)
      return -1;
    if (partsA > partsB)
      return 1;
    if (a < b)
      return -1;
    if (a > b)
      return 1;
    return 0;
  });
};
Describe Alternatives

No response

Related Code

No response

Additional Information

No response

Ngôn ngữ chính
TypeScript
Star
13.2k
Fork
865
Merge trung bình
2 ngày 16 giờ
Pull request đã merge (30 ngày)
29

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

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 stenciljs/core

Tất cả issue của stenciljs/core

Issue tương tự

Thêm issue về TypeScript

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.