2017-12-07 9 views
8

에서 특정 경로 제외하는 방법 : 내 사이트에각도 5 및 서비스 노동자 : I합니다 (<a href="https://angular.io/guide/service-worker-getting-started#step-4-create-the-configuration-file-ngsw-configjson" rel="nofollow noreferrer">docs</a>에서 가져온) <code>ngsw-config.json</code>이 ngsw-config.json

{ 
    "index": "/index.html", 
    "assetGroups": [{ 
    "name": "app", 
    "installMode": "prefetch", 
    "resources": { 
     "files": [ 
     "/favicon.ico", 
     "/index.html" 
     ], 
     "versionedFiles": [ 
     "/*.bundle.css", 
     "/*.bundle.js", 
     "/*.chunk.js" 
     ] 
    } 
    }, { 
    "name": "assets", 
    "installMode": "lazy", 
    "updateMode": "prefetch", 
    "resources": { 
     "files": [ 
     "/assets/**" 
     ] 
    } 
    }] 
} 

를 RSS 피드 /api/rss에 대한 링크가 각도 앱을로드하지 않고 새 브라우저 탭에서 열어야합니다. 요청이 index.html으로 리디렉션 된 리소스 목록에서 어떻게 제외시킬 수 있습니까?

UPD : 나는 시도했지만 다음과 같은 설정을 작동하지 (!/api/rss 참조)

{ 
    "index": "/index.html", 
    "assetGroups": [{ 
    "name": "app", 
    "installMode": "prefetch", 
    "patterns": ["!/api/rss"], 
    "resources": { 
     "files": [ 
     "/favicon.ico", 
     "/index.html", 
     "!/api/rss" 
     ], 
     "versionedFiles": [ 
     "/*.bundle.css", 
     "/*.bundle.js", 
     "/*.chunk.js" 
     ] 
    } 
    }, { 
    "name": "assets", 
    "installMode": "lazy", 
    "updateMode": "prefetch", 
    "resources": { 
     "files": [ 
     "/assets/**" 
     ] 
    } 
    }] 
} 
+0

그래서 당신은 아무것도 넣지해야 다른 장소가 아니라 각 응용 프로그램 및 서비스 작업자에서 모든 URL 또는 페이지 뷰를 가리키는 경로 '/ API/rss'를 사용하려면 해당 URL –

+0

@PannaDas에서 각도 응용 프로그램이이 URL을 제공하면 안됩니다. – ktretyak

답변

4

감사합니다 Pedro Arantesadvice에 도달하면 다음 작업 구성에 도달했습니다 (dataGroups 참조). 차 "maxAge": "0u") : 내가 잘못 아니에요 경우

{ 
    "index": "/index.html", 
    "dataGroups": 
    [ 
    { 
     "name": "api", 
     "urls": ["/api"], 
     "cacheConfig": { 
     "maxSize": 0, 
     "maxAge": "0u", 
     "strategy": "freshness" 
     } 
    } 
    ], 
    "assetGroups": 
    [ 
    { 
     "name": "app", 
     "installMode": "prefetch", 
     "resources": { 
     "files": [ 
      "/favicon.ico", 
      "/index.html" 
     ], 
     "versionedFiles": [ 
      "/*.bundle.css", 
      "/*.bundle.js", 
      "/*.chunk.js" 
     ] 
     } 
    }, 
    { 
     "name": "assets", 
     "installMode": "lazy", 
     "updateMode": "prefetch", 
     "resources": { 
     "files": [ 
      "/assets/**" 
     ] 
     } 
    } 
    ] 
} 
+0

내가 Datagroups에 넣은 모든 것은 무시 된 것 같습니다. dist (ng build --prod)로 생성 된 ngsw.json 파일에 datagroup이 없습니다. 이 구성으로 사용 했습니까, 아니면 다른 방식으로 작동합니까? – switch

+0

이것은 GET 요청에만 적용됩니다. SW 때문에 파일 진행률 막대가 작동하지 않는 경우 문제가 발생합니다. Angular가 기본 지원을 추가 할 때까지 https://github.com/angular/angular/issues/21191#issuecomment-360051276 제안 된 해결책을 수행했습니다. – doron

0

이 도움이 있으면 알려 주시기 바랍니다는 :

{ 
    "exclude": [ 
    "test.ts", 
    "**/*.spec.ts" 
    ] 
    } 
+4

StackOverflow에 오신 것을 환영합니다! 당신이 기여한 것을 보니 반갑습니다. 일반적으로 우리는 당신이 당신의 대답이 왜 적절한 대답인지에 대해 조금 설명해 줄 것을 요청합니다. – Trent

4

ngsw-configuration.json 파일 글로브 형식을 사용합니다 패턴 매칭 경로.

Patterns use a limited glob format: 

** matches 0 or more path segments. 
* matches exactly one path segment or filename segment. 
The ! prefix marks the pattern as being negative, meaning that only files that don't match the pattern will be included. 

여기서 중요한 것은 경로를 제외하는 데 사용할 수있는 ! 접두사입니다. 예를 들어, glob 패턴 !/api/rss은이 경로를 제외해야합니다.

nags-configuration.json 파일에서 경로를 제외하려면이 경로 패턴 앞에 ! 문자를 붙이기 만하면됩니다.

+1

이것이 내가 시도한 ([like this] (https://angular.io/guide/service-worker-configref#reference-configuration-file)) 첫 번째 일이지만, 작동하지 않습니다. – ktretyak

+0

'! api/rss' 또는'!/api/rss/**'와 같은 추가 변종을 사용해 보셨습니까? – Trent

4

이미 데이터 그룹을 만들려고 했습니까? dataGroups은 자산 (파일)에 assetGroups과 같은 데이터 요청에 사용됩니다.

데이터 그룹

자산 자원과는 달리, 데이터 요청 앱과 함께 을 버전이되지 않습니다. API 요청과 기타 데이터 종속성과 같은 상황에 더 유용하게 수동으로 구성된 정책에 따라 캐싱됩니다.

데이터 그룹 인터페이스 :

export interface DataGroup { 
    name: string; 
    urls: string[]; 
    version?: number; 
    cacheConfig: { 
    maxSize: number; 
    maxAge: string; 
    timeout?: string; 
    strategy?: 'freshness' | 'performance'; 
    }; 
} 

당신은 (/api/rss를 제외하는 데이터 그룹을 만들 수 있습니다 "!/api/rss"이 작동하지 않는 경우, 당신은 urls": ["/api/user", "/api/admin"]에있는 모든 사람들에게 API를 추가 할 수 있습니다

{ 
    "index": "/index.html", 
    "assetGroups": [{ 
    "name": "assetGroup1", 
    ... 
    }, { 
    "name": "assetGroup1", 
    ... 
    }], 
    "dataGroups": [{ 
    "name": "dataGroup1"; 
    "urls": ["!/api/rss"]; 
    cacheConfig: { 
     maxSize: 50; 
     maxAge: "3d12h"; 
    } 
    }, { 
    "name": "dataGroup2"; 
    "urls": ["/api/user"]; 
    cacheConfig: { 
     maxSize: 40; 
     maxAge: "3d12h"; 
    } 
    }] 
}