Skip to content

Commit

Permalink
Merge pull request #87 from GoogleChrome/bfcache
Browse files Browse the repository at this point in the history
Add support for reporting metrics on bfcache restore
  • Loading branch information
philipwalton authored Nov 10, 2020
2 parents 6bef716 + 8fd2287 commit 9943c4f
Show file tree
Hide file tree
Showing 32 changed files with 856 additions and 138 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"max-len": [2, {
"ignorePattern": "^\\s*import|= require\\(|^\\s*it\\(|^\\s*describe\\(",
"ignoreUrls": true
}]
}],
}
},
{
Expand All @@ -47,6 +47,7 @@
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/camelcase": "off",
"node/no-missing-import": "off",
"node/no-unsupported-features/es-syntax": "off",
Expand Down
2 changes: 2 additions & 0 deletions external-polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Creates the `web-vitals/external-polyfill` import in node-based bundlers.
export * from './dist/web-vitals.external-polyfill.js';
92 changes: 71 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"build:ts": "tsc -b",
"build:js": "rollup -c",
"clean": "rm -rf dist tsconfig.tsbuildinfo",
"dev": "run-p watch serve",
"dev": "run-p watch test:server",
"lint": "eslint \"*.js\" \"src/**/*.ts\" \"test/**/*.js\"",
"lint:fix": "eslint --fix \"*.js\" \"src/**/*.ts\" \"test/**/*.js\"",
"postversion": "git push --follow-tags",
Expand Down Expand Up @@ -60,6 +60,7 @@
"devDependencies": {
"@babel/core": "^7.12.3",
"@babel/preset-env": "^7.12.1",
"@rollup/plugin-replace": "^2.3.4",
"@typescript-eslint/eslint-plugin": "^4.7.0",
"@typescript-eslint/parser": "^4.7.0",
"@wdio/cli": "^6.7.4",
Expand Down
55 changes: 40 additions & 15 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,11 @@
limitations under the License.
*/

import replace from '@rollup/plugin-replace';
import {terser} from 'rollup-plugin-terser';
import babel from 'rollup-plugin-babel';


const baseOpts = {
input: 'dist/index.js',
watch: {
clearScreen: false,
},
};

const configurePlugins = ({module}) => {
const configurePlugins = ({module, externalPolyfill = false}) => {
return [
babel({
presets: [['@babel/preset-env', {
Expand All @@ -38,25 +31,57 @@ const configurePlugins = ({module}) => {
mangle: true,
compress: true,
}),
replace({
'self.__WEB_VITALS_EXTERNAL_POLYFILL__': externalPolyfill,
})
]
}

export default [
const configs = [
{
input: 'dist/modules/index.js',
output: {
format: 'esm',
file: './dist/web-vitals.full.js',
},
plugins: configurePlugins({module: true, externalPolyfill: false}),
},
{
input: 'dist/modules/index.js',
output: {
format: 'umd',
file: `./dist/web-vitals.full.umd.js`,
name: 'webVitals',
},
plugins: configurePlugins({module: false, externalPolyfill: false}),
},
{
...baseOpts,
input: 'dist/modules/index.js',
output: {
format: 'esm',
file: './dist/web-vitals.es5.min.js',
file: './dist/web-vitals.external-polyfill.js',
},
plugins: configurePlugins({module: true}),
plugins: configurePlugins({module: true, externalPolyfill: true}),
},
{
...baseOpts,
input: 'dist/modules/index.js',
output: {
format: 'umd',
file: './dist/web-vitals.es5.umd.min.js',
file: `./dist/web-vitals.external-polyfill.umd.js`,
name: 'webVitals',
},
plugins: configurePlugins({module: false, externalPolyfill: true}),
},
{
input: 'dist/modules/polyfill.js',
output: {
format: 'iife',
file: './dist/polyfill.js',
name: 'webVitals',
strict: false,
},
plugins: configurePlugins({module: false}),
},
];

export default configs;
9 changes: 7 additions & 2 deletions src/getCLS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import {initMetric} from './lib/initMetric.js';
import {observe, PerformanceEntryHandler} from './lib/observe.js';
import {onHidden} from './lib/onHidden.js';
import {onBFCacheRestore} from './lib/onBFCacheRestore.js';
import {bindReporter} from './lib/bindReporter.js';
import {ReportHandler} from './types.js';

Expand All @@ -28,8 +29,7 @@ interface LayoutShift extends PerformanceEntry {
}

export const getCLS = (onReport: ReportHandler, reportAllChanges = false) => {
const metric = initMetric('CLS', 0);

let metric = initMetric('CLS', 0);
let report: ReturnType<typeof bindReporter>;

const entryHandler = (entry: LayoutShift) => {
Expand All @@ -49,5 +49,10 @@ export const getCLS = (onReport: ReportHandler, reportAllChanges = false) => {
po.takeRecords().map(entryHandler as PerformanceEntryHandler);
report();
});

onBFCacheRestore(() => {
metric = initMetric('CLS', 0);
report = bindReporter(onReport, metric, reportAllChanges);
});
}
};
16 changes: 14 additions & 2 deletions src/getFCP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import {finalMetrics} from './lib/finalMetrics.js';
import {getFirstHidden} from './lib/getFirstHidden.js';
import {initMetric} from './lib/initMetric.js';
import {observe} from './lib/observe.js';
import {onBFCacheRestore} from './lib/onBFCacheRestore.js';
import {ReportHandler} from './types.js';


export const getFCP = (onReport: ReportHandler) => {
const metric = initMetric('FCP');
const firstHidden = getFirstHidden();

let metric = initMetric('FCP');
let report: ReturnType<typeof bindReporter>;

const entryHandler = (entry: PerformanceEntry) => {
Expand All @@ -47,5 +47,17 @@ export const getFCP = (onReport: ReportHandler) => {
const po = observe('paint', entryHandler);
if (po) {
report = bindReporter(onReport, metric);

onBFCacheRestore((event) => {
metric = initMetric('FCP');
report = bindReporter(onReport, metric);
requestAnimationFrame(() => {
requestAnimationFrame(() => {
metric.value = performance.now() - event.timeStamp;
finalMetrics.add(metric);
report();
});
});
});
}
};
Loading

0 comments on commit 9943c4f

Please sign in to comment.