fix URLSearchParams in edge

This commit is contained in:
Nolan Lawson 2018-02-21 17:52:33 -08:00
parent 9b316665b6
commit bd77fb43c3
3 changed files with 31 additions and 18 deletions

View File

@ -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}`)
}
}

11
routes/_utils/thunk.js Normal file
View File

@ -0,0 +1,11 @@
export function thunk(fn) {
let value
let called
return () => {
if (!called) {
value = fn()
called = true
}
return value
}
}

View File

@ -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__)
})