Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
Merge branch 'main' into feat/nuxi-env-path
Browse files Browse the repository at this point in the history
  • Loading branch information
cpreston321 committed Sep 25, 2022
2 parents d0c7e92 + 71ef8bd commit 4cbea56
Show file tree
Hide file tree
Showing 22 changed files with 159 additions and 56 deletions.
28 changes: 0 additions & 28 deletions docs/content/1.getting-started/10.upgrade-guide/0.migration.md

This file was deleted.

59 changes: 59 additions & 0 deletions docs/content/1.getting-started/10.upgrade.md
@@ -0,0 +1,59 @@
# Upgrade Guide

Have a Nuxt 2 project to migrate? Use these guides to upgrade your Nuxt applications to Nuxt 3 or take the first step in that direction with Nuxt Bridge.

If you are already using Nuxt 3 and want to upgrade to the latest release or test new features before their release, head over to the [Upgrading Nuxt 3](#upgrading-nuxt-3) section.

## Feature Comparison

In the table below, there is a quick comparison between 3 versions of Nuxt:

Feature / Version | Nuxt 2 | Nuxt Bridge | Nuxt 3
-------------------------|-----------------|------------------|---------
Vue | 2 | 2 | 3
Stability | 😊 Stable | 😌 Semi-stable | Release candidate
Performance | 🏎 Fast | ✈️ Faster | 🚀 Fastest
Nitro Engine | ❌ | ✅ | ✅
ESM support | 🌙 Partial | 👍 Better | ✅
TypeScript | ☑️ Opt-in | 🚧 Partial | ✅
Composition API | ❌ | 🚧 Partial | ✅
Options API | ✅ | ✅ | ✅
Components Auto Import | ✅ | ✅ | ✅
`<script setup>` syntax | ❌ | 🚧 Partial | ✅
Auto Imports | ❌ | ✅ | ✅
Webpack | 4 | 4 | 5
Vite | ⚠️ Partial | 🚧 Partial | ✅
Nuxi CLI | ❌ Old | ✅ nuxi | ✅ nuxi
Static sites | ✅ | ✅ | ✅

## Nuxt 2 to Nuxt 3

The migration guide provides a step-by-step comparison of Nuxt 2 features to Nuxt 3 features and guidance to adapt your current application.

::alert{type=info icon=👉}
[**Migrate from From Nuxt 2 to Nuxt 3**](/migration/overview)
::

## Nuxt 2 to Nuxt Bridge

If you prefer to progressively migrate your Nuxt 2 application to Nuxt 3, you can use Nuxt Bridge. Nuxt Bridge is a compatibility layer that allows you to use Nuxt 3 features in Nuxt 2 with an opt-in mechanism.

::alert{type=info icon=👉}
[**Migrate from Nuxt 2 to Nuxt Bridge**](/bridge/overview)
::

## Upgrading Nuxt 3

### Latest release

To upgrade Nuxt 3 to the latest release, use the `nuxi upgrade` command.

```bash
npx nuxi upgrade
```

### Edge release channel

::alert{type=info icon=👉}
To use the latest Nuxt 3 build and test features before their release, read the [edge release channel](/guide/going-further/edge-channel) guide.
::
2 changes: 1 addition & 1 deletion docs/content/1.getting-started/8.error-handling.md
Expand Up @@ -73,7 +73,7 @@ const handleError = () => clearError({ redirect: '/' })

### `useError`

* `function useError (): Ref<any>`
* `function useError (): Ref<Error | { url, statusCode, statusMessage, message, description, data }>`

This function will return the global Nuxt error that is being handled.

Expand Down
2 changes: 1 addition & 1 deletion docs/content/2.guide/2.directory-structure/1.server.md
Expand Up @@ -105,7 +105,7 @@ Given the example above, fetching `/test` with:

- **GET** method: Returns `Test get handler`
- **POST** method: Returns `Test post handler`
- Any other method: Returns 404 error
- Any other method: Returns 405 error

### Catch-all Route

Expand Down
22 changes: 11 additions & 11 deletions docs/content/3.api/3.utils/navigate-to.md
Expand Up @@ -71,13 +71,13 @@ An object accepting the following properties:
```vue
<script setup>
// passing 'to' as a string
return navigateTo('/search')
await navigateTo('/search')
// ... or as a route object
return navigateTo({ path: '/search' })
await navigateTo({ path: '/search' })
// ... or as a route object with query parameters
return navigateTo({
await navigateTo({
path: '/search',
query: {
page: 1,
Expand All @@ -103,13 +103,13 @@ export default defineNuxtRouteMiddleware((to, from) => {

```vue
<script setup>
// will throw an error;
// navigating to an external URL is not allowed by default
await navigateTo('https://v3.nuxtjs.org')
// will redirect successfully with the 'external' parameter set to 'true'
await navigateTo('https://v3.nuxtjs.org', {
external: true
})
// will throw an error;
// navigating to an external URL is not allowed by default
await navigateTo('https://v3.nuxtjs.org')
// will redirect successfully with the 'external' parameter set to 'true'
await navigateTo('https://v3.nuxtjs.org', {
external: true
})
</script>
```
5 changes: 3 additions & 2 deletions docs/static/_redirects
Expand Up @@ -40,8 +40,9 @@
/guide/features/state-management /getting-started/state-management 302!
/guide/features/error-handling /getting-started/error-handling 302!
/guide/going-further/testing /getting-started/testing 302!
/getting-started/migration /getting-started/upgrade-guide/migration 302!
/guide/going-further/edge-channel /getting-started/upgrade-guide/edge-channel 302!
/getting-started/migration /getting-started/upgrade-guide/migration 302
/getting-started/upgrade-guide/migration /getting-started/upgrade 302!
/getting-started/upgrade-guide/edge-channel /guide/going-further/edge-channel 302!
/guide/features/modules /guide/concepts/modules 302!
/guide/features/server-routes /guide/directory-structure/server 302!
/guide/going-further/esm /guide/concepts/esm 302!
Expand Down
5 changes: 4 additions & 1 deletion packages/nuxi/src/commands/info.ts
Expand Up @@ -119,7 +119,10 @@ function normalizeConfigModule (module: NuxtModule | string | null | undefined,

function getNuxtConfig (rootDir: string) {
try {
return jiti(rootDir, { interopDefault: true, esmResolve: true })('./nuxt.config')
(globalThis as any).defineNuxtConfig = (c: any) => c
const result = jiti(rootDir, { interopDefault: true, esmResolve: true })('./nuxt.config')
delete (globalThis as any).defineNuxtConfig
return result
} catch (err) {
// TODO: Show error as warning if it is not 404
return {}
Expand Down
3 changes: 2 additions & 1 deletion packages/nuxi/src/utils/prepare.ts
Expand Up @@ -27,7 +27,8 @@ export const writeTypes = async (nuxt: Nuxt) => {
include: [
'./nuxt.d.ts',
join(relative(nuxt.options.buildDir, nuxt.options.rootDir), '**/*'),
...nuxt.options.srcDir !== nuxt.options.rootDir ? [join(relative(nuxt.options.buildDir, nuxt.options.srcDir), '**/*')] : []
...nuxt.options.srcDir !== nuxt.options.rootDir ? [join(relative(nuxt.options.buildDir, nuxt.options.srcDir), '**/*')] : [],
...nuxt.options.workspaceDir !== nuxt.options.rootDir ? [join(relative(nuxt.options.buildDir, nuxt.options.workspaceDir), '**/*')] : []
]
})

Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/package.json
Expand Up @@ -56,7 +56,7 @@
"hash-sum": "^2.0.0",
"hookable": "^5.3.0",
"knitwork": "^0.1.2",
"magic-string": "^0.26.3",
"magic-string": "^0.26.4",
"mlly": "^0.5.16",
"nitropack": "^0.5.4",
"nuxi": "3.0.0-rc.11",
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/src/app/components/layout.ts
Expand Up @@ -21,7 +21,7 @@ export default defineComponent({
if (process.dev && process.client) {
onMounted(() => {
nextTick(() => {
if (_layout && ['#comment', '#text'].includes(vnode?.el?.nodeName)) {
if (_layout && _layout in layouts && ['#comment', '#text'].includes(vnode?.el?.nodeName)) {
console.warn(`[nuxt] \`${_layout}\` layout does not have a single root node and will cause errors when navigating between routes.`)
}
})
Expand Down
2 changes: 2 additions & 0 deletions packages/nuxt/src/app/composables/fetch.ts
Expand Up @@ -63,6 +63,7 @@ export function useFetch<
pick,
watch,
initialCache,
immediate,
...fetchOptions
} = opts

Expand All @@ -78,6 +79,7 @@ export function useFetch<
transform,
pick,
initialCache,
immediate,
watch: [
_request,
...(watch || [])
Expand Down
14 changes: 13 additions & 1 deletion packages/nuxt/src/pages/runtime/composables.ts
@@ -1,8 +1,20 @@
import { KeepAliveProps, TransitionProps, UnwrapRef } from 'vue'
import type { RouteLocationNormalizedLoaded } from 'vue-router'
import type { RouteLocationNormalizedLoaded, RouteRecordRedirectOption } from 'vue-router'

export interface PageMeta {
[key: string]: any
/**
* Where to redirect if the route is directly matched. The redirection happens
* before any navigation guard and triggers a new navigation with the new
* target location.
*/
redirect?: RouteRecordRedirectOption
/**
* Aliases for the record. Allows defining extra paths that will behave like a
* copy of the record. Allows having paths shorthands like `/users/:id` and
* `/u/:id`. All `alias` and `path` values must share the same params.
*/
alias?: string | string[]
pageTransition?: boolean | TransitionProps
layoutTransition?: boolean | TransitionProps
key?: false | string | ((route: RouteLocationNormalizedLoaded) => string)
Expand Down
1 change: 1 addition & 0 deletions packages/nuxt/src/pages/utils.ts
Expand Up @@ -243,6 +243,7 @@ export function normalizeRoutes (routes: NuxtPage[], metaImports: Set<string> =
children: route.children ? normalizeRoutes(route.children, metaImports).routes : [],
meta: route.meta ? `{...(${metaImportName} || {}), ...${JSON.stringify(route.meta)}}` : metaImportName,
alias: aliasCode,
redirect: route.redirect ? JSON.stringify(route.redirect) : `${metaImportName}?.redirect || undefined`,
component: genDynamicImport(file, { interopDefault: true })
}
}))
Expand Down
3 changes: 2 additions & 1 deletion packages/schema/src/types/hooks.ts
Expand Up @@ -43,7 +43,8 @@ export type NuxtPage = {
path: string
file: string
meta?: Record<string, any>
alias?: string[]
alias?: string[] | string
redirect?: string
children?: NuxtPage[]
}

Expand Down
4 changes: 2 additions & 2 deletions packages/vite/package.json
Expand Up @@ -35,7 +35,7 @@
"get-port-please": "^2.6.1",
"h3": "^0.7.21",
"knitwork": "^0.1.2",
"magic-string": "^0.26.3",
"magic-string": "^0.26.4",
"mlly": "^0.5.16",
"ohash": "^0.1.5",
"pathe": "^0.3.8",
Expand All @@ -44,7 +44,7 @@
"postcss": "^8.4.16",
"postcss-import": "^15.0.0",
"postcss-url": "^10.1.3",
"rollup": "^2.79.0",
"rollup": "^2.79.1",
"rollup-plugin-visualizer": "^5.8.1",
"ufo": "^0.8.5",
"unplugin": "^0.9.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack/package.json
Expand Up @@ -31,7 +31,7 @@
"fs-extra": "^10.1.0",
"hash-sum": "^2.0.0",
"lodash-es": "^4.17.21",
"magic-string": "^0.26.3",
"magic-string": "^0.26.4",
"memfs": "^3.4.7",
"mini-css-extract-plugin": "^2.6.1",
"mlly": "^0.5.16",
Expand Down
10 changes: 10 additions & 0 deletions test/basic.test.ts
Expand Up @@ -55,6 +55,16 @@ describe('pages', () => {
await expectNoClientErrors('/')
})

it('respects aliases in page metadata', async () => {
const html = await $fetch('/some-alias')
expect(html).toContain('Hello Nuxt 3!')
})

it('respects redirects in page metadata', async () => {
const { headers } = await fetch('/redirect', { redirect: 'manual' })
expect(headers.get('location')).toEqual('/')
})

it('render 404', async () => {
const html = await $fetch('/not-found')

Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/basic/nuxt.config.ts
Expand Up @@ -53,6 +53,9 @@ export default defineNuxtConfig({
}
],
hooks: {
'prepare:types' ({ tsConfig }) {
tsConfig.include = tsConfig.include.filter(i => i !== '../../../../**/*')
},
'modules:done' () {
addComponent({
name: 'CustomComponent',
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/basic/pages/index.vue
Expand Up @@ -28,6 +28,10 @@ setupDevtoolsPlugin({}, () => {})
const config = useRuntimeConfig()
definePageMeta({
alias: '/some-alias'
})
// reset title template example
useHead({
titleTemplate: ''
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/basic/pages/redirect.vue
@@ -0,0 +1,11 @@
<script setup>
definePageMeta({
redirect: () => '/'
})
</script>

<template>
<div>
<div>redirect.vue</div>
</div>
</template>

0 comments on commit 4cbea56

Please sign in to comment.