From bd77fb43c3da278dd5c7bd638236ac4f3146fc61 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Wed, 21 Feb 2018 17:52:33 -0800 Subject: [PATCH] fix URLSearchParams in edge --- routes/_utils/marks.js | 37 +++++++++++++++++++------------------ routes/_utils/thunk.js | 11 +++++++++++ templates/main.js | 1 + 3 files changed, 31 insertions(+), 18 deletions(-) create mode 100644 routes/_utils/thunk.js 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__) })