fix(nuxt): init $fetch at entry start (#23906)

This commit is contained in:
Anthony Fu 2023-10-25 08:31:25 +08:00 committed by GitHub
parent bd86e26763
commit 8c77ce81b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 11 deletions

View File

@ -1,11 +1,9 @@
// We set __webpack_public_path via this import with webpack builder // We set __webpack_public_path via this import with webpack builder
import { createApp, createSSRApp, nextTick } from 'vue' import { createApp, createSSRApp, nextTick } from 'vue'
import { $fetch } from 'ofetch'
import type { $Fetch, NitroFetchRequest } from 'nitropack'
// This file must be imported first for webpack as we set __webpack_public_path__ there // These files must be imported first as they have side effects
// @ts-expect-error virtual file import '#build/paths.mjs'
import { baseURL } from '#build/paths.mjs' import '#build/fetch.mjs'
import type { CreateOptions } from '#app' import type { CreateOptions } from '#app'
import { applyPlugins, createNuxtApp } from '#app/nuxt' import { applyPlugins, createNuxtApp } from '#app/nuxt'
@ -18,12 +16,6 @@ import RootComponent from '#build/root-component.mjs'
// @ts-expect-error virtual file // @ts-expect-error virtual file
import { vueAppRootContainer } from '#build/nuxt.config.mjs' import { vueAppRootContainer } from '#build/nuxt.config.mjs'
if (!globalThis.$fetch) {
globalThis.$fetch = $fetch.create({
baseURL: baseURL()
}) as $Fetch<unknown, NitroFetchRequest>
}
let entry: Function let entry: Function
if (import.meta.server) { if (import.meta.server) {

View File

@ -334,6 +334,21 @@ export const publicPathTemplate: NuxtTemplate = {
} }
} }
export const dollarFetchTemplate: NuxtTemplate = {
filename: 'fetch.mjs',
getContents () {
return [
"import { $fetch } from 'ofetch'",
"import { baseURL } from '#build/paths.mjs'",
'if (!globalThis.$fetch) {',
' globalThis.$fetch = $fetch.create({',
' baseURL: baseURL()',
' })',
'}'
].join('\n')
}
}
// Allow direct access to specific exposed nuxt.config // Allow direct access to specific exposed nuxt.config
export const nuxtConfigTemplate = { export const nuxtConfigTemplate = {
filename: 'nuxt.config.mjs', filename: 'nuxt.config.mjs',