You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Parcel 2 build command doesn't transform workbox-expiration npm depenpendy package as expected.
Some this references are not handled.
There was no error with Parcel 1 and the same workbox-expiration npm package.
I found a closed issue related to this bug
Every this references of the workbox-expiration npm depenpendy package should be handled during the build process.
😯 Current Behavior
The ReferenceError: _this is not defined error is thrown when web app is running.
service-worker.js:7702 Uncaught (in promise) ReferenceError: _this is not defined
at _callee$ (service-worker.js:7702:29)
at tryCatch (service-worker.js:3319:25)
at Generator.invoke [as _invoke] (service-worker.js:3495:30)
at Generator.next (service-worker.js:3367:29)
at asyncGeneratorStep (service-worker.js:997:28)
at _next (service-worker.js:1012:17)
at service-worker.js:1017:13
at new Promise (<anonymous>)
at ExpirationPlugin.<anonymous> (service-worker.js:1009:16)
at ExpirationPlugin.cachedResponseWillBeUsed (service-worker.js:7717:29)
💁 Possible Solution
It seem that _this = _this1; is missing after the build process (cf. my comments inside the code)
🔦 Context
I did an important migration of our web app from Parcel 1 to Parcel 2 to use some required new functionalities and improve the app perfomances, but we can't publish for now the new version without a working Service Worker expiration cache strategy.
We are blocked for now.
💻 Code Sample
Here the service-worker.js code that triggers the expiration strategy call, wich failed on page reload
[...]
registerRoute(
({ url }) =>
url.origin === process.env.W_API_URL && /\/files\/.*/.test(url.pathname),
new CacheFirst({
cacheName: caches_mapping.files,
plugins: [
new ExpirationPlugin({
// Cache for a maximum of 1 months
maxAgeSeconds: 31 * 24 * 60 * 60,
}),
],
})
);
[...]
Here the workbox-expiration npm package concerned code after a Parcel 2 build process.
[...]
function ExpirationPlugin() {
var config = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
var _this2 = this;
_helpers.classCallCheck(this, ExpirationPlugin);
var _this1 = this;
/**
* A "lifecycle" callback that will be triggered automatically by the
* `workbox-strategies` handlers when a `Response` is about to be returned
* from a [Cache](https://developer.mozilla.org/en-US/docs/Web/API/Cache) to
* the handler. It allows the `Response` to be inspected for freshness and
* prevents it from being used if the `Response`'s `Date` header value is
* older than the configured `maxAgeSeconds`.
*
* @param {Object} options
* @param {string} options.cacheName Name of the cache the response is in.
* @param {Response} options.cachedResponse The `Response` object that's been
* read from a cache and whose freshness should be checked.
* @return {Response} Either the `cachedResponse`, if it's
* fresh, or `null` if the `Response` is older than `maxAgeSeconds`.
*
* @private
*/ this.cachedResponseWillBeUsed = function() {
var _ref = _helpers.asyncToGenerator(_regeneratorRuntimeDefault.default.mark(function _callee(param) {
var event, request, cacheName, cachedResponse, isFresh, cacheExpiration, updateTimestampDone;
return _regeneratorRuntimeDefault.default.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
event = param.event, request = param.request, cacheName = param.cacheName, cachedResponse = param.cachedResponse;
if (cachedResponse) {
_ctx.next = 3;
break;
}
return _ctx.abrupt("return", null);
case 3:
//////////////////////////////////////
///////////////////// The missing code `_this = _this1;` should be there ///////////////////
//////////////////////////////////////
isFresh = _this._isResponseDateFresh(cachedResponse);
cacheExpiration = _this._getCacheExpiration(cacheName);
_dontWaitForJs.dontWaitFor(cacheExpiration.expireEntries());
updateTimestampDone = cacheExpiration.updateTimestamp(request.url);
if (event) try {
event.waitUntil(updateTimestampDone);
} catch (error) {}
return _ctx.abrupt("return", isFresh ? cachedResponse : null);
case 9:
case "end":
return _ctx.stop();
}
}, _callee);
}));
return function(_) {
return _ref.apply(this, arguments);
};
}();
/**
* A "lifecycle" callback that will be triggered automatically by the
* `workbox-strategies` handlers when an entry is added to a cache.
*
* @param {Object} options
* @param {string} options.cacheName Name of the cache that was updated.
* @param {string} options.request The Request for the cached entry.
*
* @private
*/ this.cacheDidUpdate = function() {
var _ref = _helpers.asyncToGenerator(_regeneratorRuntimeDefault.default.mark(function _callee(param) {
var cacheName, request, _this, cacheExpiration;
return _regeneratorRuntimeDefault.default.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
cacheName = param.cacheName, request = param.request;
//////////////////////////////////////
///////////////////// But it was added as expected in this function ///////////////////
//////////////////////////////////////
_this = _this1;
cacheExpiration = _this1._getCacheExpiration(cacheName);
_ctx.next = 5;
return cacheExpiration.updateTimestamp(request.url);
case 5:
_ctx.next = 7;
return cacheExpiration.expireEntries();
case 7:
case "end":
return _ctx.stop();
}
}, _callee);
}));
return function(_) {
return _ref.apply(this, arguments);
};
}();
this._config = config;
this._maxAgeSeconds = config.maxAgeSeconds;
this._cacheExpirations = new Map();
if (config.purgeOnQuotaError) _registerQuotaErrorCallbackJs.registerQuotaErrorCallback(function() {
return _this2.deleteCacheAndMetadata();
});
}
[...]
Here a part of the original workbox-expiration npm package code.
[...]
class ExpirationPlugin {
/**
* @param {Object} config
* @param {number} [config.maxEntries] The maximum number of entries to cache.
* Entries used the least will be removed as the maximum is reached.
* @param {number} [config.maxAgeSeconds] The maximum age of an entry before
* it's treated as stale and removed.
* @param {Object} [config.matchOptions] The [`CacheQueryOptions`](https://developer.mozilla.org/en-US/docs/Web/API/Cache/delete#Parameters)
* that will be used when calling `delete()` on the cache.
* @param {boolean} [config.purgeOnQuotaError] Whether to opt this cache in to
* automatic deletion if the available storage quota has been exceeded.
*/
constructor(config = {}) {
/**
* A "lifecycle" callback that will be triggered automatically by the
* `workbox-strategies` handlers when a `Response` is about to be returned
* from a [Cache](https://developer.mozilla.org/en-US/docs/Web/API/Cache) to
* the handler. It allows the `Response` to be inspected for freshness and
* prevents it from being used if the `Response`'s `Date` header value is
* older than the configured `maxAgeSeconds`.
*
* @param {Object} options
* @param {string} options.cacheName Name of the cache the response is in.
* @param {Response} options.cachedResponse The `Response` object that's been
* read from a cache and whose freshness should be checked.
* @return {Response} Either the `cachedResponse`, if it's
* fresh, or `null` if the `Response` is older than `maxAgeSeconds`.
*
* @private
*/
this.cachedResponseWillBeUsed = async ({ event, request, cacheName, cachedResponse }) => {
if (!cachedResponse) {
return null;
}
const isFresh = this._isResponseDateFresh(cachedResponse);
// Expire entries to ensure that even if the expiration date has
// expired, it'll only be used once.
const cacheExpiration = this._getCacheExpiration(cacheName);
dontWaitFor(cacheExpiration.expireEntries());
// Update the metadata for the request URL to the current timestamp,
// but don't `await` it as we don't want to block the response.
const updateTimestampDone = cacheExpiration.updateTimestamp(request.url);
if (event) {
try {
event.waitUntil(updateTimestampDone);
}
catch (error) {
if (process.env.NODE_ENV !== 'production') {
// The event may not be a fetch event; only log the URL if it is.
if ('request' in event) {
logger.warn(`Unable to ensure service worker stays alive when ` +
`updating cache entry for ` +
`'${getFriendlyURL(event.request.url)}'.`);
}
}
}
}
return isFresh ? cachedResponse : null;
};
[...]
🐛 bug report
Parcel 2
build command doesn't transformworkbox-expiration
npm depenpendy package as expected.Some
this
references are not handled.There was no error with
Parcel 1
and the sameworkbox-expiration
npm package.I found a closed issue related to this bug
🎛 Configuration (.babelrc, package.json, cli command)
My
package.json
My
.parcelrc
🤔 Expected Behavior
Every
this
references of theworkbox-expiration
npm depenpendy package should be handled during the build process.😯 Current Behavior
The
ReferenceError: _this is not defined
error is thrown when web app is running.💁 Possible Solution
It seem that
_this = _this1;
is missing after the build process (cf. my comments inside the code)🔦 Context
I did an important migration of our web app from
Parcel 1
toParcel 2
to use some required new functionalities and improve the app perfomances, but we can't publish for now the new version without a working Service Worker expiration cache strategy.We are blocked for now.
💻 Code Sample
Here the
service-worker.js
code that triggers the expiration strategy call, wich failed on page reloadHere the
workbox-expiration
npm package concerned code after aParcel 2
build process.Here a part of the original
workbox-expiration
npm package code.🌍 Your Environment
Thank you very much for your work and any help.
The text was updated successfully, but these errors were encountered: