Webpack 5 plugin to generate manifest of critical assets for Vue 3 SSR applications
npm install vue-ssr-assets-pluginThis is a Webpack 5 plugin for Vue 3 SSR applications to generate the manifest of critical assets. This package consists of two plugins: one for the client bundle and one for the server bundle similar to how SSR in Vue 2 worked.
Out of the box, Vue 3 SSR loads the entry bundle (e.g. main.js) that then asynchronously loads the remaining files needed for the page. However, this results in a poor user experience due to FOUC and poor web performance scores.
``html`
...
`html`
...
This generates a JSON manifest that describes the dependent assets of each Vue component.
`ts
import { VueSsrAssetsClientPlugin } from 'vue-ssr-assets-plugin'
const clientEntryConfig = merge(commonConfig, {
target: 'web',
entry: {
main: ${srcDir}/entryClient.ts,
},
plugins: [
new VueSsrAssetsClientPlugin({
fileName: 'ssr-manifest.json',
}),
],
})
`
`json
{
"main":{
"js":[
"/public/main.36e325f82fb1e4e13943.js"
],
"css":[
"/public/main.64381ac7ca0c0b20aee8.css"
]
},
"HomePage.vue":{
"js":[
"/public/762.929bac4bc20090ac3b46.js"
],
"css":[
]
},
"VueComposition.vue":{
"js":[
"/public/468.7c2ba2b804d1daa47030.js"
],
"css":[
"/public/468.ac8a11c16afa837c5a84.css"
]
},
"VueCompositionSetup.vue":{
"js":[
"/public/489.43cf0cdad14c7c1be803.js"
],
"css":[
"/public/489.afa23172870ddaef0e0e.css"
]
},
"VueOptions.vue":{
"js":[
"/public/599.f92fa658658a699cc9cc.js"
],
"css":[
"/public/599.ebbf4c878b37d1db1782.css"
]
}
}
`
If you are using webpack-dev-middleware or webpack-dev-server to serve your frontend during development, then you will need to configure them to write the manifest file to disk instead of keeping the manifest file in their memory file system.
#### webpack-dev-webpack
`ts
import { Configuration } from 'webpack'
import HtmlWebpackPlugin from 'html-webpack-plugin'
import { VueSsrAssetsClientPlugin } from 'vue-ssr-assets-plugin'
export default {
target: 'web',
entry: {
main: './src/entryClient.ts',
},
output: {
path: './dist/client'
},
devServer: {
index: 'index.html',
devMiddleware: {
writeToDisk: (filePath) => filePath.includes('ssr-manifest.json'),
},
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
filename: './dist/client/index.html',
}),
new VueSsrAssetsClientPlugin({
fileName: './dist/server/ssr-manifest.json',
}),
],
} satisfies Configuration
`
#### webpack-dev-middleware
`ts
import webpack from 'webpack'
import webpackDevMiddleware from 'webpack-dev-middleware'
import express from 'express'
const app = express()
const compiler = webpack({ / Client Config / })
app.use(
webpackDevMiddleware(compiler, {
writeToDisk: (filePath) => filePath.includes('ssr-manifest.json'),
}),
)
`
This modifies the ssrRender function that vue-loader automatically generates from your tag in your SFC. At runtime, each component will register their webpack chunk name into ssrContext._matchedComponents: Set. Each name will correspond with a key in the manifest generated by the client plugin that you can then use to look up the critical assets for the current request.
`ts
import { VueSsrAssetsServerPlugin } from 'vue-ssr-assets-plugin'
const serverEntryConfig = merge(commonConfig, {
target: 'node',
entry: {
www: ${srcDir}/entryServer.ts,
},
output: {
libraryTarget: 'commonjs2',
},
plugins: [
new VueSsrAssetsServerPlugin(),
],
})
`
`ts
import App from './App.vue'
import { createSSRApp } from 'vue'
import { createRouter } from 'vue-router'
import { renderToString } from '@vue/server-renderer'
import { readFileSync } from 'node:fs'
import { VueSsrAssetRenderer } from 'vue-ssr-assets-plugin/dist/utils/VueSsrAssetsRenderer'
/**
* After renderToString(), ssrContext._matchedComponents will contain
* all the components that this request needs e.g. ['MainLayout.vue', 'HomePage.vue']
*
* These can then be matched with the dict in ssr-manifest.json to find all the critical js/css files
*
* VueSsrAssetRenderer is a helper class that reads the ssrContext
* and generates the corresponding