Switching to node-fetch to avoid caching bugs in make-fetch-happen (#2171)

* Workaround from caching problem within make-fetch-happen for 304s.

* Switching to node-fetch

* Docs

* Fixing race conditino in build
This commit is contained in:
Joel Denning 2020-04-13 10:56:14 -06:00 committed by GitHub
parent 9a15cfd3b7
commit 0b007715a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 6 additions and 41 deletions

View File

@ -12,7 +12,7 @@ yarn add systemjs
```
```js
const { System, applyImportMap, clearFetchCache, setBaseUrl } = require('systemjs');
const { System, applyImportMap, setBaseUrl } = require('systemjs');
System.import('file:///Users/name/some-module.js').then(module => {
console.log("The loaded module", module);
@ -29,7 +29,7 @@ Separate instances of SystemJS, each with their own import map and module regist
Additionally, [global loading](/README.md#extras), [module types](/docs/module-types.md), and the [SystemJS Registry API](/docs/api.md#registry) are supported. Other extras, such as the AMD extra, have not been thoroughly tested but are presumed to work.
Modules loaded over HTTP will be cached on disk, according to the [Cache-Control](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control) header. This caching is performed by [make-fetch-happen](https://github.com/npm/make-fetch-happen).
Modules loaded over HTTP will be loaded via [node-fetch](https://github.com/node-fetch/node-fetch).
## API
@ -106,18 +106,4 @@ setBaseUrl(System, 'https://example.com/base/');
// Use a file URL as the base
setBaseUrl(System, 'file:///Users/name/some-dir/');
setBaseUrl(System, pathToFileURL(path.join(process.cwd(), 'some-dir')) + path.sep);
```
### clearFetchCache() -> undefined
SystemJS uses [make-fetch-happen](https://github.com/npm/make-fetch-happen) to manage a disk cache that respects the [`Cache-Control`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control) header. The `clearFetchCache` API clears the disk cache of modules loaded over the network.
Calling `clearFetchCache` ensures that subsequent modules will be loaded from the network instead of disk cache. This does not impact any existing SystemJS modules in the registry.
Note that the disk cache is reused by all NodeJS programs that use SystemJS on any particular machine. This includes reusing the cache after shutting down the program and restarting it.
```js
const { clearFetchCache } = require('systemjs');
clearFetchCache();
```

View File

@ -30,8 +30,9 @@
"concurrently": "^5.1.0",
"construct-style-sheets-polyfill": "^2.3.5",
"cross-env": "^7.0.2",
"make-fetch-happen": "^8.0.4",
"mkdirp": "^1.0.4",
"mocha": "^7.1.1",
"node-fetch": "^2.6.0",
"open": "^7.0.3",
"rimraf": "^3.0.2",
"rollup": "^2.2.0",
@ -42,7 +43,7 @@
"whatwg-fetch": "^3.0.0"
},
"scripts": {
"build": "rimraf dist && concurrently -n w: 'npm:build:*'",
"build": "rimraf dist && mkdirp dist && concurrently -n w: 'npm:build:*'",
"build:node": "node --experimental-modules build-node.js",
"build:browser": "rollup -c",
"build-browser-dev": "rollup -c --environment dev",

View File

@ -1,30 +1,10 @@
import sourceMapSupport from 'source-map-support';
import makeFetchHappen from 'make-fetch-happen';
import path from 'path';
import fetch from 'node-fetch';
import { promises as fs } from 'fs';
import { fileURLToPath } from 'url';
import rimraf from 'rimraf';
import os from 'os';
export let clearFetchCache;
sourceMapSupport.install();
const home = os.homedir();
let cacheDir;
if (process.platform === 'darwin')
cacheDir = path.join(home, 'Library', 'Caches', 'systemjs');
else if (process.platform === 'win32')
cacheDir = path.join(process.env.LOCALAPPDATA || path.join(home, 'AppData', 'Local'), 'systemjs-cache');
else
cacheDir = path.join(process.env.XDG_CACHE_HOME || path.join(home, '.cache'), 'systemjs');
clearFetchCache = function () {
rimraf.sync(path.join(cacheDir, 'fetch-cache'));
};
const fetch = makeFetchHappen.defaults({ cacheManager: path.join(cacheDir, 'fetch-cache')});
global.System.constructor.prototype.shouldFetch = () => true;
global.System.constructor.prototype.fetch = async url => {
if (url.startsWith('file:')) {

View File

@ -23,8 +23,6 @@ export function applyImportMap(loader, newMap, mapBase) {
loader[IMPORT_MAP_PROMISE] = Promise.resolve();
}
export { clearFetchCache } from './features/node-fetch.js';
export function setBaseUrl(loader, url) {
ensureValidSystemLoader(loader);
loader[BASE_URL] = new URL(url).href;