mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-18 17:35:57 +00:00
chore(nitro): lazy load renderer resources
This commit is contained in:
parent
717b26f3ce
commit
d8423a2f9a
@ -2,24 +2,31 @@ import { createRenderer } from 'vue-bundle-renderer'
|
|||||||
import devalue from '@nuxt/devalue'
|
import devalue from '@nuxt/devalue'
|
||||||
import config from './config'
|
import config from './config'
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import { renderToString } from '#nitro-renderer'
|
|
||||||
// @ts-ignore
|
|
||||||
import createApp from '#build/dist/server/server'
|
|
||||||
// @ts-ignore
|
|
||||||
import clientManifest from '#build/dist/server/client.manifest.json'
|
|
||||||
// @ts-ignore
|
|
||||||
import htmlTemplate from '#build/views/document.template.js'
|
import htmlTemplate from '#build/views/document.template.js'
|
||||||
|
|
||||||
function _interopDefault (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e }
|
function _interopDefault (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e }
|
||||||
|
|
||||||
const renderer = createRenderer(_interopDefault(createApp), {
|
|
||||||
clientManifest: _interopDefault(clientManifest),
|
|
||||||
renderToString
|
|
||||||
})
|
|
||||||
|
|
||||||
const STATIC_ASSETS_BASE = process.env.NUXT_STATIC_BASE + '/' + process.env.NUXT_STATIC_VERSION
|
const STATIC_ASSETS_BASE = process.env.NUXT_STATIC_BASE + '/' + process.env.NUXT_STATIC_VERSION
|
||||||
const PAYLOAD_JS = '/payload.js'
|
const PAYLOAD_JS = '/payload.js'
|
||||||
|
|
||||||
|
let _renderer
|
||||||
|
async function loadRenderer () {
|
||||||
|
if (_renderer) {
|
||||||
|
return _renderer
|
||||||
|
}
|
||||||
|
// @ts-ignore
|
||||||
|
const { renderToString } = await import('#nitro-renderer')
|
||||||
|
// @ts-ignore
|
||||||
|
const createApp = await import('#build/dist/server/server')
|
||||||
|
// @ts-ignore
|
||||||
|
const clientManifest = await import('#build/dist/server/client.manifest.json')
|
||||||
|
_renderer = createRenderer(_interopDefault(createApp), {
|
||||||
|
clientManifest: _interopDefault(clientManifest),
|
||||||
|
renderToString
|
||||||
|
})
|
||||||
|
return _renderer
|
||||||
|
}
|
||||||
|
|
||||||
export async function renderMiddleware (req, res) {
|
export async function renderMiddleware (req, res) {
|
||||||
let url = req.url
|
let url = req.url
|
||||||
|
|
||||||
@ -40,6 +47,7 @@ export async function renderMiddleware (req, res) {
|
|||||||
},
|
},
|
||||||
...(req.context || {})
|
...(req.context || {})
|
||||||
}
|
}
|
||||||
|
const renderer = await loadRenderer()
|
||||||
const rendered = await renderer.renderToString(ssrContext)
|
const rendered = await renderer.renderToString(ssrContext)
|
||||||
|
|
||||||
if (ssrContext.nuxt.hooks) {
|
if (ssrContext.nuxt.hooks) {
|
||||||
|
@ -48,7 +48,7 @@ export async function buildServer (ctx: ViteBuildContext) {
|
|||||||
const serverDist = resolve(ctx.nuxt.options.buildDir, 'dist/server')
|
const serverDist = resolve(ctx.nuxt.options.buildDir, 'dist/server')
|
||||||
await mkdirp(serverDist)
|
await mkdirp(serverDist)
|
||||||
|
|
||||||
await writeFile(resolve(serverDist, 'server.js'), 'try { module.exports = require("./entry.server") } catch (err) { module.exports = () => { throw err } }', 'utf8')
|
await writeFile(resolve(serverDist, 'server.js'), 'module.exports = require("./entry.server")', 'utf8')
|
||||||
await writeFile(resolve(serverDist, 'client.manifest.json'), 'false', 'utf8')
|
await writeFile(resolve(serverDist, 'client.manifest.json'), 'false', 'utf8')
|
||||||
|
|
||||||
const onBuild = () => ctx.nuxt.callHook('build:resources', wpfs)
|
const onBuild = () => ctx.nuxt.callHook('build:resources', wpfs)
|
||||||
|
Loading…
Reference in New Issue
Block a user