Skip to content

Commit 2a58e9e

Browse files
committed
fix: missing register component in ssr (vuejs#1887)
1 parent 6986365 commit 2a58e9e

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"packageManager": "[email protected]",
3939
"dependencies": {
4040
"chalk": "^4.1.0",
41+
"hash-sum": "^2.0.0",
4142
"watchpack": "^2.4.0"
4243
},
4344
"peerDependencies": {
@@ -57,6 +58,7 @@
5758
"@intlify/vue-i18n-loader": "^3.0.0",
5859
"@types/cssesc": "^3.0.2",
5960
"@types/estree": "^0.0.45",
61+
"@types/hash-sum": "^1.0.2",
6062
"@types/jest": "^26.0.13",
6163
"@types/jsdom": "^16.2.13",
6264
"@types/mini-css-extract-plugin": "^0.9.1",

src/index.ts

+15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import type {
1111
SFCTemplateCompileOptions,
1212
SFCScriptCompileOptions,
1313
} from 'vue/compiler-sfc'
14+
15+
import hashSum from 'hash-sum'
16+
1417
import { selectBlock } from './select'
1518
import { genHotReloadCode } from './hotReload'
1619
import { genCSSModulesCode } from './cssModules'
@@ -363,6 +366,18 @@ export default function loader(
363366
.join(`\n`) + `\n`
364367
}
365368

369+
if (isServer) {
370+
code += `\nimport { useSSRContext } from 'vue'\n`
371+
code += `const _setup = script.setup\n`
372+
;(code += `script.setup = (props, ctx) => {`),
373+
(code += ` const ssrContext = useSSRContext()`),
374+
(code += ` ;(ssrContext._registeredComponents || (ssrContext._registeredComponents = new Set())).add(${JSON.stringify(
375+
hashSum(loaderContext.request)
376+
)});`)
377+
code += ` return _setup ? _setup(props, ctx) : undefined`
378+
code += `}\n`
379+
}
380+
366381
// finalize
367382
if (!propsToAttach.length) {
368383
code += `\n\nconst __exports__ = script;`

0 commit comments

Comments
 (0)