mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
refactor: update unjs dependencies to stable v1 (#9011)
This commit is contained in:
parent
4c8369fbf3
commit
b49f2a8147
@ -352,7 +352,7 @@ export default defineComponent({
|
|||||||
|
|
||||||
## Directly Calling an API Endpoint
|
## Directly Calling an API Endpoint
|
||||||
|
|
||||||
There are instances where you may need to directly call the API. Nuxt 3 provides a globally available `$fetch` method using [unjs/ohmyfetch](https://github.com/unjs/ohmyfetch) (in addition to `fetch`)
|
There are instances where you may need to directly call the API. Nuxt 3 provides a globally available `$fetch` method using [unjs/ofetch](https://github.com/unjs/ofetch) (in addition to `fetch`)
|
||||||
with the same API as the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch).
|
with the same API as the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch).
|
||||||
|
|
||||||
Using `$fetch` has a number of benefits, including:
|
Using `$fetch` has a number of benefits, including:
|
||||||
|
@ -32,12 +32,12 @@ Learn more about the API layer in the [`server/`](/guide/directory-structure/ser
|
|||||||
|
|
||||||
Nitro allows 'direct' calling of routes via the globally-available `$fetch` helper. This will make an API call to the server if run on the browser, but will directly call the relevant function if run on the server, **saving an additional API call**.
|
Nitro allows 'direct' calling of routes via the globally-available `$fetch` helper. This will make an API call to the server if run on the browser, but will directly call the relevant function if run on the server, **saving an additional API call**.
|
||||||
|
|
||||||
`$fetch` API is using [ohmyfetch](https://github.com/unjs/ohmyfetch), with key features including:
|
`$fetch` API is using [ofetch](https://github.com/unjs/ofetch), with key features including:
|
||||||
|
|
||||||
- Automatic parsing of JSON responses (with access to raw response if needed)
|
- Automatic parsing of JSON responses (with access to raw response if needed)
|
||||||
- Request body and params are automatically handled, with correct `Content-Type` headers
|
- Request body and params are automatically handled, with correct `Content-Type` headers
|
||||||
|
|
||||||
For more information on `$fetch` features, check out [ohmyfetch](https://github.com/unjs/ohmyfetch).
|
For more information on `$fetch` features, check out [ofetch](https://github.com/unjs/ofetch).
|
||||||
|
|
||||||
## Typed API Routes
|
## Typed API Routes
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ Nuxt automatically scans files inside the `~/server/api`, `~/server/routes`, and
|
|||||||
|
|
||||||
Each file should export a default function defined with `defineEventHandler()`.
|
Each file should export a default function defined with `defineEventHandler()`.
|
||||||
|
|
||||||
The handler can directly return JSON data, a `Promise` or use `event.res.end()` to send response.
|
The handler can directly return JSON data, a `Promise` or use `event.node.res.end()` to send response.
|
||||||
|
|
||||||
::ReadMore{link="https://nitro.unjs.io/guide/introduction/routing" title="Nitro Route Handling Docs"}
|
::ReadMore{link="https://nitro.unjs.io/guide/introduction/routing" title="Nitro Route Handling Docs"}
|
||||||
::
|
::
|
||||||
@ -57,7 +57,7 @@ Middleware handlers should not return anything (nor close or respond to the requ
|
|||||||
|
|
||||||
```ts [server/middleware/log.ts]
|
```ts [server/middleware/log.ts]
|
||||||
export default defineEventHandler((event) => {
|
export default defineEventHandler((event) => {
|
||||||
console.log('New request: ' + event.req.url)
|
console.log('New request: ' + event.node.req.url)
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -41,7 +41,7 @@ type AsyncData<DataT> = {
|
|||||||
## Params
|
## Params
|
||||||
|
|
||||||
* **Url**: The URL to fetch.
|
* **Url**: The URL to fetch.
|
||||||
* **Options (extends [unjs/ohmyfetch](https://github.com/unjs/ohmyfetch) options & [AsyncDataOptions](/api/composables/use-async-data#params))**:
|
* **Options (extends [unjs/ofetch](https://github.com/unjs/ofetch) options & [AsyncDataOptions](/api/composables/use-async-data#params))**:
|
||||||
* `method`: Request method.
|
* `method`: Request method.
|
||||||
* `query`: Adds query search params to URL using [ufo](https://github.com/unjs/ufo)
|
* `query`: Adds query search params to URL using [ufo](https://github.com/unjs/ufo)
|
||||||
* `params`: Alias for `query`
|
* `params`: Alias for `query`
|
||||||
@ -89,7 +89,7 @@ const { data, pending, error, refresh } = await useFetch('https://api.nuxtjs.dev
|
|||||||
|
|
||||||
Adding Query Search Params:
|
Adding Query Search Params:
|
||||||
|
|
||||||
Using the `query` option, you can add search parameters to your query. This option is extended from [unjs/ohmyfetch](https://github.com/unjs/ohmyfetch) and is using [ufo](https://github.com/unjs/ufo) to create the URL. Objects are automatically stringified.
|
Using the `query` option, you can add search parameters to your query. This option is extended from [unjs/ofetch](https://github.com/unjs/ofetch) and is using [unjs/ufo](https://github.com/unjs/ufo) to create the URL. Objects are automatically stringified.
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
const param1 = ref('value1')
|
const param1 = ref('value1')
|
||||||
@ -100,7 +100,7 @@ const { data, pending, error, refresh } = await useFetch('https://api.nuxtjs.dev
|
|||||||
|
|
||||||
Results in `https://api.nuxtjs.dev/mountains?param1=value1¶m2=value2`
|
Results in `https://api.nuxtjs.dev/mountains?param1=value1¶m2=value2`
|
||||||
|
|
||||||
Using [interceptors](https://github.com/unjs/ohmyfetch#%EF%B8%8F-interceptors):
|
Using [interceptors](https://github.com/unjs/ofetch#%EF%B8%8F-interceptors):
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
const { data, pending, error, refresh } = await useFetch('/api/auth/login', {
|
const { data, pending, error, refresh } = await useFetch('/api/auth/login', {
|
||||||
|
@ -12,7 +12,7 @@ Within your pages, components, and plugins you can use `useRequestEvent` to acce
|
|||||||
const event = useRequestEvent()
|
const event = useRequestEvent()
|
||||||
|
|
||||||
// Get the URL
|
// Get the URL
|
||||||
const url = event.req.url
|
const url = event.node.req.url
|
||||||
```
|
```
|
||||||
|
|
||||||
::alert{icon=👉}
|
::alert{icon=👉}
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
---
|
---
|
||||||
title: "$fetch"
|
title: "$fetch"
|
||||||
description: Nuxt uses ohmyfetch to expose globally the $fetch helper for making HTTP requests.
|
description: Nuxt uses ofetch to expose globally the $fetch helper for making HTTP requests.
|
||||||
---
|
---
|
||||||
|
|
||||||
# `$fetch`
|
# `$fetch`
|
||||||
|
|
||||||
Nuxt uses [ohmyfetch](https://github.com/unjs/ohmyfetch) to expose globally the `$fetch` helper for making HTTP requests within your Vue app or API routes.
|
Nuxt uses [ofetch](https://github.com/unjs/ofetch) to expose globally the `$fetch` helper for making HTTP requests within your Vue app or API routes.
|
||||||
|
|
||||||
::ReadMore{link="/getting-started/data-fetching"}
|
::ReadMore{link="/getting-started/data-fetching"}
|
||||||
::
|
::
|
||||||
|
@ -11,7 +11,7 @@ Nuxt 3 provides new options for [fetching data from an API](/getting-started/dat
|
|||||||
|
|
||||||
In Nuxt 2 you might use `@nuxtjs/axios` or `@nuxt/http` to fetch your data - or just the polyfilled global `fetch`.
|
In Nuxt 2 you might use `@nuxtjs/axios` or `@nuxt/http` to fetch your data - or just the polyfilled global `fetch`.
|
||||||
|
|
||||||
In Nuxt 3 you can use a globally available `fetch` method that has the same API as [the Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) or `$fetch` method which is using [unjs/ohmyfetch](https://github.com/unjs/ohmyfetch). It has a number of benefits, including:
|
In Nuxt 3 you can use a globally available `fetch` method that has the same API as [the Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) or `$fetch` method which is using [unjs/ofetch](https://github.com/unjs/ofetch). It has a number of benefits, including:
|
||||||
|
|
||||||
1. It will handle 'smartly' making [direct API calls](/guide/concepts/server-engine#direct-api-calls) if it's running on the server, or making a client-side call to your API if it's running on the client. (It can also handle calling third-party APIs.)
|
1. It will handle 'smartly' making [direct API calls](/guide/concepts/server-engine#direct-api-calls) if it's running on the server, or making a client-side call to your API if it's running on the client. (It can also handle calling third-party APIs.)
|
||||||
|
|
||||||
|
@ -12,8 +12,8 @@
|
|||||||
"@nuxt-themes/website": "0.1.9",
|
"@nuxt-themes/website": "0.1.9",
|
||||||
"jiti": "^1.16.0",
|
"jiti": "^1.16.0",
|
||||||
"nuxt": "^3.0.0-rc.12",
|
"nuxt": "^3.0.0-rc.12",
|
||||||
"pathe": "^0.3.9",
|
"pathe": "^1.0.0",
|
||||||
"scule": "^0.3.2",
|
"scule": "^1.0.0",
|
||||||
"untyped": "^0.5.0"
|
"untyped": "^0.5.0"
|
||||||
},
|
},
|
||||||
"packageManager": "yarn@3.2.4"
|
"packageManager": "yarn@3.2.4"
|
||||||
|
@ -1 +1 @@
|
|||||||
export default defineEventHandler(event => `Hello world (${event.req.url.substr(1)}) (Generated at ${new Date().toUTCString()})`)
|
export default defineEventHandler(event => `Hello world (${event.node.req.url.substr(1)}) (Generated at ${new Date().toUTCString()})`)
|
||||||
|
@ -64,13 +64,13 @@
|
|||||||
"markdownlint-cli": "^0.32.2",
|
"markdownlint-cli": "^0.32.2",
|
||||||
"nuxi": "workspace:*",
|
"nuxi": "workspace:*",
|
||||||
"nuxt": "workspace:*",
|
"nuxt": "workspace:*",
|
||||||
"ohmyfetch": "^0.4.21",
|
"ofetch": "^1.0.0",
|
||||||
"pathe": "^0.3.9",
|
"pathe": "^1.0.0",
|
||||||
"rimraf": "^3.0.2",
|
"rimraf": "^3.0.2",
|
||||||
"semver": "^7.3.8",
|
"semver": "^7.3.8",
|
||||||
"std-env": "^3.3.0",
|
"std-env": "^3.3.0",
|
||||||
"typescript": "^4.8.4",
|
"typescript": "^4.8.4",
|
||||||
"ufo": "^0.8.6",
|
"ufo": "^1.0.0",
|
||||||
"unbuild": "^0.9.4",
|
"unbuild": "^0.9.4",
|
||||||
"vite": "^3.2.3",
|
"vite": "^3.2.3",
|
||||||
"vitest": "^0.25.1",
|
"vitest": "^0.25.1",
|
||||||
|
@ -14,22 +14,22 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@nuxt/schema": "3.0.0-rc.13",
|
"@nuxt/schema": "3.0.0-rc.13",
|
||||||
"c12": "^0.2.13",
|
"c12": "^1.0.1",
|
||||||
"consola": "^2.15.3",
|
"consola": "^2.15.3",
|
||||||
"defu": "^6.1.0",
|
"defu": "^6.1.0",
|
||||||
"globby": "^13.1.2",
|
"globby": "^13.1.2",
|
||||||
"hash-sum": "^2.0.0",
|
"hash-sum": "^2.0.0",
|
||||||
"ignore": "^5.2.0",
|
"ignore": "^5.2.0",
|
||||||
"jiti": "^1.16.0",
|
"jiti": "^1.16.0",
|
||||||
"knitwork": "^0.1.2",
|
"knitwork": "^1.0.0",
|
||||||
"lodash.template": "^4.5.0",
|
"lodash.template": "^4.5.0",
|
||||||
"mlly": "^0.5.16",
|
"mlly": "^1.0.0",
|
||||||
"pathe": "^0.3.9",
|
"pathe": "^1.0.0",
|
||||||
"pkg-types": "^0.3.6",
|
"pkg-types": "^1.0.1",
|
||||||
"scule": "^0.3.2",
|
"scule": "^1.0.0",
|
||||||
"semver": "^7.3.8",
|
"semver": "^7.3.8",
|
||||||
"unctx": "^2.0.2",
|
"unctx": "^2.0.2",
|
||||||
"unimport": "^0.7.0",
|
"unimport": "^1.0.0",
|
||||||
"untyped": "^0.5.0"
|
"untyped": "^0.5.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
"@types/flat": "^5.0.2",
|
"@types/flat": "^5.0.2",
|
||||||
"@types/mri": "^1.1.1",
|
"@types/mri": "^1.1.1",
|
||||||
"@types/semver": "^7",
|
"@types/semver": "^7",
|
||||||
"c12": "^0.2.13",
|
"c12": "^1.0.1",
|
||||||
"chokidar": "^3.5.3",
|
"chokidar": "^3.5.3",
|
||||||
"clear": "^0.1.0",
|
"clear": "^0.1.0",
|
||||||
"clipboardy": "^3.0.0",
|
"clipboardy": "^3.0.0",
|
||||||
@ -35,15 +35,15 @@
|
|||||||
"execa": "^6.1.0",
|
"execa": "^6.1.0",
|
||||||
"flat": "^5.0.2",
|
"flat": "^5.0.2",
|
||||||
"giget": "^0.1.7",
|
"giget": "^0.1.7",
|
||||||
"h3": "^0.8.6",
|
"h3": "^1.0.1",
|
||||||
"jiti": "^1.16.0",
|
"jiti": "^1.16.0",
|
||||||
"listhen": "^0.3.5",
|
"listhen": "^0.3.5",
|
||||||
"mlly": "^0.5.16",
|
"mlly": "^1.0.0",
|
||||||
"mri": "^1.2.0",
|
"mri": "^1.2.0",
|
||||||
"pathe": "^0.3.9",
|
"pathe": "^1.0.0",
|
||||||
"perfect-debounce": "^0.1.3",
|
"perfect-debounce": "^0.1.3",
|
||||||
"pkg-types": "^0.3.6",
|
"pkg-types": "^1.0.1",
|
||||||
"scule": "^0.3.2",
|
"scule": "^1.0.0",
|
||||||
"semver": "^7.3.8",
|
"semver": "^7.3.8",
|
||||||
"unbuild": "latest"
|
"unbuild": "latest"
|
||||||
},
|
},
|
||||||
|
@ -39,7 +39,7 @@ export default defineNuxtCommand({
|
|||||||
|
|
||||||
const serveFile = (filePath: string) => lazyEventHandler(async () => {
|
const serveFile = (filePath: string) => lazyEventHandler(async () => {
|
||||||
const contents = await fsp.readFile(filePath, 'utf-8')
|
const contents = await fsp.readFile(filePath, 'utf-8')
|
||||||
return eventHandler((event) => { event.res.end(contents) })
|
return eventHandler((event) => { event.node.res.end(contents) })
|
||||||
})
|
})
|
||||||
|
|
||||||
console.warn('Do not deploy analyze results! Use `nuxi build` before deploying.')
|
console.warn('Do not deploy analyze results! Use `nuxi build` before deploying.')
|
||||||
|
@ -17,7 +17,7 @@ export default defineBuildConfig({
|
|||||||
dependencies: [
|
dependencies: [
|
||||||
'nuxi',
|
'nuxi',
|
||||||
'vue-router',
|
'vue-router',
|
||||||
'ohmyfetch'
|
'ofetch'
|
||||||
],
|
],
|
||||||
externals: [
|
externals: [
|
||||||
'@vue/reactivity',
|
'@vue/reactivity',
|
||||||
|
@ -53,25 +53,25 @@
|
|||||||
"estree-walker": "^3.0.1",
|
"estree-walker": "^3.0.1",
|
||||||
"fs-extra": "^10.1.0",
|
"fs-extra": "^10.1.0",
|
||||||
"globby": "^13.1.2",
|
"globby": "^13.1.2",
|
||||||
"h3": "^0.8.6",
|
"h3": "^1.0.1",
|
||||||
"hash-sum": "^2.0.0",
|
"hash-sum": "^2.0.0",
|
||||||
"hookable": "^5.4.1",
|
"hookable": "^5.4.1",
|
||||||
"knitwork": "^0.1.2",
|
"knitwork": "^1.0.0",
|
||||||
"magic-string": "^0.26.7",
|
"magic-string": "^0.26.7",
|
||||||
"mlly": "^0.5.16",
|
"mlly": "^1.0.0",
|
||||||
"nitropack": "^0.6.1",
|
"nitropack": "^1.0.0-0",
|
||||||
"nuxi": "3.0.0-rc.13",
|
"nuxi": "3.0.0-rc.13",
|
||||||
"ohash": "^0.1.5",
|
"ohash": "^1.0.0",
|
||||||
"ohmyfetch": "^0.4.21",
|
"ofetch": "^1.0.0",
|
||||||
"pathe": "^0.3.9",
|
"pathe": "^1.0.0",
|
||||||
"perfect-debounce": "^0.1.3",
|
"perfect-debounce": "^0.1.3",
|
||||||
"scule": "^0.3.2",
|
"scule": "^1.0.0",
|
||||||
"strip-literal": "^0.4.2",
|
"strip-literal": "^1.0.0",
|
||||||
"ufo": "^0.8.6",
|
"ufo": "^1.0.0",
|
||||||
"ultrahtml": "^1.0.0",
|
"ultrahtml": "^1.0.0",
|
||||||
"unctx": "^2.0.2",
|
"unctx": "^2.0.2",
|
||||||
"unenv": "^0.6.2",
|
"unenv": "^0.6.2",
|
||||||
"unimport": "^0.7.0",
|
"unimport": "^1.0.0",
|
||||||
"unplugin": "^0.10.2",
|
"unplugin": "^0.10.2",
|
||||||
"untyped": "^0.5.0",
|
"untyped": "^0.5.0",
|
||||||
"vue": "^3.2.45",
|
"vue": "^3.2.45",
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import type { FetchError, FetchOptions } from 'ohmyfetch'
|
import type { FetchError, FetchOptions } from 'ofetch'
|
||||||
import type { TypedInternalResponse, NitroFetchRequest } from 'nitropack'
|
import type { TypedInternalResponse, NitroFetchRequest } from 'nitropack'
|
||||||
import { computed, unref, Ref, reactive } from 'vue'
|
import { computed, unref, Ref, reactive } from 'vue'
|
||||||
import type { AsyncDataOptions, _Transform, KeyOfRes, AsyncData, PickFrom } from './asyncData'
|
import type { AsyncDataOptions, _Transform, KeyOfRes, AsyncData, PickFrom } from './asyncData'
|
||||||
|
@ -6,7 +6,7 @@ export function useRequestHeaders<K extends string = string> (include: K[]): Rec
|
|||||||
export function useRequestHeaders (): Readonly<Record<string, string | undefined>>
|
export function useRequestHeaders (): Readonly<Record<string, string | undefined>>
|
||||||
export function useRequestHeaders (include?: any[]) {
|
export function useRequestHeaders (include?: any[]) {
|
||||||
if (process.client) { return {} }
|
if (process.client) { return {} }
|
||||||
const headers = useNuxtApp().ssrContext?.event.req.headers ?? {}
|
const headers = useNuxtApp().ssrContext?.event.node.req.headers ?? {}
|
||||||
if (!include) { return headers }
|
if (!include) { return headers }
|
||||||
return Object.fromEntries(include.map(key => key.toLowerCase()).filter(key => headers[key]).map(key => [key, headers[key]]))
|
return Object.fromEntries(include.map(key => key.toLowerCase()).filter(key => headers[key]).map(key => [key, headers[key]]))
|
||||||
}
|
}
|
||||||
@ -18,9 +18,9 @@ export function useRequestEvent (nuxtApp: NuxtApp = useNuxtApp()): H3Event {
|
|||||||
export function setResponseStatus (code: number, message?: string) {
|
export function setResponseStatus (code: number, message?: string) {
|
||||||
const event = process.server && useRequestEvent()
|
const event = process.server && useRequestEvent()
|
||||||
if (event) {
|
if (event) {
|
||||||
event.res.statusCode = code
|
event.node.res.statusCode = code
|
||||||
if (message) {
|
if (message) {
|
||||||
event.res.statusMessage = message
|
event.node.res.statusMessage = message
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
// We set __webpack_public_path via this import with webpack builder
|
// We set __webpack_public_path via this import with webpack builder
|
||||||
import { createSSRApp, createApp, nextTick } from 'vue'
|
import { createSSRApp, createApp, nextTick } from 'vue'
|
||||||
import { $fetch } from 'ohmyfetch'
|
import { $fetch } from 'ofetch'
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import { baseURL } from '#build/paths.mjs'
|
import { baseURL } from '#build/paths.mjs'
|
||||||
import { createNuxtApp, applyPlugins, normalizePlugins, CreateOptions } from '#app'
|
import { createNuxtApp, applyPlugins, normalizePlugins, CreateOptions } from '#app'
|
||||||
|
@ -10,7 +10,7 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
|
|||||||
|
|
||||||
// Create an error object
|
// Create an error object
|
||||||
const errorObject = {
|
const errorObject = {
|
||||||
url: event.req.url,
|
url: event.node.req.url,
|
||||||
statusCode,
|
statusCode,
|
||||||
statusMessage,
|
statusMessage,
|
||||||
message,
|
message,
|
||||||
@ -21,9 +21,9 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set response code and message
|
// Set response code and message
|
||||||
event.res.statusCode = (errorObject.statusCode !== 200 && errorObject.statusCode) as any as number || 500
|
event.node.res.statusCode = (errorObject.statusCode !== 200 && errorObject.statusCode) as any as number || 500
|
||||||
if (errorObject.statusMessage) {
|
if (errorObject.statusMessage) {
|
||||||
event.res.statusMessage = errorObject.statusMessage
|
event.node.res.statusMessage = errorObject.statusMessage
|
||||||
}
|
}
|
||||||
// Console output
|
// Console output
|
||||||
if (error.unhandled || error.fatal) {
|
if (error.unhandled || error.fatal) {
|
||||||
@ -39,13 +39,13 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
|
|||||||
|
|
||||||
// JSON response
|
// JSON response
|
||||||
if (isJsonRequest(event)) {
|
if (isJsonRequest(event)) {
|
||||||
event.res.setHeader('Content-Type', 'application/json')
|
event.node.res.setHeader('Content-Type', 'application/json')
|
||||||
event.res.end(JSON.stringify(errorObject))
|
event.node.res.end(JSON.stringify(errorObject))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTML response (via SSR)
|
// HTML response (via SSR)
|
||||||
const isErrorPage = event.req.url?.startsWith('/__nuxt_error')
|
const isErrorPage = event.node.req.url?.startsWith('/__nuxt_error')
|
||||||
const res = !isErrorPage
|
const res = !isErrorPage
|
||||||
? await useNitroApp().localFetch(withQuery(joinURL(useRuntimeConfig().app.baseURL, '/__nuxt_error'), errorObject), {
|
? await useNitroApp().localFetch(withQuery(joinURL(useRuntimeConfig().app.baseURL, '/__nuxt_error'), errorObject), {
|
||||||
headers: getRequestHeaders(event) as Record<string, string>,
|
headers: getRequestHeaders(event) as Record<string, string>,
|
||||||
@ -64,8 +64,8 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
|
|||||||
// TODO: Support `message` in template
|
// TODO: Support `message` in template
|
||||||
(errorObject as any).description = errorObject.message
|
(errorObject as any).description = errorObject.message
|
||||||
}
|
}
|
||||||
event.res.setHeader('Content-Type', 'text/html;charset=UTF-8')
|
event.node.res.setHeader('Content-Type', 'text/html;charset=UTF-8')
|
||||||
event.res.end(template(errorObject))
|
event.node.res.end(template(errorObject))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,12 +74,12 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (res.status && res.status !== 200) {
|
if (res.status && res.status !== 200) {
|
||||||
event.res.statusCode = res.status
|
event.node.res.statusCode = res.status
|
||||||
}
|
}
|
||||||
|
|
||||||
if (res.statusText) {
|
if (res.statusText) {
|
||||||
event.res.statusMessage = res.statusText
|
event.node.res.statusMessage = res.statusText
|
||||||
}
|
}
|
||||||
|
|
||||||
event.res.end(await res.text())
|
event.node.res.end(await res.text())
|
||||||
}
|
}
|
||||||
|
@ -119,20 +119,20 @@ const PRERENDER_NO_SSR_ROUTES = new Set(['/index.html', '/200.html', '/404.html'
|
|||||||
|
|
||||||
export default defineRenderHandler(async (event) => {
|
export default defineRenderHandler(async (event) => {
|
||||||
// Whether we're rendering an error page
|
// Whether we're rendering an error page
|
||||||
const ssrError = event.req.url?.startsWith('/__nuxt_error')
|
const ssrError = event.node.req.url?.startsWith('/__nuxt_error')
|
||||||
? getQuery(event) as Exclude<NuxtApp['payload']['error'], Error>
|
? getQuery(event) as Exclude<NuxtApp['payload']['error'], Error>
|
||||||
: null
|
: null
|
||||||
if (ssrError && event.req.socket.readyState !== 'readOnly' /* direct request */) {
|
if (ssrError && event.node.req.socket.readyState !== 'readOnly' /* direct request */) {
|
||||||
throw createError('Cannot directly render error page!')
|
throw createError('Cannot directly render error page!')
|
||||||
}
|
}
|
||||||
|
|
||||||
let url = ssrError?.url as string || event.req.url!
|
let url = ssrError?.url as string || event.node.req.url!
|
||||||
|
|
||||||
// Whether we are rendering payload route
|
// Whether we are rendering payload route
|
||||||
const isRenderingPayload = PAYLOAD_URL_RE.test(url)
|
const isRenderingPayload = PAYLOAD_URL_RE.test(url)
|
||||||
if (isRenderingPayload) {
|
if (isRenderingPayload) {
|
||||||
url = url.substring(0, url.lastIndexOf('/')) || '/'
|
url = url.substring(0, url.lastIndexOf('/')) || '/'
|
||||||
event.req.url = url
|
event.node.req.url = url
|
||||||
if (process.env.prerender && PAYLOAD_CACHE!.has(url)) {
|
if (process.env.prerender && PAYLOAD_CACHE!.has(url)) {
|
||||||
return PAYLOAD_CACHE!.get(url)
|
return PAYLOAD_CACHE!.get(url)
|
||||||
}
|
}
|
||||||
@ -148,7 +148,7 @@ export default defineRenderHandler(async (event) => {
|
|||||||
runtimeConfig: useRuntimeConfig() as NuxtSSRContext['runtimeConfig'],
|
runtimeConfig: useRuntimeConfig() as NuxtSSRContext['runtimeConfig'],
|
||||||
noSSR:
|
noSSR:
|
||||||
!!(process.env.NUXT_NO_SSR) ||
|
!!(process.env.NUXT_NO_SSR) ||
|
||||||
!!(event.req.headers['x-nuxt-no-ssr']) ||
|
!!(event.node.req.headers['x-nuxt-no-ssr']) ||
|
||||||
routeOptions.ssr === false ||
|
routeOptions.ssr === false ||
|
||||||
(process.env.prerender ? PRERENDER_NO_SSR_ROUTES.has(url) : false),
|
(process.env.prerender ? PRERENDER_NO_SSR_ROUTES.has(url) : false),
|
||||||
error: !!ssrError,
|
error: !!ssrError,
|
||||||
@ -247,8 +247,8 @@ export default defineRenderHandler(async (event) => {
|
|||||||
// Construct HTML response
|
// Construct HTML response
|
||||||
const response: RenderResponse = {
|
const response: RenderResponse = {
|
||||||
body: renderHTMLDocument(htmlContext),
|
body: renderHTMLDocument(htmlContext),
|
||||||
statusCode: event.res.statusCode,
|
statusCode: event.node.res.statusCode,
|
||||||
statusMessage: event.res.statusMessage,
|
statusMessage: event.node.res.statusMessage,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'text/html;charset=UTF-8',
|
'Content-Type': 'text/html;charset=UTF-8',
|
||||||
'X-Powered-By': 'Nuxt'
|
'X-Powered-By': 'Nuxt'
|
||||||
@ -304,8 +304,8 @@ async function renderInlineStyles (usedModules: Set<string> | string[]) {
|
|||||||
function renderPayloadResponse (ssrContext: NuxtSSRContext) {
|
function renderPayloadResponse (ssrContext: NuxtSSRContext) {
|
||||||
return <RenderResponse> {
|
return <RenderResponse> {
|
||||||
body: `export default ${devalue(splitPayload(ssrContext).payload)}`,
|
body: `export default ${devalue(splitPayload(ssrContext).payload)}`,
|
||||||
statusCode: ssrContext.event.res.statusCode,
|
statusCode: ssrContext.event.node.res.statusCode,
|
||||||
statusMessage: ssrContext.event.res.statusMessage,
|
statusMessage: ssrContext.event.node.res.statusMessage,
|
||||||
headers: {
|
headers: {
|
||||||
'content-type': 'text/javascript;charset=UTF-8',
|
'content-type': 'text/javascript;charset=UTF-8',
|
||||||
'x-powered-by': 'Nuxt'
|
'x-powered-by': 'Nuxt'
|
||||||
|
@ -18,22 +18,22 @@
|
|||||||
"@types/semver": "^7",
|
"@types/semver": "^7",
|
||||||
"@vitejs/plugin-vue": "^3.2.0",
|
"@vitejs/plugin-vue": "^3.2.0",
|
||||||
"@vueuse/head": "~1.0.0-rc.14",
|
"@vueuse/head": "~1.0.0-rc.14",
|
||||||
"nitropack": "^0.6.1",
|
"nitropack": "^1.0.0-0",
|
||||||
"unbuild": "latest",
|
"unbuild": "latest",
|
||||||
"vite": "~3.2.3"
|
"vite": "~3.2.3"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"c12": "^0.2.13",
|
"c12": "^1.0.1",
|
||||||
"create-require": "^1.1.1",
|
"create-require": "^1.1.1",
|
||||||
"defu": "^6.1.0",
|
"defu": "^6.1.0",
|
||||||
"jiti": "^1.16.0",
|
"jiti": "^1.16.0",
|
||||||
"pathe": "^0.3.9",
|
"pathe": "^1.0.0",
|
||||||
"pkg-types": "^0.3.6",
|
"pkg-types": "^1.0.1",
|
||||||
"postcss-import-resolver": "^2.0.0",
|
"postcss-import-resolver": "^2.0.0",
|
||||||
"scule": "^0.3.2",
|
"scule": "^1.0.0",
|
||||||
"std-env": "^3.3.0",
|
"std-env": "^3.3.0",
|
||||||
"ufo": "^0.8.6",
|
"ufo": "^1.0.0",
|
||||||
"unimport": "^0.7.0",
|
"unimport": "^1.0.0",
|
||||||
"untyped": "^0.5.0"
|
"untyped": "^0.5.0"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
|
@ -20,8 +20,8 @@
|
|||||||
"execa": "^6.1.0",
|
"execa": "^6.1.0",
|
||||||
"get-port-please": "^2.6.1",
|
"get-port-please": "^2.6.1",
|
||||||
"jiti": "^1.16.0",
|
"jiti": "^1.16.0",
|
||||||
"ohmyfetch": "^0.4.21",
|
"ofetch": "^1.0.0",
|
||||||
"pathe": "^0.3.9"
|
"pathe": "^1.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"playwright": "^1.27.1",
|
"playwright": "^1.27.1",
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { resolve } from 'node:path'
|
import { resolve } from 'node:path'
|
||||||
import { execa } from 'execa'
|
import { execa } from 'execa'
|
||||||
import { getRandomPort, waitForPort } from 'get-port-please'
|
import { getRandomPort, waitForPort } from 'get-port-please'
|
||||||
import { fetch as _fetch, $fetch as _$fetch, FetchOptions } from 'ohmyfetch'
|
import { fetch as _fetch, $fetch as _$fetch, FetchOptions } from 'ofetch'
|
||||||
import * as _kit from '@nuxt/kit'
|
import * as _kit from '@nuxt/kit'
|
||||||
import { useTestContext } from './context'
|
import { useTestContext } from './context'
|
||||||
|
|
||||||
|
@ -30,23 +30,23 @@
|
|||||||
"esbuild": "^0.15.13",
|
"esbuild": "^0.15.13",
|
||||||
"escape-string-regexp": "^5.0.0",
|
"escape-string-regexp": "^5.0.0",
|
||||||
"estree-walker": "^3.0.1",
|
"estree-walker": "^3.0.1",
|
||||||
"externality": "^0.2.2",
|
"externality": "^1.0.0",
|
||||||
"fs-extra": "^10.1.0",
|
"fs-extra": "^10.1.0",
|
||||||
"get-port-please": "^2.6.1",
|
"get-port-please": "^2.6.1",
|
||||||
"h3": "^0.8.6",
|
"h3": "^1.0.1",
|
||||||
"knitwork": "^0.1.2",
|
"knitwork": "^1.0.0",
|
||||||
"magic-string": "^0.26.7",
|
"magic-string": "^0.26.7",
|
||||||
"mlly": "^0.5.16",
|
"mlly": "^1.0.0",
|
||||||
"ohash": "^0.1.5",
|
"ohash": "^1.0.0",
|
||||||
"pathe": "^0.3.9",
|
"pathe": "^1.0.0",
|
||||||
"perfect-debounce": "^0.1.3",
|
"perfect-debounce": "^0.1.3",
|
||||||
"pkg-types": "^0.3.6",
|
"pkg-types": "^1.0.1",
|
||||||
"postcss": "^8.4.19",
|
"postcss": "^8.4.19",
|
||||||
"postcss-import": "^15.0.0",
|
"postcss-import": "^15.0.0",
|
||||||
"postcss-url": "^10.1.3",
|
"postcss-url": "^10.1.3",
|
||||||
"rollup": "^2.79.1",
|
"rollup": "^2.79.1",
|
||||||
"rollup-plugin-visualizer": "^5.8.3",
|
"rollup-plugin-visualizer": "^5.8.3",
|
||||||
"ufo": "^0.8.6",
|
"ufo": "^1.0.0",
|
||||||
"unplugin": "^0.10.2",
|
"unplugin": "^0.10.2",
|
||||||
"vite": "~3.2.3",
|
"vite": "~3.2.3",
|
||||||
"vite-node": "^0.25.1",
|
"vite-node": "^0.25.1",
|
||||||
|
@ -112,13 +112,13 @@ export async function buildClient (ctx: ViteBuildContext) {
|
|||||||
const viteRoutes = viteServer.middlewares.stack.map(m => m.route).filter(r => r.length > 1)
|
const viteRoutes = viteServer.middlewares.stack.map(m => m.route).filter(r => r.length > 1)
|
||||||
const viteMiddleware = defineEventHandler(async (event) => {
|
const viteMiddleware = defineEventHandler(async (event) => {
|
||||||
// Workaround: vite devmiddleware modifies req.url
|
// Workaround: vite devmiddleware modifies req.url
|
||||||
const originalURL = event.req.url!
|
const originalURL = event.node.req.url!
|
||||||
if (!viteRoutes.some(route => originalURL.startsWith(route)) && !originalURL.startsWith(clientConfig.base!)) {
|
if (!viteRoutes.some(route => originalURL.startsWith(route)) && !originalURL.startsWith(clientConfig.base!)) {
|
||||||
event.req.url = joinURL('/__url', originalURL)
|
event.node.req.url = joinURL('/__url', originalURL)
|
||||||
}
|
}
|
||||||
await new Promise((resolve, reject) => {
|
await new Promise((resolve, reject) => {
|
||||||
viteServer.middlewares.handle(event.req, event.res, (err: Error) => {
|
viteServer.middlewares.handle(event.node.req, event.node.res, (err: Error) => {
|
||||||
event.req.url = originalURL
|
event.node.req.url = originalURL
|
||||||
return err ? reject(err) : resolve(null)
|
return err ? reject(err) : resolve(null)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { Agent as HTTPSAgent } from 'node:https'
|
import { Agent as HTTPSAgent } from 'node:https'
|
||||||
import { $fetch } from 'ohmyfetch'
|
import { $fetch } from 'ofetch'
|
||||||
|
|
||||||
export const viteNodeOptions = JSON.parse(process.env.NUXT_VITE_NODE_OPTIONS || '{}')
|
export const viteNodeOptions = JSON.parse(process.env.NUXT_VITE_NODE_OPTIONS || '{}')
|
||||||
|
|
||||||
|
@ -117,7 +117,7 @@ function createViteNodeApp (ctx: ViteBuildContext, invalidates: Set<string> = ne
|
|||||||
}
|
}
|
||||||
|
|
||||||
return eventHandler(async (event) => {
|
return eventHandler(async (event) => {
|
||||||
const moduleId = decodeURI(event.req.url!).substring(1)
|
const moduleId = decodeURI(event.node.req.url!).substring(1)
|
||||||
if (moduleId === '/') {
|
if (moduleId === '/') {
|
||||||
throw createError({ statusCode: 400 })
|
throw createError({ statusCode: 400 })
|
||||||
}
|
}
|
||||||
|
@ -34,9 +34,9 @@
|
|||||||
"magic-string": "^0.26.7",
|
"magic-string": "^0.26.7",
|
||||||
"memfs": "^3.4.10",
|
"memfs": "^3.4.10",
|
||||||
"mini-css-extract-plugin": "^2.6.1",
|
"mini-css-extract-plugin": "^2.6.1",
|
||||||
"mlly": "^0.5.16",
|
"mlly": "^1.0.0",
|
||||||
"ohash": "^0.1.5",
|
"ohash": "^1.0.0",
|
||||||
"pathe": "^0.3.9",
|
"pathe": "^1.0.0",
|
||||||
"pify": "^6.1.0",
|
"pify": "^6.1.0",
|
||||||
"postcss": "^8.4.19",
|
"postcss": "^8.4.19",
|
||||||
"postcss-import": "^15.0.0",
|
"postcss-import": "^15.0.0",
|
||||||
@ -44,7 +44,7 @@
|
|||||||
"postcss-url": "^10.1.3",
|
"postcss-url": "^10.1.3",
|
||||||
"style-resources-loader": "^1.5.0",
|
"style-resources-loader": "^1.5.0",
|
||||||
"time-fix-plugin": "^2.0.7",
|
"time-fix-plugin": "^2.0.7",
|
||||||
"ufo": "^0.8.6",
|
"ufo": "^1.0.0",
|
||||||
"unplugin": "^0.10.2",
|
"unplugin": "^0.10.2",
|
||||||
"url-loader": "^4.1.1",
|
"url-loader": "^4.1.1",
|
||||||
"vue-bundle-renderer": "^0.5.0",
|
"vue-bundle-renderer": "^0.5.0",
|
||||||
|
732
pnpm-lock.yaml
732
pnpm-lock.yaml
File diff suppressed because it is too large
Load Diff
@ -1,5 +1,5 @@
|
|||||||
import { execSync } from 'node:child_process'
|
import { execSync } from 'node:child_process'
|
||||||
import { $fetch } from 'ohmyfetch'
|
import { $fetch } from 'ofetch'
|
||||||
import { inc } from 'semver'
|
import { inc } from 'semver'
|
||||||
import { loadWorkspace } from './_utils'
|
import { loadWorkspace } from './_utils'
|
||||||
|
|
||||||
|
@ -9,7 +9,8 @@ import { expectNoClientErrors, renderPage, withLogs } from './utils'
|
|||||||
await setup({
|
await setup({
|
||||||
rootDir: fileURLToPath(new URL('./fixtures/basic', import.meta.url)),
|
rootDir: fileURLToPath(new URL('./fixtures/basic', import.meta.url)),
|
||||||
server: true,
|
server: true,
|
||||||
browser: true
|
browser: true,
|
||||||
|
setupTimeout: (isWindows ? 240 : 120) * 1000
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('server api', () => {
|
describe('server api', () => {
|
||||||
|
@ -62,14 +62,13 @@ describe.skipIf(isWindows)('minimal nuxt application', () => {
|
|||||||
"@vueuse/shared",
|
"@vueuse/shared",
|
||||||
"buffer-from",
|
"buffer-from",
|
||||||
"cookie-es",
|
"cookie-es",
|
||||||
"defu",
|
|
||||||
"destr",
|
"destr",
|
||||||
"estree-walker",
|
"estree-walker",
|
||||||
"h3",
|
"h3",
|
||||||
"hookable",
|
"hookable",
|
||||||
"node-fetch-native",
|
"node-fetch-native",
|
||||||
|
"ofetch",
|
||||||
"ohash",
|
"ohash",
|
||||||
"ohmyfetch",
|
|
||||||
"pathe",
|
"pathe",
|
||||||
"radix3",
|
"radix3",
|
||||||
"scule",
|
"scule",
|
||||||
|
4
test/fixtures/basic/types.ts
vendored
4
test/fixtures/basic/types.ts
vendored
@ -3,12 +3,12 @@ import { describe, it } from 'vitest'
|
|||||||
import type { Ref } from 'vue'
|
import type { Ref } from 'vue'
|
||||||
import type { AppConfig } from '@nuxt/schema'
|
import type { AppConfig } from '@nuxt/schema'
|
||||||
|
|
||||||
import type { FetchError } from 'ohmyfetch'
|
import type { FetchError } from 'ofetch'
|
||||||
import { NavigationFailure, RouteLocationNormalizedLoaded, RouteLocationRaw, useRouter as vueUseRouter } from 'vue-router'
|
import { NavigationFailure, RouteLocationNormalizedLoaded, RouteLocationRaw, useRouter as vueUseRouter } from 'vue-router'
|
||||||
import { defineNuxtConfig } from '~~/../../../packages/nuxt/config'
|
|
||||||
import type { NavigateToOptions } from '~~/../../../packages/nuxt/dist/app/composables/router'
|
import type { NavigateToOptions } from '~~/../../../packages/nuxt/dist/app/composables/router'
|
||||||
// eslint-disable-next-line import/order
|
// eslint-disable-next-line import/order
|
||||||
import { isVue3 } from '#app'
|
import { isVue3 } from '#app'
|
||||||
|
import { defineNuxtConfig } from '~~/../../../packages/nuxt/config'
|
||||||
import { useRouter } from '#imports'
|
import { useRouter } from '#imports'
|
||||||
|
|
||||||
interface TestResponse { message: string }
|
interface TestResponse { message: string }
|
||||||
|
Loading…
Reference in New Issue
Block a user