mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 22:25:12 +00:00
917adc0618
* feat: add options.target * fix(lint): lint * fix(test): update snapshots * fix(builder): default value for target * fix(test): fix test * fix(test): test fixing * fix: use this.options.target * fix: final test * Update packages/vue-renderer/src/renderer.js Co-Authored-By: Alexander Lichter <manniL@gmx.net> * feat: Add target option and update banner * fix(lint): fix * feat: Add warning when using serverMiddleware in static target * chore(utils): add TARGETS and MODES as constants * hotfix: lint * chore(module): add filename as alias of fileName * feat: introducing nuxt export and router/routes.json * hotfix: Fix the linting lord * chore(core): add comment for filename vs fileName * fix: use targets constant * chore: remove warning * fix: unit testing * wip: refactor and use TARGETS * fix: lint * feat: add target as alias for first arg value * fix: generate only for SPA * chore: explain to use nuxt static X * fix: render SPA fallback on redirect for static target * fix: lint issue * fix: only target is useful for now * wip * wip: nuxt static export is looking good * Update packages/generator/src/generator.js Co-Authored-By: Devon Rueckner <indirectlylit@users.noreply.github.com> * Update packages/cli/src/options/common.js Co-Authored-By: Alexander Lichter <manniL@gmx.net> * feat: add options.target * fix(lint): lint * fix(test): update snapshots * fix(builder): default value for target * fix(test): fix test * fix(test): test fixing * fix: use this.options.target * fix: final test * Update packages/vue-renderer/src/renderer.js Co-Authored-By: Alexander Lichter <manniL@gmx.net> * feat: Add target option and update banner * fix(lint): fix * feat: Add warning when using serverMiddleware in static target * chore(utils): add TARGETS and MODES as constants * hotfix: lint * chore(module): add filename as alias of fileName * feat: introducing nuxt export and router/routes.json * hotfix: Fix the linting lord * chore(core): add comment for filename vs fileName * fix: use targets constant * chore: remove warning * fix: unit testing * wip: refactor and use TARGETS * fix: lint * feat: add target as alias for first arg value * chore: explain to use nuxt static X * fix: render SPA fallback on redirect for static target * fix: lint issue * fix: only target is useful for now * wip * wip: nuxt static export is looking good * Update packages/generator/src/generator.js Co-Authored-By: Devon Rueckner <indirectlylit@users.noreply.github.com> * Update packages/cli/src/options/common.js Co-Authored-By: Alexander Lichter <manniL@gmx.net> * fix: duplicate imports * chore: don't server render if an error happens on static target * test: update unit and add export * lint: fix * lint: fix * fix: e2e test * fix: fallback only for static target * fix: dev test * feat: add generate.crawler * fix: full static is when generate.static is given * chore: improvements * fix: Add isFullStatic in nuxt/config.json * feat: handle fetch for full static * feat: router.prefetchPayloads for full static * chore: use fetch in async-data example * fix: add target only if given * fix: use created to have access to props in fetchOnServer * chore: add console.error in dev for easy debugging * feat: payload smart pre-fetching * fix: remove alias for target * fix: increment payloadFetchIndex is static set to false * chore: lint * chore: add serve command * chore: rename universal to server-side * fix: handle payloadPath on SPA fallback * fix: lint * chore lint again * feat: handle spa fallback * feat: support string for exclude * fix: fallback only if no extension or html * chore: use JSON.stringify() for static target * chore: lint again, dammit * chore: fix tests and remove too early return * fix: early return only for server target * fix: update tests * fix: unit tests * chore: add ssr option * chore: add logic for ssr option * fix: #6682 * chore(dx): add next command to run * fix: lint * fix: tests * chore: keep old behaviour for nuxt build in spa * fix: test again, oh boy * fix: alright this is good now * chore: add comment for spa fallback * chore: move routes.json to dot nuxt dir * chore: simplify check for promise * chore: unique lock id * chore: refactor isFullStatic * fix: dont set default in build context * chore: add test for serve * chore: update tests * hotfix: lint tests * chore(dx): improve message for bundling * feat: js payload extraction with jsonp * fix: keep serialized session script for legacy generate * fix: call to setPagePayload from fetchPayload * use devalue for payload chunks * feat: add initial load state chunk * feat: preload payload and state scripts * fix(vue-app): don't re-render the app if trailing slash on SSG * hotfix: remove console.log * chore(dx): add deploy infos for nuxt export Co-authored-by: Pooya Parsa <pyapar@gmail.com> * chore: handle fetching payload.js for nuxt state * chore(dx): error when using nuxt generate and static * chore: remove static option for clarity * chore: remove serverless target * hotfix: lint * hotfix: unit tests * chore: update legacy js resource * chore: remove query params from url in static target * fix: use globalName and urlJoin * chore: typo * feat: previewMode 👀 * chore: rename to enablePreview * fix: wait next tick to avoid error on spa * chore: try 1 sec * hotfix: test only for linux, wtf azure * refactor: static assets - generalize logic for modules need emit export static assets - allow customization for version, dir and base - serialization logic is only in ssr now * feat: smart state chunk creates * fix(client): ignore payload load error * perf: avoide payload loading for spa initial * perf: avoid loading failed chunks again * chore(cli): add simple compression for nuxt serve * test: update snapshots * fix version snapshot * fix(generator): set staticAssetsBase on context only for full static * fix tests * fix: honor shouldHashCspScriptSrc * chore(dx): add log for client-side fallback creation Co-authored-by: Xin Du (Clark) <clark.duxin@gmail.com> Co-authored-by: Alexander Lichter <manniL@gmx.net> Co-authored-by: Pooya Parsa <pooya@pi0.ir> Co-authored-by: Devon Rueckner <indirectlylit@users.noreply.github.com> Co-authored-by: Pooya Parsa <pyapar@gmail.com>
653 lines
17 KiB
JavaScript
653 lines
17 KiB
JavaScript
import Vue from 'vue'
|
|
|
|
// window.{{globals.loadedCallback}} hook
|
|
// Useful for jsdom testing or plugins (https://github.com/tmpvar/jsdom#dealing-with-asynchronous-script-loading)
|
|
if (process.client) {
|
|
window.<%= globals.readyCallback %>Cbs = []
|
|
window.<%= globals.readyCallback %> = (cb) => {
|
|
window.<%= globals.readyCallback %>Cbs.push(cb)
|
|
}
|
|
}
|
|
|
|
export function empty () {}
|
|
|
|
export function globalHandleError (error) {
|
|
if (Vue.config.errorHandler) {
|
|
Vue.config.errorHandler(error)
|
|
}
|
|
}
|
|
|
|
export function interopDefault (promise) {
|
|
return promise.then(m => m.default || m)
|
|
}
|
|
|
|
<% if (features.fetch) { %>
|
|
export function hasFetch(vm) {
|
|
return vm.$options && typeof vm.$options.fetch === 'function' && !vm.$options.fetch.length
|
|
}
|
|
export function getChildrenComponentInstancesUsingFetch(vm, instances = []) {
|
|
const children = vm.$children || []
|
|
for (const child of children) {
|
|
if (child.$fetch) {
|
|
instances.push(child)
|
|
continue; // Don't get the children since it will reload the template
|
|
}
|
|
if (child.$children) {
|
|
getChildrenComponentInstancesUsingFetch(child, instances)
|
|
}
|
|
}
|
|
return instances
|
|
}
|
|
<% } %>
|
|
<% if (features.asyncData) { %>
|
|
export function applyAsyncData (Component, asyncData) {
|
|
if (
|
|
// For SSR, we once all this function without second param to just apply asyncData
|
|
// Prevent doing this for each SSR request
|
|
!asyncData && Component.options.__hasNuxtData
|
|
) {
|
|
return
|
|
}
|
|
|
|
const ComponentData = Component.options._originDataFn || Component.options.data || function () { return {} }
|
|
Component.options._originDataFn = ComponentData
|
|
|
|
Component.options.data = function () {
|
|
const data = ComponentData.call(this, this)
|
|
if (this.$ssrContext) {
|
|
asyncData = this.$ssrContext.asyncData[Component.cid]
|
|
}
|
|
return { ...data, ...asyncData }
|
|
}
|
|
|
|
Component.options.__hasNuxtData = true
|
|
|
|
if (Component._Ctor && Component._Ctor.options) {
|
|
Component._Ctor.options.data = Component.options.data
|
|
}
|
|
}
|
|
<% } %>
|
|
|
|
export function sanitizeComponent (Component) {
|
|
// If Component already sanitized
|
|
if (Component.options && Component._Ctor === Component) {
|
|
return Component
|
|
}
|
|
if (!Component.options) {
|
|
Component = Vue.extend(Component) // fix issue #6
|
|
Component._Ctor = Component
|
|
} else {
|
|
Component._Ctor = Component
|
|
Component.extendOptions = Component.options
|
|
}
|
|
// If no component name defined, set file path as name, (also fixes #5703)
|
|
if (!Component.options.name && Component.options.__file) {
|
|
Component.options.name = Component.options.__file
|
|
}
|
|
return Component
|
|
}
|
|
|
|
export function getMatchedComponents (route, matches = false, prop = 'components') {
|
|
return Array.prototype.concat.apply([], route.matched.map((m, index) => {
|
|
return Object.keys(m[prop]).map((key) => {
|
|
matches && matches.push(index)
|
|
return m[prop][key]
|
|
})
|
|
}))
|
|
}
|
|
|
|
export function getMatchedComponentsInstances (route, matches = false) {
|
|
return getMatchedComponents(route, matches, 'instances')
|
|
}
|
|
|
|
export function flatMapComponents (route, fn) {
|
|
return Array.prototype.concat.apply([], route.matched.map((m, index) => {
|
|
return Object.keys(m.components).reduce((promises, key) => {
|
|
if (m.components[key]) {
|
|
promises.push(fn(m.components[key], m.instances[key], m, key, index))
|
|
} else {
|
|
delete m.components[key]
|
|
}
|
|
return promises
|
|
}, [])
|
|
}))
|
|
}
|
|
|
|
export function resolveRouteComponents (route, fn) {
|
|
return Promise.all(
|
|
flatMapComponents(route, async (Component, instance, match, key) => {
|
|
// If component is a function, resolve it
|
|
if (typeof Component === 'function' && !Component.options) {
|
|
Component = await Component()
|
|
}
|
|
match.components[key] = Component = sanitizeComponent(Component)
|
|
return typeof fn === 'function' ? fn(Component, instance, match, key) : Component
|
|
})
|
|
)
|
|
}
|
|
|
|
export async function getRouteData (route) {
|
|
if (!route) {
|
|
return
|
|
}
|
|
// Make sure the components are resolved (code-splitting)
|
|
await resolveRouteComponents(route)
|
|
// Send back a copy of route with meta based on Component definition
|
|
return {
|
|
...route,
|
|
meta: getMatchedComponents(route).map((Component, index) => {
|
|
return { ...Component.options.meta, ...(route.matched[index] || {}).meta }
|
|
})
|
|
}
|
|
}
|
|
|
|
export async function setContext (app, context) {
|
|
// If context not defined, create it
|
|
if (!app.context) {
|
|
app.context = {
|
|
isStatic: process.static,
|
|
isDev: <%= isDev %>,
|
|
isHMR: false,
|
|
app,
|
|
<%= (store ? 'store: app.store,' : '') %>
|
|
payload: context.payload,
|
|
error: context.error,
|
|
base: '<%= router.base %>',
|
|
env: <%= JSON.stringify(env) %><%= isTest ? '// eslint-disable-line' : '' %>
|
|
}
|
|
// Only set once
|
|
if (!process.static && context.req) {
|
|
app.context.req = context.req
|
|
}
|
|
if (!process.static && context.res) {
|
|
app.context.res = context.res
|
|
}
|
|
if (context.ssrContext) {
|
|
app.context.ssrContext = context.ssrContext
|
|
}
|
|
app.context.redirect = (status, path, query) => {
|
|
if (!status) {
|
|
return
|
|
}
|
|
app.context._redirected = true
|
|
// if only 1 or 2 arguments: redirect('/') or redirect('/', { foo: 'bar' })
|
|
let pathType = typeof path
|
|
if (typeof status !== 'number' && (pathType === 'undefined' || pathType === 'object')) {
|
|
query = path || {}
|
|
path = status
|
|
pathType = typeof path
|
|
status = 302
|
|
}
|
|
if (pathType === 'object') {
|
|
path = app.router.resolve(path).route.fullPath
|
|
}
|
|
// "/absolute/route", "./relative/route" or "../relative/route"
|
|
if (/(^[.]{1,2}\/)|(^\/(?!\/))/.test(path)) {
|
|
app.context.next({
|
|
path,
|
|
query,
|
|
status
|
|
})
|
|
} else {
|
|
path = formatUrl(path, query)
|
|
if (process.server) {
|
|
app.context.next({
|
|
path,
|
|
status
|
|
})
|
|
}
|
|
if (process.client) {
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/Location/replace
|
|
window.location.replace(path)
|
|
|
|
// Throw a redirect error
|
|
throw new Error('ERR_REDIRECT')
|
|
}
|
|
}
|
|
}
|
|
if (process.server) {
|
|
app.context.beforeNuxtRender = fn => context.beforeRenderFns.push(fn)
|
|
}
|
|
if (process.client) {
|
|
app.context.nuxtState = window.<%= globals.context %>
|
|
}
|
|
}
|
|
|
|
// Dynamic keys
|
|
const [currentRouteData, fromRouteData] = await Promise.all([
|
|
getRouteData(context.route),
|
|
getRouteData(context.from)
|
|
])
|
|
|
|
if (context.route) {
|
|
app.context.route = currentRouteData
|
|
}
|
|
|
|
if (context.from) {
|
|
app.context.from = fromRouteData
|
|
}
|
|
|
|
app.context.next = context.next
|
|
app.context._redirected = false
|
|
app.context._errored = false
|
|
app.context.isHMR = <% if(isDev) { %>Boolean(context.isHMR)<% } else { %>false<% } %>
|
|
app.context.params = app.context.route.params || {}
|
|
app.context.query = app.context.route.query || {}
|
|
}
|
|
<% if (features.middleware) { %>
|
|
export function middlewareSeries (promises, appContext) {
|
|
if (!promises.length || appContext._redirected || appContext._errored) {
|
|
return Promise.resolve()
|
|
}
|
|
return promisify(promises[0], appContext)
|
|
.then(() => {
|
|
return middlewareSeries(promises.slice(1), appContext)
|
|
})
|
|
}
|
|
<% } %>
|
|
export function promisify (fn, context) {
|
|
<% if (features.deprecations) { %>
|
|
let promise
|
|
if (fn.length === 2) {
|
|
<% if (isDev) { %>
|
|
console.warn('Callback-based asyncData, fetch or middleware calls are deprecated. ' +
|
|
'Please switch to promises or async/await syntax')
|
|
<% } %>
|
|
|
|
// fn(context, callback)
|
|
promise = new Promise((resolve) => {
|
|
fn(context, function (err, data) {
|
|
if (err) {
|
|
context.error(err)
|
|
}
|
|
data = data || {}
|
|
resolve(data)
|
|
})
|
|
})
|
|
} else {
|
|
promise = fn(context)
|
|
}
|
|
<% } else { %>
|
|
const promise = fn(context)
|
|
<% } %>
|
|
if (promise && promise instanceof Promise && typeof promise.then === 'function') {
|
|
return promise
|
|
}
|
|
return Promise.resolve(promise)
|
|
}
|
|
|
|
// Imported from vue-router
|
|
export function getLocation (base, mode) {
|
|
let path = decodeURI(window.location.pathname)
|
|
if (mode === 'hash') {
|
|
return window.location.hash.replace(/^#\//, '')
|
|
}
|
|
if (base && path.indexOf(base) === 0) {
|
|
path = path.slice(base.length)
|
|
}
|
|
return (path || '/') + window.location.search + window.location.hash
|
|
}
|
|
|
|
// Imported from path-to-regexp
|
|
|
|
/**
|
|
* Compile a string to a template function for the path.
|
|
*
|
|
* @param {string} str
|
|
* @param {Object=} options
|
|
* @return {!function(Object=, Object=)}
|
|
*/
|
|
export function compile (str, options) {
|
|
return tokensToFunction(parse(str, options), options)
|
|
}
|
|
|
|
export function getQueryDiff (toQuery, fromQuery) {
|
|
const diff = {}
|
|
const queries = { ...toQuery, ...fromQuery }
|
|
for (const k in queries) {
|
|
if (String(toQuery[k]) !== String(fromQuery[k])) {
|
|
diff[k] = true
|
|
}
|
|
}
|
|
return diff
|
|
}
|
|
|
|
export function normalizeError (err) {
|
|
let message
|
|
if (!(err.message || typeof err === 'string')) {
|
|
try {
|
|
message = JSON.stringify(err, null, 2)
|
|
} catch (e) {
|
|
message = `[${err.constructor.name}]`
|
|
}
|
|
} else {
|
|
message = err.message || err
|
|
}
|
|
return {
|
|
...err,
|
|
message,
|
|
statusCode: (err.statusCode || err.status || (err.response && err.response.status) || 500)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The main path matching regexp utility.
|
|
*
|
|
* @type {RegExp}
|
|
*/
|
|
const PATH_REGEXP = new RegExp([
|
|
// Match escaped characters that would otherwise appear in future matches.
|
|
// This allows the user to escape special characters that won't transform.
|
|
'(\\\\.)',
|
|
// Match Express-style parameters and un-named parameters with a prefix
|
|
// and optional suffixes. Matches appear as:
|
|
//
|
|
// "/:test(\\d+)?" => ["/", "test", "\d+", undefined, "?", undefined]
|
|
// "/route(\\d+)" => [undefined, undefined, undefined, "\d+", undefined, undefined]
|
|
// "/*" => ["/", undefined, undefined, undefined, undefined, "*"]
|
|
'([\\/.])?(?:(?:\\:(\\w+)(?:\\(((?:\\\\.|[^\\\\()])+)\\))?|\\(((?:\\\\.|[^\\\\()])+)\\))([+*?])?|(\\*))'
|
|
].join('|'), 'g')
|
|
|
|
/**
|
|
* Parse a string for the raw tokens.
|
|
*
|
|
* @param {string} str
|
|
* @param {Object=} options
|
|
* @return {!Array}
|
|
*/
|
|
function parse (str, options) {
|
|
const tokens = []
|
|
let key = 0
|
|
let index = 0
|
|
let path = ''
|
|
const defaultDelimiter = (options && options.delimiter) || '/'
|
|
let res
|
|
|
|
while ((res = PATH_REGEXP.exec(str)) != null) {
|
|
const m = res[0]
|
|
const escaped = res[1]
|
|
const offset = res.index
|
|
path += str.slice(index, offset)
|
|
index = offset + m.length
|
|
|
|
// Ignore already escaped sequences.
|
|
if (escaped) {
|
|
path += escaped[1]
|
|
continue
|
|
}
|
|
|
|
const next = str[index]
|
|
const prefix = res[2]
|
|
const name = res[3]
|
|
const capture = res[4]
|
|
const group = res[5]
|
|
const modifier = res[6]
|
|
const asterisk = res[7]
|
|
|
|
// Push the current path onto the tokens.
|
|
if (path) {
|
|
tokens.push(path)
|
|
path = ''
|
|
}
|
|
|
|
const partial = prefix != null && next != null && next !== prefix
|
|
const repeat = modifier === '+' || modifier === '*'
|
|
const optional = modifier === '?' || modifier === '*'
|
|
const delimiter = res[2] || defaultDelimiter
|
|
const pattern = capture || group
|
|
|
|
tokens.push({
|
|
name: name || key++,
|
|
prefix: prefix || '',
|
|
delimiter,
|
|
optional,
|
|
repeat,
|
|
partial,
|
|
asterisk: Boolean(asterisk),
|
|
pattern: pattern ? escapeGroup(pattern) : (asterisk ? '.*' : '[^' + escapeString(delimiter) + ']+?')
|
|
})
|
|
}
|
|
|
|
// Match any characters still remaining.
|
|
if (index < str.length) {
|
|
path += str.substr(index)
|
|
}
|
|
|
|
// If the path exists, push it onto the end.
|
|
if (path) {
|
|
tokens.push(path)
|
|
}
|
|
|
|
return tokens
|
|
}
|
|
|
|
/**
|
|
* Prettier encoding of URI path segments.
|
|
*
|
|
* @param {string}
|
|
* @return {string}
|
|
*/
|
|
function encodeURIComponentPretty (str, slashAllowed) {
|
|
const re = slashAllowed ? /[?#]/g : /[/?#]/g
|
|
return encodeURI(str).replace(re, (c) => {
|
|
return '%' + c.charCodeAt(0).toString(16).toUpperCase()
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Encode the asterisk parameter. Similar to `pretty`, but allows slashes.
|
|
*
|
|
* @param {string}
|
|
* @return {string}
|
|
*/
|
|
function encodeAsterisk (str) {
|
|
return encodeURIComponentPretty(str, true)
|
|
}
|
|
|
|
/**
|
|
* Escape a regular expression string.
|
|
*
|
|
* @param {string} str
|
|
* @return {string}
|
|
*/
|
|
function escapeString (str) {
|
|
return str.replace(/([.+*?=^!:${}()[\]|/\\])/g, '\\$1')
|
|
}
|
|
|
|
/**
|
|
* Escape the capturing group by escaping special characters and meaning.
|
|
*
|
|
* @param {string} group
|
|
* @return {string}
|
|
*/
|
|
function escapeGroup (group) {
|
|
return group.replace(/([=!:$/()])/g, '\\$1')
|
|
}
|
|
|
|
/**
|
|
* Expose a method for transforming tokens into the path function.
|
|
*/
|
|
function tokensToFunction (tokens, options) {
|
|
// Compile all the tokens into regexps.
|
|
const matches = new Array(tokens.length)
|
|
|
|
// Compile all the patterns before compilation.
|
|
for (let i = 0; i < tokens.length; i++) {
|
|
if (typeof tokens[i] === 'object') {
|
|
matches[i] = new RegExp('^(?:' + tokens[i].pattern + ')$', flags(options))
|
|
}
|
|
}
|
|
|
|
return function (obj, opts) {
|
|
let path = ''
|
|
const data = obj || {}
|
|
const options = opts || {}
|
|
const encode = options.pretty ? encodeURIComponentPretty : encodeURIComponent
|
|
|
|
for (let i = 0; i < tokens.length; i++) {
|
|
const token = tokens[i]
|
|
|
|
if (typeof token === 'string') {
|
|
path += token
|
|
|
|
continue
|
|
}
|
|
|
|
const value = data[token.name || 'pathMatch']
|
|
let segment
|
|
|
|
if (value == null) {
|
|
if (token.optional) {
|
|
// Prepend partial segment prefixes.
|
|
if (token.partial) {
|
|
path += token.prefix
|
|
}
|
|
|
|
continue
|
|
} else {
|
|
throw new TypeError('Expected "' + token.name + '" to be defined')
|
|
}
|
|
}
|
|
|
|
if (Array.isArray(value)) {
|
|
if (!token.repeat) {
|
|
throw new TypeError('Expected "' + token.name + '" to not repeat, but received `' + JSON.stringify(value) + '`')
|
|
}
|
|
|
|
if (value.length === 0) {
|
|
if (token.optional) {
|
|
continue
|
|
} else {
|
|
throw new TypeError('Expected "' + token.name + '" to not be empty')
|
|
}
|
|
}
|
|
|
|
for (let j = 0; j < value.length; j++) {
|
|
segment = encode(value[j])
|
|
|
|
if (!matches[i].test(segment)) {
|
|
throw new TypeError('Expected all "' + token.name + '" to match "' + token.pattern + '", but received `' + JSON.stringify(segment) + '`')
|
|
}
|
|
|
|
path += (j === 0 ? token.prefix : token.delimiter) + segment
|
|
}
|
|
|
|
continue
|
|
}
|
|
|
|
segment = token.asterisk ? encodeAsterisk(value) : encode(value)
|
|
|
|
if (!matches[i].test(segment)) {
|
|
throw new TypeError('Expected "' + token.name + '" to match "' + token.pattern + '", but received "' + segment + '"')
|
|
}
|
|
|
|
path += token.prefix + segment
|
|
}
|
|
|
|
return path
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the flags for a regexp from the options.
|
|
*
|
|
* @param {Object} options
|
|
* @return {string}
|
|
*/
|
|
function flags (options) {
|
|
return options && options.sensitive ? '' : 'i'
|
|
}
|
|
|
|
/**
|
|
* Format given url, append query to url query string
|
|
*
|
|
* @param {string} url
|
|
* @param {string} query
|
|
* @return {string}
|
|
*/
|
|
function formatUrl (url, query) {
|
|
<% if (features.clientUseUrl) { %>
|
|
url = new URL(url, top.location.href)
|
|
for (const key in query) {
|
|
const value = query[key]
|
|
if (value == null) {
|
|
continue
|
|
}
|
|
if (Array.isArray(value)) {
|
|
for (const arrayValue of value) {
|
|
url.searchParams.append(key, arrayValue)
|
|
}
|
|
continue
|
|
}
|
|
url.searchParams.append(key, value)
|
|
}
|
|
url.searchParams.sort()
|
|
return url.toString()
|
|
<% } else { %>
|
|
let protocol
|
|
const index = url.indexOf('://')
|
|
if (index !== -1) {
|
|
protocol = url.substring(0, index)
|
|
url = url.substring(index + 3)
|
|
} else if (url.startsWith('//')) {
|
|
url = url.substring(2)
|
|
}
|
|
|
|
let parts = url.split('/')
|
|
let result = (protocol ? protocol + '://' : '//') + parts.shift()
|
|
|
|
let path = parts.filter(Boolean).join('/')
|
|
let hash
|
|
parts = path.split('#')
|
|
if (parts.length === 2) {
|
|
[path, hash] = parts
|
|
}
|
|
|
|
result += path ? '/' + path : ''
|
|
|
|
if (query && JSON.stringify(query) !== '{}') {
|
|
result += (url.split('?').length === 2 ? '&' : '?') + formatQuery(query)
|
|
}
|
|
result += hash ? '#' + hash : ''
|
|
|
|
return result
|
|
<% } %>
|
|
}
|
|
<% if (!features.clientUseUrl) { %>
|
|
/**
|
|
* Transform data object to query string
|
|
*
|
|
* @param {object} query
|
|
* @return {string}
|
|
*/
|
|
function formatQuery (query) {
|
|
return Object.keys(query).sort().map((key) => {
|
|
const val = query[key]
|
|
if (val == null) {
|
|
return ''
|
|
}
|
|
if (Array.isArray(val)) {
|
|
return val.slice().map(val2 => [key, '=', val2].join('')).join('&')
|
|
}
|
|
return key + '=' + val
|
|
}).filter(Boolean).join('&')
|
|
}
|
|
<% } %>
|
|
|
|
export function addLifecycleHook(vm, hook, fn) {
|
|
if (!vm.$options[hook]) {
|
|
vm.$options[hook] = []
|
|
}
|
|
if (!vm.$options[hook].includes(fn)) {
|
|
vm.$options[hook].push(fn)
|
|
}
|
|
}
|
|
|
|
export const urlJoin = function urlJoin () {
|
|
return [].slice
|
|
.call(arguments)
|
|
.join('/')
|
|
.replace(/\/+/g, '/')
|
|
.replace(':/', '://')
|
|
}
|