8000 fix(service-worker): only consider GET requests as navigation requests · angular/angular@ac023f7 · GitHub
[go: up one dir, main page]

Skip to content

Commit ac023f7

Browse files
committed
fix(service-worker): only consider GET requests as navigation requests
Previously, the criteria for determining if a request was a [navigation request][1] did not account for the request method. This incorrectly identified HTML form submit POST requests as navigation requests and served `index.html` instead of passing them through to the server, thus breaking the form submission. This commit fixes this by ensuring that only GET requests are considered navigation requests. > **Note** > HTML forms with their method set to `GET` will still be affected by > the issue. This is not a big concern, because using `GET` for form > submission is quite uncommon and generally discouraged (due to > limitations and security considerations). [1]: https://angular.io/guide/service-worker-config#handling-navigation-requests Fixes #36368
1 parent 0afb895 commit ac023f7

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

aio/content/guide/service-worker-config.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ This optional section enables you to specify a custom list of URLs that will be
370370
The ServiceWorker redirects navigation requests that don't match any `asset` or `data` group to the specified [index file](#index-file).
371371
A request is considered to be a navigation request if:
372372

373+
* Its [method](https://developer.mozilla.org/docs/Web/API/Request/method) is `GET`
373374
* Its [mode](https://developer.mozilla.org/docs/Web/API/Request/mode) is `naviga 8000 tion`
374375
* It accepts a `text/html` response as determined by the value of the `Accept` header
375376
* Its URL matches the following criteria:

packages/service-worker/worker/src/app-version.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,10 @@ export class AppVersion implements UpdateSource {
203203

204204
/**
205205
* Determine whether the request is a navigation request.
206-
* Takes into account: Request mode, `Accept` header, `navigationUrls` patterns.
206+
* Takes into account: Request method and mode, `Accept` header, `navigationUrls` patterns.
207207
*/
208208
isNavigationRequest(req: Request): boolean {
209-
if (req.mode !== 'navigate') {
209+
if (req.method !== 'GET' || req.mode !== 'navigate') {
210210
return false;
211211
}
212212

packages/service-worker/worker/test/happy_spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,6 +1704,14 @@ describe('Driver', () => {
17041704
server.assertNoOtherRequests();
17051705
});
17061706

1707+
it('does not redirect to index on a non-GET request', async () => {
1708+
expect(await navRequest('/baz', {method: 'POST'})).toBeNull();
1709+
server.assertSawRequestFor('/baz');
1710+
1711+
expect(await navRequest('/qux', {method: 'PUT'})).toBeNull();
1712+
server.assertSawRequestFor('/qux');
1713+
});
1714+
17071715
it('does not redirect to index on a non-navigation request', async () => {
17081716
expect(await navRequest('/baz', {mode: undefined})).toBeNull();
17091717
server.assertSawRequestFor('/baz');

0 commit comments

Comments
 (0)
0