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