From a4f55edf449e23b4e14626b59a4469cf345f8c12 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Mon, 2 Aug 2021 00:30:48 -0400 Subject: [PATCH 1/3] refactor: remove axios, use fetch --- package.json | 1 - src/utils/fetchFeeds.ts | 10 +++++----- yarn.lock | 12 +----------- 3 files changed, 6 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index 73c1044..5accf67 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,6 @@ "dependencies": { "@material-ui/core": "^4.12.1", "@material-ui/icons": "^4.11.2", - "axios": "^0.21.1", "htmlparser2": "^6.1.0", "md5": "^2.3.0", "react": "^17.0.2", diff --git a/src/utils/fetchFeeds.ts b/src/utils/fetchFeeds.ts index 21a6cb3..352e750 100644 --- a/src/utils/fetchFeeds.ts +++ b/src/utils/fetchFeeds.ts @@ -1,4 +1,3 @@ -import axios from 'axios' import { parseFeed } from 'htmlparser2' import type { Feed } from '../types' @@ -65,12 +64,13 @@ export default async function fetchFeeds( if (!forceRefetch && lastPull > getRefetchThreshold()) return storedFeedData - const response = await axios.get('/.netlify/functions/rss-proxy', { - params: { url }, - }) + const response = await fetch( + `/.netlify/functions/rss-proxy?url=${url}`, + ) + const responseData = await response.text() try { - const newFeedData = parseFeed(response.data) + const newFeedData = parseFeed(responseData) const newFeed = processFeedXML(newFeedData) const mergedFeeds = storedFeedData ? mergeFeeds(storedFeedData, newFeed) diff --git a/yarn.lock b/yarn.lock index d8a1dda..3426a61 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4793,15 +4793,6 @@ __metadata: languageName: node linkType: hard -"axios@npm:^0.21.1": - version: 0.21.1 - resolution: "axios@npm:0.21.1" - dependencies: - follow-redirects: ^1.10.0 - checksum: c87915fa0b18c15c63350112b6b3563a3e2ae524d7707de0a73d2e065e0d30c5d3da8563037bc29d4cc1b7424b5a350cb7274fa52525c6c04a615fe561c6ab11 - languageName: node - linkType: hard - "axobject-query@npm:^2.2.0": version: 2.2.0 resolution: "axobject-query@npm:2.2.0" @@ -8668,7 +8659,7 @@ __metadata: languageName: node linkType: hard -"follow-redirects@npm:^1.0.0, follow-redirects@npm:^1.10.0": +"follow-redirects@npm:^1.0.0": version: 1.14.1 resolution: "follow-redirects@npm:1.14.1" peerDependenciesMeta: @@ -15776,7 +15767,6 @@ __metadata: "@tophat/eslint-import-resolver-require": ^0.1.3 "@typescript-eslint/eslint-plugin": ^4.28.3 "@typescript-eslint/parser": ^4.28.3 - axios: ^0.21.1 eslint: ^7.30.0 eslint-config-prettier: ^8.3.0 eslint-import-resolver-node: ^0.3.4 -- 2.45.2 From 52a0b7bd09400b1c68f9d9422414b81485b2cb45 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Mon, 2 Aug 2021 00:35:44 -0400 Subject: [PATCH 2/3] refactor: remove md5 --- package.json | 1 - src/utils/persistence.ts | 10 ++++++---- yarn.lock | 28 +--------------------------- 3 files changed, 7 insertions(+), 32 deletions(-) diff --git a/package.json b/package.json index 5accf67..b3058aa 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,6 @@ "@material-ui/core": "^4.12.1", "@material-ui/icons": "^4.11.2", "htmlparser2": "^6.1.0", - "md5": "^2.3.0", "react": "^17.0.2", "react-dom": "^17.0.2" } diff --git a/src/utils/persistence.ts b/src/utils/persistence.ts index 993af84..31ae474 100644 --- a/src/utils/persistence.ts +++ b/src/utils/persistence.ts @@ -1,10 +1,12 @@ -import md5 from 'md5' - import type { RSSData, Settings } from '../types' const SETTINGS_KEY = 'settings' const RSS_DATA_KEY_PREFIX = 'savedItems_' +function cacheKeyFromURL(url: string): string { + return url.replace(/^https?:\/\//, '').replace(/\/$/, '') +} + function storeToLocal(key: string, data): void { window.localStorage.setItem(key, JSON.stringify(data)) } @@ -26,11 +28,11 @@ export function restoreSettings(): Settings { } export function storeRssData(url: string, items: Item[]): void { - const key = RSS_DATA_KEY_PREFIX + md5(url) + const key = RSS_DATA_KEY_PREFIX + cacheKeyFromURL(url) storeToLocal(key, items) } export function restoreRssData(url: string): RSSData { - const key = RSS_DATA_KEY_PREFIX + md5(url) + const key = RSS_DATA_KEY_PREFIX + cacheKeyFromURL(url) return restoreFromLocal(key) } diff --git a/yarn.lock b/yarn.lock index 3426a61..e767d64 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5617,13 +5617,6 @@ __metadata: languageName: node linkType: hard -"charenc@npm:0.0.2": - version: 0.0.2 - resolution: "charenc@npm:0.0.2" - checksum: 81dcadbe57e861d527faf6dd3855dc857395a1c4d6781f4847288ab23cffb7b3ee80d57c15bba7252ffe3e5e8019db767757ee7975663ad2ca0939bb8fcaf2e5 - languageName: node - linkType: hard - "chokidar@npm:^3.0.2": version: 3.5.2 resolution: "chokidar@npm:3.5.2" @@ -6420,13 +6413,6 @@ __metadata: languageName: node linkType: hard -"crypt@npm:0.0.2": - version: 0.0.2 - resolution: "crypt@npm:0.0.2" - checksum: baf4c7bbe05df656ec230018af8cf7dbe8c14b36b98726939cef008d473f6fe7a4fad906cfea4062c93af516f1550a3f43ceb4d6615329612c6511378ed9fe34 - languageName: node - linkType: hard - "crypto-browserify@npm:^3.12.0": version: 3.12.0 resolution: "crypto-browserify@npm:3.12.0" @@ -10058,7 +10044,7 @@ __metadata: languageName: node linkType: hard -"is-buffer@npm:^1.1.5, is-buffer@npm:~1.1.6": +"is-buffer@npm:^1.1.5": version: 1.1.6 resolution: "is-buffer@npm:1.1.6" checksum: 4a186d995d8bbf9153b4bd9ff9fd04ae75068fe695d29025d25e592d9488911eeece84eefbd8fa41b8ddcc0711058a71d4c466dcf6f1f6e1d83830052d8ca707 @@ -12308,17 +12294,6 @@ __metadata: languageName: node linkType: hard -"md5@npm:^2.3.0": - version: 2.3.0 - resolution: "md5@npm:2.3.0" - dependencies: - charenc: 0.0.2 - crypt: 0.0.2 - is-buffer: ~1.1.6 - checksum: a63cacf4018dc9dee08c36e6f924a64ced735b37826116c905717c41cebeb41a522f7a526ba6ad578f9c80f02cb365033ccd67fe186ffbcc1a1faeb75daa9b6e - languageName: node - linkType: hard - "mdn-data@npm:2.0.14": version: 2.0.14 resolution: "mdn-data@npm:2.0.14" @@ -15779,7 +15754,6 @@ __metadata: eslint-plugin-react-hooks: ^4.2.0 htmlparser2: ^6.1.0 jest: ^27.0.6 - md5: ^2.3.0 netlify-cli: ^5.2.3 parcel: ^2.0.0-beta.2 prettier: ^2.3.2 -- 2.45.2 From de6904253a43a6db246acddaab59a19fd4e28d96 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Mon, 2 Aug 2021 00:49:39 -0400 Subject: [PATCH 3/3] fix: handle failed fetches --- src/utils/fetchFeeds.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/utils/fetchFeeds.ts b/src/utils/fetchFeeds.ts index 352e750..a0d80de 100644 --- a/src/utils/fetchFeeds.ts +++ b/src/utils/fetchFeeds.ts @@ -67,6 +67,9 @@ export default async function fetchFeeds( const response = await fetch( `/.netlify/functions/rss-proxy?url=${url}`, ) + + if (!response.ok) return storedFeedData + const responseData = await response.text() try { @@ -87,5 +90,5 @@ export default async function fetchFeeds( }), ) - return feeds + return feeds.filter((feed) => feed) } -- 2.45.2