Skip to content

Commit

Permalink
fix: module stability (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
atinux authored Jun 11, 2021
1 parent 6a71ec5 commit e9aa91c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
5 changes: 0 additions & 5 deletions lib/templates/color-scheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ export default {
}
},
render (createElement, { parent, data, props, children }) {
const { $colorMode } = parent

if (!$colorMode.unknown) {
return children
}
// transform props for <client-only>
props = {
placeholder: props.placeholder,
Expand Down
23 changes: 18 additions & 5 deletions lib/templates/plugin.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,26 @@ const script = {
innerHTML: `<%= options.script %>`,
pbody: true
}
const addScript = (head) => {
head.script = head.script || []
head.script.push(script)
const serializeProp = '__dangerouslyDisableSanitizersByTagID'
head[serializeProp] = head[serializeProp] || {}
head[serializeProp]['<%= options.hid %>'] = ['innerHTML']
}

export default function (ctx, inject) {
ctx.app.head.script.push(script)

const serializeProp = '__dangerouslyDisableSanitizersByTagID'
ctx.app.head[serializeProp] = ctx.app.head[serializeProp] || {}
ctx.app.head[serializeProp]['<%= options.hid %>'] = ['innerHTML']
export default function (ctx, inject) {
if (typeof ctx.app.head === 'function') {
const originalHead = ctx.app.head
ctx.app.head = function () {
const head = originalHead.call(this) || {}
addScript(head)
return head
}
} else {
addScript(ctx.app.head)
}

const preference = '<%= options.preference %>'

Expand Down
31 changes: 31 additions & 0 deletions test/csr.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { join } from 'path'
import { setup, get } from '@nuxtjs/module-test-utils'
import colorModeModule from '../'

describe('module', () => {
let nuxt

beforeAll(async () => {
const rootDir = join(__dirname, '..', 'example')

const config = {
ssr: false,
rootDir,
modules: [
'@nuxtjs/svg',
colorModeModule
]
}

nuxt = (await setup(config)).nuxt
}, 60000)

afterAll(async () => {
await nuxt.close()
})

test('render', async () => {
const html = await get('/')
expect(html).toContain('nuxt-color-mode')
})
})
2 changes: 1 addition & 1 deletion test/module.test.js → test/ssr.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { join } from 'path'
import { setup, get } from '@nuxtjs/module-test-utils'
import colorModeModule from '..'
import colorModeModule from '../'

describe('module', () => {
let nuxt
Expand Down

0 comments on commit e9aa91c

Please sign in to comment.