npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dreamonkey/vue-routes-flattener

v1.0.0

Published

A package that let you use grouping routes, logical routes without 'component' property meant to reduce repetition in their children paths

Downloads

94

Readme

vue-routes-flattener

This package is meant to be used with Vue Router and based on this issue. It allows usage of grouping routes, logical routes without 'component' property meant to reduce repetition in their children paths. This feature is purely additive and doesn't affect standard vue-router behaviour.

Install

yarn add @dreamonkey/vue-routes-flattener

Use case

Suppose you have a "MainLayout" component, containing a router outlet which displays these pages:

  • MainLayout
    • Home -> '/home'
    • NewYork -> '/locations/new-york'
    • LosAngeles -> '/locations/los-angeles'
    • Italy -> '/locations/italy'
    • Contacts -> '/contacts

Also suppose you don't need a '/location' page containing a router outlet. Your routes configuration will be:

// src/router/routes.ts

import { RouteConfig } from "vue-router";

const routes: RouteConfig[] = [
  {
    path: "/",
    component: () => import("layouts/main-layout.vue"),
    children: [
      { path: "home", component: () => import("pages/home.vue") },
      {
        path: "locations/new-york", // <-- "locations" repetition
        component: () => import("pages/new-york.vue"),
      },
      {
        path: "locations/los-angeles", // <-- "locations" repetition
        component: () => import("pages/los-angeles.vue"),
      },
      {
        path: "locations/italy", // <-- "locations" repetition
        component: () => import("pages/italy.vue"),
      },
      {
        path: "contacts",
        component: () => import("pages/contacts.vue"),
      },
    ],
  },
];

export default routes;

Usage

Using this package you can reduce repetitions in code, while obtaining the same configuration at runtime:

// src/router/routes.ts

import { RouteConfig } from "vue-router";
import { flatRoutes } from "@dreamonkey/vue-routes-flattener"; // <-- Import from the package

const routes: RouteConfig[] = flatRoutes([
  // Apply the flattening === ^^^^^^^^^^
  {
    path: "/",
    component: () => import("layouts/main-layout.vue"),
    children: [
      { path: "home", component: () => import("pages/home.vue") },
      {
        path: "locations", // <-- Grouping route, no 'component' is defined
        children: [
          {
            path: "new-york", // <-- No repetition into child path
            component: () => import("pages/new-york.vue"),
          },
          {
            path: "los-angeles", // <-- No repetition into child path
            component: () => import("pages/los-angeles.vue"),
          },
          {
            path: "italy", // <-- No repetition into child path
            component: () => import("pages/italy.vue"),
          },
        ],
      },
      {
        path: "contacts",
        component: () => import("pages/contacts.vue"),
      },
    ],
  },
]);

export default routes;

If you forget to apply flatRoutes, Vue Router will throw an error because routes without 'component' property are not allowed.

Why this feature is not natively available

This feature is not natively available due to some concerns exposed by Evan You.

I think this breaks the relationship between route config nesting and router-view nesting and can make things a bit harder to reason about.

URLs/paths are based on the filesystem, with its directories and files. Directories are the entry point of a new nesting level, while files are actual content.

If you think about our example configuration into as a filesystem structure (D = Directory, f = File) you'll probably get something like:

-MainLayout(D)
|--Home(f)
|--Locations(D)
  |--NewYork(f)
  |--LosAngeles(f)
  |--Italy(f)
|--Contacts(f)

Vue Router configuration is based on router outlets instead. A router outlet open a new nesting level and is hosted into a component which could add content. In our comparison, the hosting component is both a directory and a file, and this can lead to confusing results in some edge cases.

The same example, using router outlets (O) as directories, would be:

-MainLayout(O)
|--Home(f)
|--Locations(O)
  |--NewYork(f)
  |--LosAngeles(f)
  |--Italy(f)
|--Contacts(f)

but this would require you to create a dummy component containing only a router outlet. If you also need a Locations page, for example an index with links to other Location-related pages, you would need to either have the dummy component dynamically change its template based on the current route or get rid of the router outlet altogether, flat out all routes and put there the new index component.

The former option increases the complexity of a component which should not be there in the first place, while the latter will generate repetition and lead to a de-sync between the URL structure (as you mentally imagine it when thinking about directories) and the configuration one.

-Main-layout(O)
|--Home(f)
|--Locations(f)
|--Locations-NewYork(f)
|--Locations-LosAngeles(f)
|--Locations-Italy(f)
|--Contacts(f)

This package allows you to use "virtual" router outlets (aka grouping routes) which help you keep the configuration in sync with your directory-based mental model, then flattens the configuration at runtime. You can also provide an "index" component (Locations page in our previous example) by specifying a child of the grouping route with an empty path (""):

// src/router/routes.ts

import { RouteConfig } from "vue-router";
import { flatRoutes } from "@dreamonkey/vue-routes-flattener";

const routes: RouteConfig[] = flatRoutes([
  {
    path: "/",
    component: () => import("layouts/main-layout.vue"),
    children: [
      { path: "home", component: () => import("pages/home.vue") },
      {
        path: "locations", // <-- grouping route / "virtual" router outlet
        children: [
          { path: "", component: () => import("pages/locations.vue") }, // <-- "index" component
          {
            path: "new-york",
            component: () => import("pages/new-york.vue"),
          },
          {
            path: "los-angeles",
            component: () => import("pages/los-angeles.vue"),
          },
          {
            path: "italy",
            component: () => import("pages/italy.vue"),
          },
        ],
      },
      {
        path: "contacts",
        component: () => import("pages/contacts.vue"),
      },
    ],
  },
]);

export default routes;

License

MIT