fix: add nitro client plugin ($fetch support) (#223)

* fix(nuxt3): add nitro client plugin ($fetch support)

closes #213

* refactor: put hook in more logical place

* docs: update comment
This commit is contained in:
Daniel Roe 2021-06-16 12:20:18 +01:00 committed by GitHub
parent e4bb36883a
commit e2d5a2f4b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 4 deletions

View File

@ -10,14 +10,15 @@ const commonPlugins = [
<%= app.plugins.filter(p => !p.mode || p.mode === 'all').map(p => utils.importName(p.src)).join(',\n ') %>
]
export const clientPluigns = [
...commonPlugins,<%= app.plugins.filter(p => p.mode === 'client').map(p => utils.importName(p.src)).join(',\n ') %>
export const clientPlugins = [
...commonPlugins,
<%= app.plugins.filter(p => p.mode === 'client').map(p => utils.importName(p.src)).join(',\n ') %>
]
export const serverPluigns = [
export const serverPlugins = [
...commonPlugins,
preload,
<%= app.plugins.filter(p => p.mode === 'server').map(p => utils.importName(p.src)).join(',\n ') %>
]
export default process.client ? clientPluigns : serverPluigns
export default process.client ? clientPlugins : serverPlugins

View File

@ -3,3 +3,5 @@ import { $fetch } from 'ohmyfetch'
if (!globalThis.$fetch) {
globalThis.$fetch = $fetch
}
export default () => {}

View File

@ -18,6 +18,11 @@ export function initNitro (nuxt: Nuxt) {
nuxt.hooks.addHooks(nitroDevContext.nuxtHooks)
nuxt.hook('close', () => nitroDevContext._internal.hooks.callHook('close'))
// Add nitro client plugin (to inject $fetch helper)
nuxt.hook('app:resolve', (app) => {
app.plugins.push({ src: require.resolve('@nuxt/nitro/dist/runtime/app/nitro.client') })
})
// Expose process.env.NITRO_PRESET
nuxt.options.env.NITRO_PRESET = nitroContext.preset