Skip to content

Commit

Permalink
only cache extension resources, #75061
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Jul 9, 2019
1 parent 28e07ec commit 077fb1b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const _pending = new Map<string, Function>();
export function handleMessageEvent(event: MessageEvent): void {
const fn = _pending.get(event.data.token);
if (fn) {
fn(event.data.data);
fn(event.data.data, event.data.isExtensionResource);
_pending.delete(event.data.token);
}
}
Expand Down Expand Up @@ -49,16 +49,24 @@ export async function handleFetchEvent(event: any): Promise<Response | undefined
_pending.delete(token);
}, 5000);

_pending.set(token, (data: ArrayBuffer) => {
_pending.set(token, (data: ArrayBuffer, isExtensionResource: boolean) => {
clearTimeout(handle);
const res = new Response(data, {
status: 200,
headers: { 'Content-Type': getMediaMime(URI.parse(url.query).path) || 'text/plain' }
});
caches.open(cacheName).then(cache => {
cache.put(event.request, res.clone());

if (!isExtensionResource) {
// only cache extension resources but not other
// resources, esp not workspace resources
resolve(res);
});

} else {
caches.open(cacheName).then(cache => {
cache.put(event.request, res.clone());
resolve(res);
});
}
});

const client = await clients.get(event.clientId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import { URI } from 'vs/base/common/uri';
import { Registry } from 'vs/platform/registry/common/platform';
import { IWorkbenchContributionsRegistry, Extensions } from 'vs/workbench/common/contributions';
import { LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle';
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions';
import { isEqualOrParent } from 'vs/base/common/resources';

class ResourceServiceWorker {

private readonly _disposables = new DisposableStore();

constructor(
@IFileService private readonly _fileService: IFileService,
@IExtensionService private readonly _extensionService: IExtensionService,
) {
this._initServiceWorker();
this._initFetchHandler();
Expand All @@ -41,17 +44,32 @@ class ResourceServiceWorker {

const fetchListener: (this: ServiceWorkerContainer, ev: MessageEvent) => void = event => {
const uri = URI.parse(event.data.uri);
this._fileService.readFile(uri).then(file => {

Promise.all([
this._fileService.readFile(uri),
this._isExtensionResource(uri)
]).then(([file, isExtensionResource]) => {

// todo@joh typings
(<any>event.source).postMessage({
token: event.data.token,
data: file.value.buffer.buffer
data: file.value.buffer.buffer,
isExtensionResource
}, [file.value.buffer.buffer]);
});
};
navigator.serviceWorker.addEventListener('message', fetchListener);
this._disposables.add(toDisposable(() => navigator.serviceWorker.removeEventListener('message', fetchListener)));
}

private async _isExtensionResource(uri: URI): Promise<boolean> {
for (const ext of await this._extensionService.getExtensions()) {
if (isEqualOrParent(uri, ext.extensionLocation)) {
return true;
}
}
return false;
}
}

Registry.as<IWorkbenchContributionsRegistry>(Extensions.Workbench).registerWorkbenchContribution(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

// trigger service worker updates
const VERSION = 2;

(function () {

type Handler = {
handleFetchEvent(event: Event): Promise<Response | undefined>;
handleMessageEvent(event: MessageEvent): void;
Expand Down

0 comments on commit 077fb1b

Please sign in to comment.