diff --git a/routes/_utils/marks.js b/routes/_utils/marks.js index 83a4527d..56147671 100644 --- a/routes/_utils/marks.js +++ b/routes/_utils/marks.js @@ -1,25 +1,26 @@ -import noop from 'lodash/noop' +import { thunk } from './thunk' -const enableMarks = process.browser && +// Lazily invoke because URLSearchParams isn't supported in Edge 16, +// so we need the polyfill. +const enabled = thunk(() => process.browser && performance.mark && - (process.env.NODE_ENV !== 'production' || - new URLSearchParams(location.search).get('marks') === 'true') + ( + process.env.NODE_ENV !== 'production' || + new URLSearchParams(location.search).get('marks') === 'true' + ) +) const perf = process.browser && performance -function doMark (name) { - perf.mark(`start ${name}`) +export function mark(name) { + if (enabled()) { + perf.mark(`start ${name}`) + } } -function doStop (name) { - perf.mark(`end ${name}`) - perf.measure(name, `start ${name}`, `end ${name}`) -} - -const mark = enableMarks ? doMark : noop -const stop = enableMarks ? doStop : noop - -export { - mark, - stop -} +export function stop(name) { + if (enabled()) { + perf.mark(`end ${name}`) + perf.measure(name, `start ${name}`, `end ${name}`) + } +} \ No newline at end of file diff --git a/routes/_utils/thunk.js b/routes/_utils/thunk.js new file mode 100644 index 00000000..bd15a808 --- /dev/null +++ b/routes/_utils/thunk.js @@ -0,0 +1,11 @@ +export function thunk(fn) { + let value + let called + return () => { + if (!called) { + value = fn() + called = true + } + return value + } +} \ No newline at end of file diff --git a/templates/main.js b/templates/main.js index a745c0de..18a2b0c1 100644 --- a/templates/main.js +++ b/templates/main.js @@ -6,6 +6,7 @@ import '../routes/_utils/historyEvents' import '../routes/_utils/loadingMask' loadPolyfills().then(() => { + console.log('init()') // `routes` is an array of route objects injected by Sapper init(document.querySelector('#sapper'), __routes__) })