mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 16:12:12 +00:00
Refactor code base
This commit is contained in:
parent
3fef1bac61
commit
d3f707dde2
@ -42,7 +42,6 @@ class Nuxt {
|
|||||||
router: {
|
router: {
|
||||||
mode: 'history',
|
mode: 'history',
|
||||||
base: '/',
|
base: '/',
|
||||||
userSpecifiedBase: false,
|
|
||||||
middleware: [],
|
middleware: [],
|
||||||
linkActiveClass: 'nuxt-link-active',
|
linkActiveClass: 'nuxt-link-active',
|
||||||
linkExactActiveClass: 'nuxt-link-exact-active',
|
linkExactActiveClass: 'nuxt-link-exact-active',
|
||||||
@ -63,7 +62,9 @@ class Nuxt {
|
|||||||
// Sanitization
|
// Sanitization
|
||||||
if (options.loading === true) delete options.loading
|
if (options.loading === true) delete options.loading
|
||||||
if (options.router && typeof options.router.middleware === 'string') options.router.middleware = [ options.router.middleware ]
|
if (options.router && typeof options.router.middleware === 'string') options.router.middleware = [ options.router.middleware ]
|
||||||
if (options.router && typeof options.router.base === 'string') options.router.userSpecifiedBase = true
|
if (options.router && typeof options.router.base === 'string') {
|
||||||
|
this._routerBaseSpecified = true
|
||||||
|
}
|
||||||
if (typeof options.transition === 'string') options.transition = { name: options.transition }
|
if (typeof options.transition === 'string') options.transition = { name: options.transition }
|
||||||
this.options = _.defaultsDeep(options, defaults)
|
this.options = _.defaultsDeep(options, defaults)
|
||||||
// Env variables
|
// Env variables
|
||||||
|
@ -22,34 +22,33 @@ export async function render (req, res) {
|
|||||||
}, 1000)
|
}, 1000)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
const self = this
|
|
||||||
const context = getContext(req, res)
|
const context = getContext(req, res)
|
||||||
res.statusCode = 200
|
res.statusCode = 200
|
||||||
try {
|
try {
|
||||||
if (self.dev) {
|
if (this.dev) {
|
||||||
// Call webpack middleware only in development
|
// Call webpack middleware only in development
|
||||||
await self.webpackDevMiddleware(req, res)
|
await this.webpackDevMiddleware(req, res)
|
||||||
await self.webpackHotMiddleware(req, res)
|
await this.webpackHotMiddleware(req, res)
|
||||||
}
|
}
|
||||||
if (!self.dev && self.options.performance.gzip) {
|
if (!this.dev && this.options.performance.gzip) {
|
||||||
await self.gzipMiddleware(req, res)
|
await this.gzipMiddleware(req, res)
|
||||||
}
|
}
|
||||||
// If base in req.url, remove it for the middleware and vue-router
|
// If base in req.url, remove it for the middleware and vue-router
|
||||||
if (self.options.router.base !== '/' && req.url.indexOf(self.options.router.base) === 0) {
|
if (this.options.router.base !== '/' && req.url.indexOf(this.options.router.base) === 0) {
|
||||||
// Compatibility with base url for dev server
|
// Compatibility with base url for dev server
|
||||||
req.url = req.url.replace(self.options.router.base, '/')
|
req.url = req.url.replace(this.options.router.base, '/')
|
||||||
}
|
}
|
||||||
// Serve static/ files
|
// Serve static/ files
|
||||||
await self.serveStatic(req, res)
|
await this.serveStatic(req, res)
|
||||||
// Serve .nuxt/dist/ files (only for production)
|
// Serve .nuxt/dist/ files (only for production)
|
||||||
if (!self.dev && req.url.indexOf(self.options.build.publicPath) === 0) {
|
if (!this.dev && req.url.indexOf(this.options.build.publicPath) === 0) {
|
||||||
const url = req.url
|
const url = req.url
|
||||||
req.url = req.url.replace(self.options.build.publicPath, '/')
|
req.url = req.url.replace(this.options.build.publicPath, '/')
|
||||||
await self.serveStaticNuxt(req, res)
|
await this.serveStaticNuxt(req, res)
|
||||||
/* istanbul ignore next */
|
/* istanbul ignore next */
|
||||||
req.url = url
|
req.url = url
|
||||||
}
|
}
|
||||||
if (this.dev && req.url.indexOf(self.options.build.publicPath) === 0 && req.url.includes('.hot-update.json')) {
|
if (this.dev && req.url.indexOf(this.options.build.publicPath) === 0 && req.url.includes('.hot-update.json')) {
|
||||||
res.statusCode = 404
|
res.statusCode = 404
|
||||||
return res.end()
|
return res.end()
|
||||||
}
|
}
|
||||||
@ -87,20 +86,19 @@ export async function renderRoute (url, context = {}) {
|
|||||||
context.url = url
|
context.url = url
|
||||||
context.isServer = true
|
context.isServer = true
|
||||||
// Call renderToString from the bundleRenderer and generate the HTML (will update the context as well)
|
// Call renderToString from the bundleRenderer and generate the HTML (will update the context as well)
|
||||||
const self = this
|
let APP = await this.renderToString(context)
|
||||||
let APP = await self.renderToString(context)
|
|
||||||
if (!context.nuxt.serverRendered) {
|
if (!context.nuxt.serverRendered) {
|
||||||
APP = '<div id="__nuxt"></div>'
|
APP = '<div id="__nuxt"></div>'
|
||||||
}
|
}
|
||||||
const m = context.meta.inject()
|
const m = context.meta.inject()
|
||||||
let HEAD = m.meta.text() + m.title.text() + m.link.text() + m.style.text() + m.script.text() + m.noscript.text()
|
let HEAD = m.meta.text() + m.title.text() + m.link.text() + m.style.text() + m.script.text() + m.noscript.text()
|
||||||
if (self.options.router.userSpecifiedBase) {
|
if (this._routerBaseSpecified) {
|
||||||
HEAD += `<base href="${self.options.router.base}">`
|
HEAD += `<base href="${this.options.router.base}">`
|
||||||
}
|
}
|
||||||
HEAD += context.renderResourceHints() + context.renderStyles()
|
HEAD += context.renderResourceHints() + context.renderStyles()
|
||||||
APP += `<script type="text/javascript">window.__NUXT__=${serialize(context.nuxt, { isJSON: true })}</script>`
|
APP += `<script type="text/javascript">window.__NUXT__=${serialize(context.nuxt, { isJSON: true })}</script>`
|
||||||
APP += context.renderScripts()
|
APP += context.renderScripts()
|
||||||
const html = self.appTemplate({
|
const html = this.appTemplate({
|
||||||
HTML_ATTRS: 'data-n-head-ssr ' + m.htmlAttrs.text(),
|
HTML_ATTRS: 'data-n-head-ssr ' + m.htmlAttrs.text(),
|
||||||
BODY_ATTRS: m.bodyAttrs.text(),
|
BODY_ATTRS: m.bodyAttrs.text(),
|
||||||
HEAD,
|
HEAD,
|
||||||
|
Loading…
Reference in New Issue
Block a user