-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: change runtime config using ipc signal
- Loading branch information
1 parent
235755b
commit 49383f1
Showing
6 changed files
with
92 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,48 @@ | ||
import { fileURLToPath } from 'node:url' | ||
import { describe, expect, it } from 'vitest' | ||
import { $fetch, setRuntimeConfig, setup } from '@nuxt/test-utils/e2e' | ||
import { $fetch, getBrowser, setRuntimeConfig, setup, url } from '@nuxt/test-utils/e2e' | ||
|
||
describe('ssr', async () => { | ||
await setup({ | ||
rootDir: fileURLToPath(new URL('./fixtures/basic', import.meta.url)), | ||
browser: true, | ||
}) | ||
|
||
it('renders the index page', async () => { | ||
// Get response to a server-rendered page with `$fetch`. | ||
const html = await $fetch('/') | ||
expect(html).toContain('<div>basic <span>original value</span></div>') | ||
expect(html).toContain('<span id="runtime">original value</span></div>') | ||
}) | ||
|
||
it('changes runtime config and restarts', async () => { | ||
const restoreConfig = await setRuntimeConfig({ public: { myValue: 'overwritten by test!' } }) | ||
it('changes runtime config client-side', async () => { | ||
const browser = await getBrowser() | ||
const page = await browser.newPage() | ||
await page.goto(url('/')) | ||
|
||
const html = await $fetch('/') | ||
expect(html).toContain('<div>basic <span>overwritten by test!</span></div>') | ||
const el = page.locator('#runtime') | ||
expect(await el.innerText()).to.equal('original value') | ||
|
||
await page.evaluate(() => { | ||
window.__NUXT_TEST_RUNTIME_CONFIG_SETTER__({ public: { myValue: 'overwritten by test!' } }) | ||
}) | ||
|
||
expect(await el.innerText()).to.equal('overwritten by test!') | ||
}) | ||
|
||
it('changes runtime config in server route', async () => { | ||
const originalConfig = await $fetch('/api/config') | ||
expect(originalConfig.public.myValue).to.equal('original value') | ||
|
||
await restoreConfig() | ||
const htmlRestored = await $fetch('/') | ||
expect(htmlRestored).toContain('<div>basic <span>original value</span></div>') | ||
await setRuntimeConfig({ public: { myValue: 'overwritten by test!' } }) | ||
|
||
const newConfig = await $fetch('/api/config') | ||
expect(newConfig.public.myValue).to.equal('overwritten by test!') | ||
}) | ||
|
||
it('changes runtime config', async () => { | ||
await setRuntimeConfig({ public: { myValue: 'overwritten by test!' } }) | ||
|
||
const html = await $fetch('/') | ||
expect(html).toContain('<span id="runtime">overwritten by test!</span></div>') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
<template> | ||
<div>basic <span>{{ config.public.myValue }}</span></div> | ||
<div> | ||
basic <span id="runtime">{{ config.public.myValue }}</span> | ||
</div> | ||
</template> | ||
|
||
<script setup> | ||
const config = useRuntimeConfig(); | ||
const config = useRuntimeConfig() | ||
</script> |
30 changes: 30 additions & 0 deletions
30
examples/module/test/fixtures/basic/plugins/ipc-listener.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import defu from 'defu' | ||
import { defineNuxtPlugin } from 'nuxt/app' | ||
|
||
declare global { | ||
interface Window { | ||
__NUXT_TEST_RUNTIME_CONFIG_SETTER__: (env: { public: Record<string, unknown> }) => void | ||
} | ||
} | ||
|
||
export default defineNuxtPlugin(() => { | ||
const config = useRuntimeConfig() | ||
|
||
if (process.client) { | ||
window.__NUXT_TEST_RUNTIME_CONFIG_SETTER__ ??= (env: { public: Record<string, unknown> }) => { | ||
config.public = defu(env.public, config.public) | ||
} | ||
} | ||
|
||
if (process.server) { | ||
process.on('message', (msg: { type: string; value: Record<string, string> }) => { | ||
if (msg.type === 'update:runtime-config') { | ||
for (const [key, value] of Object.entries(msg.value)) { | ||
process.env[key] = value | ||
} | ||
|
||
process!.send!({ type: 'confirm:runtime-config' }) | ||
} | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default defineEventHandler(async (event) => { | ||
const config = useRuntimeConfig(event) | ||
return config | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters