-
Notifications
You must be signed in to change notification settings - Fork 792
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(next): fix ts readDirectory, use fetch cache
- Loading branch information
1 parent
6fd43e7
commit 18360ca
Showing
21 changed files
with
418 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { HAS_FETCH_CACHE } from '../environment'; | ||
import { isString } from '@utils'; | ||
import { version } from '../../../version'; | ||
|
||
|
||
const activeFetches = new Map<string, Promise<Response>>(); | ||
|
||
export const cachedFetch = async (url: string) => { | ||
const immutableCache = HAS_FETCH_CACHE ? await caches.open(CACHES.immutable) : null; | ||
if (immutableCache) { | ||
const cachedImmutableRsp = await immutableCache.match(url); | ||
if (cachedImmutableRsp) { | ||
return cachedImmutableRsp; | ||
} | ||
} | ||
|
||
if (HAS_FETCH_CACHE && isCoreResource(url)) { | ||
const coreCache = await caches.open(CACHES.core); | ||
const cachedCoreRsp = await coreCache.match(url); | ||
if (cachedCoreRsp) { | ||
return cachedCoreRsp; | ||
} | ||
} | ||
|
||
const activeFetch = activeFetches.get(url); | ||
if (activeFetch) { | ||
return activeFetch; | ||
} | ||
|
||
try { | ||
const fetchPromise = fetch(url); | ||
activeFetches.set(url, fetchPromise); | ||
|
||
const fetchRsp = await fetchPromise; | ||
|
||
if (HAS_FETCH_CACHE && fetchRsp.ok) { | ||
if (isCoreResource(url)) { | ||
const coreCache = await caches.open(CACHES.core); | ||
coreCache.put(url, fetchRsp.clone()); | ||
|
||
} else if (isImmutableResponse(fetchRsp)) { | ||
immutableCache.put(url, fetchRsp.clone()); | ||
|
||
} else if (HAS_FETCH_CACHE) { | ||
const offlineCache = await caches.open(CACHES.offline); | ||
offlineCache.put(url, fetchRsp.clone()); | ||
} | ||
} | ||
|
||
return fetchRsp; | ||
|
||
} catch (e) { | ||
if (HAS_FETCH_CACHE) { | ||
const offlineReqCache = await caches.open(CACHES.offline); | ||
const cachedOfflineRsp = await offlineReqCache.match(url); | ||
if (cachedOfflineRsp) { | ||
return cachedOfflineRsp; | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
export const isImmutableResponse = (rsp: Response) => { | ||
if (rsp && rsp.ok && rsp.headers) { | ||
const cacheControl = rsp.headers.get('Cache-Control'); | ||
if (isString(cacheControl)) { | ||
if (cacheControl.includes('immutable')) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
}; | ||
|
||
export const cleanFetchCache = async () => { | ||
if (HAS_FETCH_CACHE) { | ||
try { | ||
const currentCacheKeys = Object.values(CACHES); | ||
const cacheKeys = await caches.keys(); | ||
|
||
const invalidCacheKeys = cacheKeys.filter(k => { | ||
return !currentCacheKeys.includes(k); | ||
}); | ||
|
||
await Promise.all(invalidCacheKeys.map(k => { | ||
return caches.delete(k); | ||
})); | ||
|
||
} catch (e) {} | ||
} | ||
}; | ||
|
||
export const CACHES = { | ||
core: `stencil_core_${version}`, | ||
immutable: `stencil_immutable`, | ||
offline: `stencil_offline`, | ||
}; | ||
|
||
|
||
const isCoreResource = (url: string) => CORE_RESOURCES.some(u => url.includes(u)); | ||
|
||
const CORE_RESOURCES = [ | ||
'/@stencil/core/', | ||
'/@stencil/core@', | ||
'/typescript/', | ||
'/typescript@', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,31 @@ | ||
import * as d from '../../../declarations'; | ||
import { cachedFetch } from './fetch-cache'; | ||
import { known404Urls } from './fetch-utils'; | ||
import { skipFilePathFetch, skipUrlFetch } from '../fetch/fetch-utils'; | ||
import { writeFetchSuccess } from './write-fetch-success'; | ||
import { writeFetchSuccessAsync } from './write-fetch-success'; | ||
|
||
|
||
const fetchCacheAsync = new Map<string, Promise<string>>(); | ||
|
||
|
||
export const fetchModuleAsync = (inMemoryFs: d.InMemoryFileSystem, pkgVersions: Map<string, string>, url: string, filePath: string) => { | ||
export const fetchModuleAsync = async (inMemoryFs: d.InMemoryFileSystem, pkgVersions: Map<string, string>, url: string, filePath: string) => { | ||
if (skipFilePathFetch(filePath) || known404Urls.has(url) || skipUrlFetch(url)) { | ||
return Promise.resolve(undefined); | ||
return undefined; | ||
} | ||
|
||
let fetchPromise = fetchCacheAsync.get(url); | ||
|
||
if (!fetchPromise) { | ||
fetchPromise = new Promise(resolve => { | ||
fetch(url) | ||
.then(async rsp => { | ||
if (rsp.status >= 200 && rsp.status < 300) { | ||
const content = await rsp.text(); | ||
writeFetchSuccess(inMemoryFs, url, filePath, content, pkgVersions); | ||
resolve(content); | ||
|
||
} else { | ||
known404Urls.add(url); | ||
resolve(undefined); | ||
} | ||
}) | ||
.catch(() => { | ||
known404Urls.add(url); | ||
resolve(undefined); | ||
}); | ||
}); | ||
fetchCacheAsync.set(url, fetchPromise); | ||
try { | ||
const rsp = await cachedFetch(url); | ||
if (rsp) { | ||
if (rsp.ok) { | ||
const content = await rsp.clone().text(); | ||
writeFetchSuccessAsync(inMemoryFs, url, filePath, content, pkgVersions); | ||
return content; | ||
} | ||
|
||
if (rsp.status === 404) { | ||
known404Urls.add(url); | ||
} | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
|
||
return fetchPromise; | ||
return undefined; | ||
}; |
Oops, something went wrong.