Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(renderToString): Remove document usage on SSR render #2023

Merged
merged 1 commit into from
Apr 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions src/createInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,6 @@ export function createInstance(
// Let's register it as a stub so user can find it
registerStub({ source: originalComponent, stub: component })

const el = document.createElement('div')

if (options?.attachTo) {
let to: Element | null
if (typeof options.attachTo === 'string') {
to = document.querySelector(options.attachTo)
if (!to) {
throw new Error(
`Unable to find the element matching the selector ${options.attachTo} given as the \`attachTo\` option`
)
}
} else {
to = options.attachTo
}

to.appendChild(el)
}

function slotToFunction(slot: Slot) {
switch (typeof slot) {
case 'function':
Expand Down Expand Up @@ -359,7 +341,6 @@ export function createInstance(

return {
app,
el,
props,
componentRef
}
Expand Down
22 changes: 18 additions & 4 deletions src/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,7 @@ export function mount(
inputComponent: any,
options?: MountingOptions<any> & Record<string, any>
): VueWrapper<any> {
const { app, props, el, componentRef } = createInstance(
inputComponent,
options
)
const { app, props, componentRef } = createInstance(inputComponent, options)

const setProps = (newProps: Record<string, unknown>) => {
for (const [k, v] of Object.entries(newProps)) {
Expand All @@ -272,6 +269,23 @@ export function mount(
}

// mount the app!
const el = document.createElement('div')

if (options?.attachTo) {
let to: Element | null
if (typeof options.attachTo === 'string') {
to = document.querySelector(options.attachTo)
if (!to) {
throw new Error(
`Unable to find the element matching the selector ${options.attachTo} given as the \`attachTo\` option`
)
}
} else {
to = options.attachTo
}

to.appendChild(el)
}
const vm = app.mount(el)

if (errorOnMount) {
Expand Down
2 changes: 1 addition & 1 deletion tests/__snapshots__/shallowMount.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Vitest Snapshot v1
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`shallowMount > renders props for stubbed component in a snapshot 1`] = `
<div>
Expand Down
5 changes: 5 additions & 0 deletions tests/renderToString.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* Run SSR tests in node environment
* @vitest-environment node
*/

import { describe, it, expect } from 'vitest'
import { defineComponent, onMounted, onServerPrefetch, ref } from 'vue'
import { renderToString } from '../src'
Expand Down