Skip to content

Commit 639d04a

Browse files
authored
Plugin search by modified date (#409)
* add function to check plugin uri match * use `pluginMatchesUrl` to find plugin match `hasNewPluginForUri` - finds newest plugin for cache invalidation * export `hasNewPluginForUri` * cache: better search plugin by uri for cache invalidation
1 parent a2c5c1e commit 639d04a

File tree

3 files changed

+108
-47
lines changed

3 files changed

+108
-47
lines changed

lib/core.js

+9-45
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,7 @@
403403

404404
} else {
405405

406-
var domain = uri.split('/')[2]
407-
// Skip www. for domain search.
408-
.replace(/^www\./i, "")
409-
.toLowerCase();
406+
const domain = pluginLoader.getDomainForPlugin(uri);
410407

411408
var pluginMatchesByDomains = {};
412409

@@ -426,48 +423,15 @@
426423
for(var i = 0; i < pluginsList.length; i++) {
427424
var plugin = pluginsList[i];
428425

429-
if (plugin.domain) {
426+
var match = plugin.pluginMatchesUrl(domain, uri);
430427

431-
// Match only by regexp. Used in specific cases where domain changes (like national domain).
432-
433-
var match = null, j = 0, res = plugin.re;
434-
while (!match && j < res.length) {
435-
match = uri.match(res[j]);
436-
j++;
437-
}
438-
if (match) {
439-
// Store match for plugin.
428+
if (match) {
429+
if (match === true) {
430+
// Match without regexp.
431+
registerDomainPlugin(plugin, null);
432+
} else {
440433
registerDomainPlugin(plugin, match);
441434
pluginsUrlMatches[plugin.id] = match;
442-
continue;
443-
} else if (res.length) {
444-
// Skip plugin with unmatched re.
445-
continue;
446-
}
447-
448-
// Straight match by domain.
449-
450-
// Positive match on plugin.domain="domain.com", domain="sub.domain.com"
451-
// Positive match on plugin.domain="domain.com", domain="domain.com"
452-
var idx = domain.indexOf(plugin.domain);
453-
454-
if (idx === -1 || ((idx > 0) && domain.charAt(idx - 1) !== '.')) {
455-
// Break if not found, or not dot separation.
456-
continue;
457-
}
458-
459-
if (idx > 0) {
460-
var subdomain = domain.substring(0, idx - 1);
461-
if (subdomain === 'blog') {
462-
// Skip "blog.*.*" blog page for domain plugin without re.
463-
continue;
464-
}
465-
}
466-
467-
var match = (idx + plugin.domain.length) === domain.length;
468-
469-
if (match) {
470-
registerDomainPlugin(plugin, null);
471435
}
472436
}
473437
}
@@ -488,8 +452,8 @@
488452
addAllGeneric();
489453
}
490454

491-
for(var domain in pluginMatchesByDomains) {
492-
var domainPlugins = pluginMatchesByDomains[domain];
455+
for(var d in pluginMatchesByDomains) {
456+
var domainPlugins = pluginMatchesByDomains[d];
493457

494458
var matchedPluginsNames = [];
495459

lib/loader/pluginLoader.js

+96
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
pluginsList = [],
3232
postPluginsList = [],
3333
pluginsByDomain = {};
34+
35+
var pluginsListSortedByModifiedTime;
3436

3537
export {
3638
plugins as _plugins,
@@ -98,6 +100,19 @@
98100
}
99101
}
100102

103+
function fillModifiedDate() {
104+
for(var i = 0; i < pluginsList.length; i++) {
105+
var plugin = pluginsList[i];
106+
plugin.modifiedWithMixins = plugin.getPluginLastModifiedDate();
107+
}
108+
109+
pluginsListSortedByModifiedTime = [...pluginsList];
110+
pluginsListSortedByModifiedTime.sort((p1, p2) => {
111+
// Sort desc by modified date.
112+
return p2.modifiedWithMixins - p1.modifiedWithMixins;
113+
});
114+
}
115+
101116
function getFileName(filenameWithExt) {
102117
return filenameWithExt.replace(/\.(js|ejs)$/i, "");
103118
}
@@ -304,6 +319,7 @@
304319
var stat = fs.statSync(pluginPath);
305320
pluginDeclaration.modified = new Date(stat.mtime);
306321
pluginDeclaration.getPluginLastModifiedDate = getPluginLastModifiedDate;
322+
pluginDeclaration.pluginMatchesUrl = pluginMatchesUrl;
307323

308324
// If no mixins - mark domain plugin 'asks to mixin all generic plugins'.
309325
if (plugin.mixins) {
@@ -445,6 +461,84 @@
445461
return record;
446462
};
447463

464+
export function getDomainForPlugin(uri) {
465+
return uri.split('/')[2]
466+
// Skip www. for domain search.
467+
.replace(/^www\./i, "")
468+
.toLowerCase();
469+
}
470+
471+
export function hasNewPluginForUri(timeAfter, uri) {
472+
if (typeof timeAfter !== 'number') {
473+
return;
474+
}
475+
476+
var i = 0,
477+
plugin, match;
478+
479+
const domain = getDomainForPlugin(uri);
480+
481+
while (i < pluginsListSortedByModifiedTime.length
482+
&& (plugin = pluginsListSortedByModifiedTime[i])
483+
// Dot not check next plugins if older, because they are all older (SortedByModifiedTime desc).
484+
&& plugin.modifiedWithMixins > timeAfter
485+
// Find first plugin match (newest).
486+
&& !(match = plugin.pluginMatchesUrl(domain, uri))) {
487+
488+
i++;
489+
}
490+
491+
// Return plugin if match found.
492+
return match && plugin;
493+
};
494+
495+
function pluginMatchesUrl(domain, uri) {
496+
const plugin = this;
497+
if (plugin.domain) {
498+
499+
// Match only by regexp. Used in specific cases where domain changes (like national domain).
500+
501+
var match = null, j = 0, res = plugin.re;
502+
while (!match && j < res.length) {
503+
match = uri.match(res[j]);
504+
j++;
505+
}
506+
if (match) {
507+
// Store match for plugin.
508+
return match;
509+
} else if (res.length) {
510+
// Skip plugin with unmatched re.
511+
return;
512+
}
513+
514+
// Straight match by domain.
515+
516+
// Positive match on plugin.domain="domain.com", domain="sub.domain.com"
517+
// Positive match on plugin.domain="domain.com", domain="domain.com"
518+
var idx = domain.indexOf(plugin.domain);
519+
520+
if (idx === -1 || ((idx > 0) && domain.charAt(idx - 1) !== '.')) {
521+
// Break if not found, or not dot separation.
522+
return;
523+
}
524+
525+
if (idx > 0) {
526+
var subdomain = domain.substring(0, idx - 1);
527+
if (subdomain === 'blog') {
528+
// Skip "blog.*.*" blog page for domain plugin without re.
529+
return;
530+
}
531+
}
532+
533+
var match = (idx + plugin.domain.length) === domain.length;
534+
535+
if (match) {
536+
// TODO: use `true` as `null`.
537+
return true;
538+
}
539+
}
540+
}
541+
448542
async function scanModulesForPlugins() {
449543

450544
// Scan node_modules dir.
@@ -513,6 +607,8 @@
513607
}
514608
}
515609

610+
fillModifiedDate();
611+
516612
console.log('Iframely plugins loaded:')
517613
console.log(' - custom domains:', pluginsList.filter(function(p) { return p.domain; }).length);
518614
console.log(' - generic & meta:', pluginsList.filter(function(p) { return !p.domain && !p.custom; }).length);

utils.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@
8787
result += new Date(whitelistRecord.date).getTime();
8888
}
8989

90-
var plugin = pluginLoader.findDomainPlugin(uri);
90+
// `0` to search all plugins.
91+
var plugin = pluginLoader.hasNewPluginForUri(0, uri);
9192
if (plugin) {
92-
result += plugin.getPluginLastModifiedDate().getTime();
93+
result += plugin.modifiedWithMixins.getTime();
9394
}
9495

9596
if (result) {

0 commit comments

Comments
 (0)