From b7d9d17867f2c57c56b811620ecbcdcc27327b0f Mon Sep 17 00:00:00 2001 From: SerKo Date: Sat, 20 Apr 2024 17:16:37 +0800 Subject: [PATCH 1/2] feat!: change index fallback behavior --- .../pages/@left/{~default.vue => ~index.vue} | 2 +- .../parallel-sidebar/pages/@main/~index.vue | 5 +++++ src/runtime/parallel-router.ts | 19 ++++++++++++------- 3 files changed, 18 insertions(+), 8 deletions(-) rename examples/parallel-sidebar/pages/@left/{~default.vue => ~index.vue} (75%) create mode 100644 examples/parallel-sidebar/pages/@main/~index.vue diff --git a/examples/parallel-sidebar/pages/@left/~default.vue b/examples/parallel-sidebar/pages/@left/~index.vue similarity index 75% rename from examples/parallel-sidebar/pages/@left/~default.vue rename to examples/parallel-sidebar/pages/@left/~index.vue index 8c43a3c..f2deb06 100644 --- a/examples/parallel-sidebar/pages/@left/~default.vue +++ b/examples/parallel-sidebar/pages/@left/~index.vue @@ -1,5 +1,5 @@ diff --git a/examples/parallel-sidebar/pages/@main/~index.vue b/examples/parallel-sidebar/pages/@main/~index.vue new file mode 100644 index 0000000..dae4992 --- /dev/null +++ b/examples/parallel-sidebar/pages/@main/~index.vue @@ -0,0 +1,5 @@ + diff --git a/src/runtime/parallel-router.ts b/src/runtime/parallel-router.ts index 4962582..9162d24 100644 --- a/src/runtime/parallel-router.ts +++ b/src/runtime/parallel-router.ts @@ -77,6 +77,7 @@ export default defineNuxtPlugin(async () => { async function createParallelRouter(name: string, routes: RouteRecord[], router: Router, parallelPageOptions: Partial): Promise { const options = defu(parallelPageOptions, { mode: 'sync', + index: '/~index', fallback: true, } satisfies ParallelPageOptions) @@ -136,19 +137,23 @@ async function createParallelRouter(name: string, routes: RouteRecord[], router: } async function init() { + async function tryIndex() { + const pushIndex = options.index && tryPush(options.index) + if (pushIndex) { + await pushIndex + } else { + fallback.index = true + } + } + if (options.mode === 'manual') { - if (options.index) - await parallelRouter.push(options.index) + await tryIndex() } else { const initSync = sync() if (initSync) await initSync else { - if (options.index) { - await parallelRouter.push(options.index) - } else { - fallback.index = true - } + await tryIndex() } } } From eabc5fcee7ce7523a8b380c30e88469e62dbaa2b Mon Sep 17 00:00:00 2001 From: SerKo Date: Sun, 21 Apr 2024 13:43:40 +0800 Subject: [PATCH 2/2] docs(example): update docs for index fallback --- .../1.getting-started/3.configuration.md | 2 +- docs/content/2.routing/1.parallel-routes.md | 12 +++++----- docs/layouts/parallel.vue | 20 +--------------- docs/nuxt.config.ts | 10 ++++++++ .../examples/parallel-routes/@left/~index.vue | 23 +++++++++++++++++++ src/runtime/types.ts | 2 +- 6 files changed, 42 insertions(+), 27 deletions(-) create mode 100644 docs/pages/examples/parallel-routes/@left/~index.vue diff --git a/docs/content/1.getting-started/3.configuration.md b/docs/content/1.getting-started/3.configuration.md index 035ecfa..347edfa 100644 --- a/docs/content/1.getting-started/3.configuration.md +++ b/docs/content/1.getting-started/3.configuration.md @@ -48,7 +48,7 @@ The sync mode of the parallel route. Please read the [Parallel Routes > Sync Mod ##### `index` - Type: `string | undefined` -- Default: `undefined` +- Default: `/~index` The index page of the parallel route. Please read the [Parallel Routes > Fallback Mechanism](/routing/parallel-routes#fallback-mechanism) for more information. diff --git a/docs/content/2.routing/1.parallel-routes.md b/docs/content/2.routing/1.parallel-routes.md index 2b13d1c..08ab692 100644 --- a/docs/content/2.routing/1.parallel-routes.md +++ b/docs/content/2.routing/1.parallel-routes.md @@ -164,17 +164,17 @@ Here’s the fallback sequence of operations that occurs during navigation: **On initial page load:** 1. Navigate if the current URL matches any routes in `~/pages`. -2. Otherwise, redirect to `fallback.redirect` if it has been set in config. -3. Otherwise, redirect to `index` if it has been set in config. -4. Otherwise, render the `#not-found` slot if it has been set in `PlusParallelPage`. -5. Otherwise, render the `#index` slot if it has been set in `PlusParallelPage`. +2. Otherwise, try redirecting to [`fallback.redirect`](/getting-started/configuration#fallback) if it has been set in config. +4. Otherwise, try rendering the `#not-found` slot if it has been set in [`PlusParallelPage`](/components/plus-parallel-page). +3. Otherwise, try redirecting to `/~index` (or [`index`](/getting-started/configuration#index) set in config) if it can be resolved. +5. Otherwise, try rendering the `#index` slot if it has been set in [`PlusParallelPage`](/components/plus-parallel-page). 6. If none of the above conditions are met, leave the view empty. **On route navigate:** 1. Navigate if the target URL matches any routes in `~/pages`. -2. Otherwise, redirect to `fallback.redirect` if it has been set in config. -3. Otherwise, render the `#not-found` slot if it has been set in `PlusParallelPage`. +2. Otherwise, try redirecting to [`fallback.redirect`](/getting-started/configuration#fallback) if it has been set in config. +3. Otherwise, try rendering the `#not-found` slot if it has been set in [`PlusParallelPage`](/components/plus-parallel-page). 4. If none of the above conditions are met, keep the last successful view. ::alert{type="info"} diff --git a/docs/layouts/parallel.vue b/docs/layouts/parallel.vue index 641ec3e..60b13eb 100644 --- a/docs/layouts/parallel.vue +++ b/docs/layouts/parallel.vue @@ -29,25 +29,7 @@ const relativePath = computed(() => { - - - + diff --git a/docs/nuxt.config.ts b/docs/nuxt.config.ts index 3e9a35d..1e6face 100755 --- a/docs/nuxt.config.ts +++ b/docs/nuxt.config.ts @@ -30,4 +30,14 @@ export default defineNuxtConfig({ pagesPath: { basedPath: /examples\/[\w-]+\//, }, + + pagesPlus: { + parallelPages: { + left: { + // this config is for demo in docs only + // the index default value is '/~index' so usually you don't need to set it in your project + index: '/examples/parallel-routes/~index', + }, + }, + }, }) diff --git a/docs/pages/examples/parallel-routes/@left/~index.vue b/docs/pages/examples/parallel-routes/@left/~index.vue new file mode 100644 index 0000000..599c02d --- /dev/null +++ b/docs/pages/examples/parallel-routes/@left/~index.vue @@ -0,0 +1,23 @@ + + + diff --git a/src/runtime/types.ts b/src/runtime/types.ts index f7bab69..7a5d5d3 100644 --- a/src/runtime/types.ts +++ b/src/runtime/types.ts @@ -2,7 +2,7 @@ export interface ParallelPageOptions { // default: 'sync' mode: 'sync' | 'sync-once' | 'manual' - // default: undefined + // default: '/~index' index?: string // default: true