3.40.28 • Published 3 days ago

@tramvai/module-router v3.40.28

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
3 days ago

@tramvai/module-router

Module for routing in the application. Exports two sub-modules: with client SPA transitions, and no-SPA.

Installation

You need to install @tramvai/module-router:

yarn add @tramvai/module-router

And connect in the project:

import { createApp } from '@tramvai/core';
import { NoSpaRouterModule, SpaRouterModule } from '@tramvai/module-router';

createApp({
  name: 'tincoin',
  modules: [SpaRouterModule],
  // modules: [ NoSpaRouterModule ], if you want to disable client SPA transitions
});

Explanation

The module is based on the library @tinkoff/router

Default Settings

Next settings are used

  • trailingSlashes = true
  • mergeSlashes = true

Navigation flow on the server

Diagram

Flow of the first navigation on the client

Diagram

Flow of navigation on the client without SPA transitions

Diagram

Flow of navigation on the client with SPA transitions

Diagram

API

Static routes in the application

Route description format:

const routes = [
  {
    // the name of the route is required
    name: 'route1',
    // the path of the route is required
    path: '/route/a/',
    // additional configs for the route
    config: {
      // layout component name
      layoutComponent: 'layout',
      // page component name
      pageComponent: 'page',
    },
  },
];

You can explicitly transfer a list of routes to routing when adding a router module:

import { createApp } from '@tramvai/core';
import { SpaRouterModule } from '@tramvai/module-router';

const routes = [
  // ...
];

createApp({
  modules: [
    // ...,
    SpaRouterModule.forRoot(routes),
  ],
});

Or separately with the ROUTES_TOKEN token (you can set it several times):

import { ROUTES_TOKEN } from '@tramvai/module-router';
import { provide } from '@tramvai/core';

const routesCommon = [
  // ...
];
const routesSpecific = [
  // ...
];

const providers = [
  // ...,
  provide({
    provide: ROUTES_TOKEN,
    multi: true,
    useValue: routesCommon,
  }),
  provide({
    provide: ROUTES_TOKEN,
    multi: true,
    useValue: routesSpecific,
  }),
];

PAGE_SERVICE_TOKEN

Service wrapper for working with routing. Serves to hide routing work and is the preferred way of routing work.

Methods:

  • getCurrentRoute() - get the current route
  • getCurrentUrl() - object-result of parsing the current url
  • getConfig() - get the config of the current page
  • getContent() - get content for the current page
  • getMeta() - get the meta for the current page
  • navigate(options) - navigation to a new page more
  • updateCurrentRoute(options) - update the current route with new parameters more
  • back() - go back through history
  • forward() - go forward through history
  • go(to) - go to the specified delta by history
  • addComponent(name, component) - add new component to current page into ComponentRegistry
  • getComponent(name) - get component from current page components from ComponentRegistry

RouterStore

Store that stores information about the current and previous routes.

Properties:

  • currentRoute - current route
  • currentUrl - current url
  • previousRoute - previous route
  • previousUrl - previous url

ROUTER_GUARD_TOKEN

Allows you to block or redirect the transition to the page under certain conditions. See @tinkoff/router

Redirects

Redirects can be done via guards or explicitly via the redirect property in the route.

const routes = [
  // ...,
  {
    name: 'redirect',
    path: '/from/',
    redirect: '/to/',
  },
];

Not Found route

The route used if no matches were found for the current page, can be specified in a special way in the list of routes.

const route = [
  // ...other routes,
  {
    name: 'not-found',
    path: '*',
    config: {
      pageComponent: 'notfoundComponentName',
    },
  },
];

ROUTE_RESOLVE_TOKEN

Allows you to define an asynchronous function that returns a route object that will be called if no suitable static route was found in the application.

ROUTE_TRANSFORM_TOKEN

Transformer function for application routes (set statically and those that will be loaded via ROUTE_RESOLVE_TOKEN)

Method of setting when actions should be performed during SPA transitions

By default, SPA transitions execute actions after defining the next route, but before the actual transition, which allows the page to be displayed immediately with new data, but can cause a noticeable visual lag if the actions are taken long enough.

It is possible to change the behavior and make the execution of actions after the transition itself. Then, when developing components, you will need to take into account that data will be loaded as it becomes available.

Configurable explicitly when using the routing module:

import { createApp } from '@tramvai/core';
import { SpaRouterModule } from '@tramvai/module-router';

createApp({
  modules: [
    // ...,
    SpaRouterModule.forRoot([], {
      spaActionsMode: 'after', // default is 'before'
    }),
  ],
});

or through token ROUTER_SPA_ACTIONS_RUN_MODE_TOKEN:

import { ROUTER_SPA_ACTIONS_RUN_MODE_TOKEN } from '@tramvai/module-router';
import { provide } from '@tramvai/core';

const providers = [
  // ...,
  provide({
    provide: ROUTER_SPA_ACTIONS_RUN_MODE_TOKEN,
    useValue: 'after',
  }),
];

How to

Working with navigation in providers and actions

In this case, it is best to use the PAGE_SERVICE_TOKEN

import { provide, createAction } from '@tramvai/core';
import { PAGE_SERVICE_TOKEN } from '@tramvai/module-router';

const provider = provide({
  provide: 'token',
  useFactory: ({ pageService }) => {
    if (pageService().getCurrentUrl().pathname === '/test/') {
      return pageService.navigate({ url: '/redirect/', replace: true });
    }
  },
  deps: {
    pageService: PAGE_SERVICE_TOKEN,
  },
});

const action = createAction({
  name: 'action',
  fn: (_, __, { pageService }) => {
    if (pageService.getConfig().pageComponent === 'pageComponent') {
      return page.updateCurrentRoute({ query: { test: 'true' } });
    }
  },
  deps: {
    pageService: PAGE_SERVICE_TOKEN,
  },
});

Working with navigation in React components

You can work with routing inside React components using hooks and components - useNavigate, useRoute, Link from the @tinkoff/router

@inline ../../../examples/how-to/router-navigate/index.tsx

How to set static routes

RouterModule allows you to add new routes when configuring your application. The second way is to pass static routes to DI via the ROUTES_TOKEN token.

@inline ../../../examples/how-to/router-static-routes/index.tsx

How to set Route Guard

ROUTER_GUARD_TOKEN is set as an asynchronous function, which allows you to perform various actions and influence the routing behavior.

@inline ../../../examples/how-to/router-guards/index.tsx

How to add transition hooks

Transition hooks allows to subscribe on different steps of the transition

  1. Get router instance with ROUTER_TOKEN token
  2. Use methods registerHook, registerSyncHook to add new hooks to the router
  3. Registration should happen as soon as possible so appropriate line is customerStart as it executes before navigation happens.

How to set the Not found route

The Not found route is used if the corresponding route is not found for the url.

Such a route is specified in the list of routes with the special * character in the path property.

@inline ../../../examples/how-to/router-not-found/index.tsx

How to change Not found route response status

By default, responses for the Not found route return a status of 200. You can change status in custom Route Guard, by using RESPONSE_MANAGER_TOKEN.

@inline ../../../examples/how-to/router-not-found-custom-status/index.tsx

How to change response status in actions

For example, you make a important request in action, and if this request will fail, application need to return 500 or 404 status.

Page actions running after router navigation flow, when route is completely resolved. You can change status by using RESPONSE_MANAGER_TOKEN. If you want to prevent page component rendering, you can throw NotFoundError from @tinkoff/errors library.

@inline ../../../examples/how-to/router-action-error/index.tsx

Testing

Testing ROUTER_GUARD_TOKEN extensions

If you have a module or providers that define ROUTER_GUARD_TOKEN, then it will be convenient to use special utilities to test them separately

import { ROUTER_GUARD_TOKEN } from '@tramvai/tokens-router';
import { testGuard } from '@tramvai/module-router/tests';
import { CustomModule } from './module';
import { providers } from './providers';

describe('router guards', () => {
  it('should redirect from guard', async () => {
    const { router } = testGuard({
      providers,
    });

    await router.navigate('/test/');

    expect(router.getCurrentUrl()).toMatchObject({
      path: '/redirect/',
    });
  });

  it('should block navigation', async () => {
    const { router } = testGuard({
      modules: [CustomModule],
    });

    expect(router.getCurrentUrl()).toMatchObject({ path: '/' });

    await router.navigate('/test/').catch(() => null);

    expect(router.getCurrentUrl()).toMatchObject({
      path: '/',
    });
  });
});

Exported tokens

link

4.9.1

3 days ago

4.9.0

9 days ago

3.40.28

14 days ago

4.8.6

14 days ago

3.40.26

15 days ago

3.40.25

15 days ago

4.8.4

15 days ago

4.8.3

15 days ago

3.40.21

18 days ago

4.7.1

18 days ago

3.40.20

19 days ago

4.7.0

19 days ago

4.4.6

23 days ago

3.40.17

23 days ago

4.4.5

24 days ago

4.4.4

24 days ago

3.40.16

24 days ago

4.4.1

26 days ago

4.4.3

26 days ago

3.40.14

26 days ago

3.40.13

26 days ago

4.3.1

26 days ago

3.40.11

26 days ago

4.3.0

29 days ago

3.40.6

1 month ago

3.40.7

1 month ago

4.2.1

1 month ago

4.2.0

1 month ago

4.1.0

1 month ago

4.0.6

1 month ago

3.40.5

1 month ago

4.0.5

1 month ago

4.0.4

1 month ago

3.40.4

1 month ago

3.38.0

2 months ago

3.37.10

2 months ago

3.37.8

2 months ago

3.37.7

2 months ago

3.37.5

2 months ago

3.37.1

3 months ago

3.34.0

3 months ago

3.33.2

3 months ago

3.33.0

3 months ago

3.32.3

3 months ago

3.31.0

4 months ago

3.31.2

4 months ago

3.31.3

4 months ago

3.30.1

4 months ago

3.27.5

4 months ago

3.27.3

4 months ago

3.27.4

4 months ago

3.27.0

4 months ago

3.26.3

4 months ago

3.26.2

4 months ago

3.25.5

4 months ago

3.25.6

4 months ago

3.25.3

4 months ago

3.25.2

4 months ago

3.24.3

5 months ago

3.25.0

5 months ago

3.24.1

5 months ago

3.24.0

5 months ago

3.23.0

5 months ago

3.21.0

5 months ago

3.19.0

5 months ago

3.19.1

5 months ago

3.17.0

5 months ago

2.160.25

6 months ago

3.15.1

5 months ago

3.16.0

5 months ago

3.12.0

5 months ago

3.14.1

5 months ago

3.14.0

5 months ago

3.13.0

5 months ago

3.10.2

6 months ago

3.11.0

6 months ago

2.139.3

8 months ago

2.139.2

8 months ago

3.2.0

7 months ago

2.149.0

8 months ago

2.149.1

8 months ago

2.160.21

6 months ago

2.160.12

6 months ago

2.160.17

6 months ago

2.160.19

6 months ago

2.160.10

6 months ago

2.118.1

10 months ago

3.0.2

7 months ago

3.0.0

7 months ago

2.130.10

9 months ago

2.117.4

10 months ago

2.130.11

9 months ago

2.117.2

10 months ago

2.117.0

10 months ago

2.150.0

8 months ago

2.150.1

8 months ago

2.160.4

7 months ago

2.160.2

7 months ago

2.160.1

7 months ago

2.119.5

10 months ago

2.119.4

10 months ago

2.119.3

10 months ago

2.119.2

10 months ago

2.119.0

10 months ago

3.5.0

6 months ago

3.4.1

6 months ago

2.152.2

8 months ago

2.152.3

8 months ago

2.152.1

8 months ago

3.4.6

6 months ago

3.4.5

6 months ago

3.3.2

6 months ago

2.151.0

8 months ago

2.142.1

8 months ago

2.142.0

8 months ago

2.154.0

7 months ago

2.141.2

8 months ago

2.141.1

8 months ago

2.130.9

9 months ago

2.130.7

9 months ago

2.130.6

9 months ago

2.153.3

7 months ago

2.130.4

9 months ago

2.153.1

8 months ago

2.153.2

7 months ago

2.153.0

8 months ago

2.121.2

10 months ago

3.9.1

6 months ago

3.9.0

6 months ago

2.133.5

9 months ago

2.133.4

9 months ago

3.10.0

6 months ago

2.133.2

9 months ago

2.156.1

7 months ago

2.133.0

9 months ago

2.143.0

8 months ago

2.143.1

8 months ago

2.120.0

10 months ago

3.7.0

6 months ago

2.132.1

9 months ago

2.155.0

7 months ago

2.132.0

9 months ago

2.123.4

10 months ago

2.146.1

8 months ago

2.123.3

10 months ago

2.123.2

10 months ago

2.123.1

10 months ago

2.146.0

8 months ago

2.145.0

8 months ago

2.145.1

8 months ago

2.122.0

10 months ago

2.134.0

9 months ago

2.125.4

9 months ago

2.148.1

8 months ago

2.125.3

9 months ago

2.125.2

9 months ago

2.148.0

8 months ago

2.125.0

9 months ago

2.137.0

9 months ago

2.147.1

8 months ago

2.124.0

9 months ago

2.159.5

7 months ago

2.159.6

7 months ago

2.159.4

7 months ago

2.159.1

7 months ago

2.159.2

7 months ago

2.112.0

11 months ago

2.111.1

11 months ago

2.111.0

11 months ago

2.95.0

1 year ago

2.95.4

1 year ago

2.103.1

12 months ago

2.94.27

1 year ago

2.94.24

1 year ago

2.94.23

1 year ago

2.94.25

1 year ago

2.104.0

12 months ago

2.104.2

12 months ago

2.104.3

12 months ago

2.104.4

12 months ago

2.106.3

12 months ago

2.106.4

12 months ago

2.106.5

12 months ago

2.105.0

12 months ago

2.110.0

11 months ago

2.106.2

12 months ago

2.108.1

11 months ago

2.109.0

11 months ago

2.109.1

11 months ago

2.98.2

12 months ago

2.98.0

1 year ago

2.100.0

12 months ago

2.108.0

12 months ago

2.97.2

1 year ago

2.97.0

1 year ago

2.101.0

12 months ago

2.101.1

12 months ago

2.102.1

12 months ago

2.94.17

1 year ago

2.94.16

1 year ago

2.94.2

1 year ago

2.94.19

1 year ago

2.94.18

1 year ago

2.94.13

1 year ago

2.94.12

1 year ago

2.94.6

1 year ago

2.94.15

1 year ago

2.94.4

1 year ago

2.94.9

1 year ago

2.94.7

1 year ago

2.94.8

1 year ago

2.94.0

1 year ago

2.79.9

1 year ago

2.79.7

1 year ago

2.79.6

1 year ago

2.79.5

1 year ago

2.79.4

1 year ago

2.82.0

1 year ago

2.93.0

1 year ago

2.78.2

1 year ago

2.78.1

1 year ago

2.89.2

1 year ago

2.92.1

1 year ago

2.77.0

1 year ago

2.92.0

1 year ago

2.76.2

1 year ago

2.91.1

1 year ago

2.87.0

1 year ago

2.90.0

1 year ago

2.85.1

1 year ago

2.84.0

1 year ago

2.84.2

1 year ago

2.75.0

1 year ago

2.74.0

1 year ago

2.73.1

1 year ago

2.73.0

1 year ago

2.72.0

1 year ago

2.72.5

1 year ago

2.72.4

1 year ago

2.72.3

1 year ago

2.67.1

1 year ago

2.70.1

1 year ago

2.70.0

1 year ago

2.67.0

1 year ago

2.66.0

1 year ago

2.66.2

1 year ago

2.66.3

1 year ago

2.65.9

1 year ago

2.56.0

1 year ago

2.56.5

1 year ago

2.56.1

1 year ago

2.56.2

1 year ago

2.64.0

1 year ago

2.49.5

1 year ago

2.49.2

1 year ago

2.49.3

1 year ago

2.49.0

1 year ago

2.63.0

1 year ago

2.48.3

1 year ago

2.48.0

1 year ago

2.51.2

1 year ago

2.51.0

1 year ago

2.51.1

1 year ago

2.59.2

1 year ago

2.59.3

1 year ago

2.59.4

1 year ago

2.59.0

1 year ago

2.50.0

1 year ago

2.61.1

1 year ago

2.61.2

1 year ago

2.34.0

2 years ago

2.45.0

1 year ago

2.45.1

1 year ago

2.33.1

2 years ago

2.33.3

2 years ago

2.33.2

2 years ago

2.44.2

1 year ago

2.32.0

2 years ago

2.31.0

2 years ago

2.39.0

2 years ago

2.39.3

1 year ago

2.39.2

1 year ago

2.37.3

2 years ago

2.37.1

2 years ago

2.37.0

2 years ago

2.40.0

1 year ago

2.36.0

2 years ago

2.47.3

1 year ago

2.47.0

1 year ago

2.47.2

1 year ago

2.47.1

1 year ago

2.35.0

2 years ago

2.22.0

2 years ago

2.21.0

2 years ago

2.21.1

2 years ago

2.29.0

2 years ago

2.20.0

2 years ago

2.20.1

2 years ago

2.28.0

2 years ago

2.27.0

2 years ago

2.26.2

2 years ago

2.26.0

2 years ago

2.25.1

2 years ago

2.24.1

2 years ago

2.24.0

2 years ago

2.24.3

2 years ago

2.11.0

2 years ago

2.4.0

2 years ago

2.3.0

2 years ago

2.10.2

2 years ago

2.2.3

2 years ago

2.2.2

2 years ago

2.0.2

2 years ago

2.0.7

2 years ago

2.0.0

2 years ago

2.7.0

2 years ago

2.7.1

2 years ago

1.110.2

2 years ago

1.110.0

2 years ago

2.6.2

2 years ago

1.109.0

2 years ago

2.5.0

2 years ago

1.108.1

2 years ago

1.94.5

2 years ago

1.94.2

2 years ago

1.94.1

2 years ago

1.94.0

2 years ago

1.103.0

2 years ago

1.102.1

2 years ago

1.96.0

2 years ago

1.105.3

2 years ago

1.105.2

2 years ago

1.95.2

2 years ago

1.95.1

2 years ago

1.95.0

2 years ago

1.104.2

2 years ago

1.105.6

2 years ago

1.98.0

2 years ago

1.99.1

2 years ago

1.97.0

2 years ago

1.106.0

2 years ago

1.101.8

2 years ago

1.101.6

2 years ago

1.101.3

2 years ago

1.101.4

2 years ago

1.101.2

2 years ago

1.101.9

2 years ago

1.90.6

2 years ago

1.90.4

2 years ago

1.90.2

2 years ago

1.90.1

2 years ago

1.93.1

2 years ago

1.92.3

2 years ago

1.92.2

2 years ago

1.92.0

2 years ago

1.84.0

2 years ago

1.84.2

2 years ago

1.89.1

2 years ago

1.91.1

2 years ago

1.91.0

2 years ago

1.85.0

2 years ago

1.82.1

2 years ago

1.82.2

2 years ago

1.82.3

2 years ago

1.79.0

2 years ago

1.76.2

2 years ago

1.78.0

2 years ago

1.78.1

2 years ago

1.78.2

2 years ago

1.78.3

2 years ago

1.81.0

2 years ago

1.77.0

2 years ago

1.74.0

2 years ago

1.75.0

2 years ago

1.75.1

2 years ago

1.72.1

2 years ago

1.72.2

2 years ago

1.76.0

2 years ago

1.76.1

2 years ago

1.73.0

2 years ago

1.56.0

2 years ago

1.63.1

2 years ago

1.67.0

2 years ago

1.48.3

2 years ago

1.70.0

2 years ago

1.70.2

2 years ago

1.51.0

2 years ago

1.55.0

2 years ago

1.53.4

2 years ago

1.53.6

2 years ago

1.57.1

2 years ago

1.53.5

2 years ago

1.60.2

2 years ago

1.60.1

2 years ago

1.64.0

2 years ago

1.68.0

2 years ago

1.68.1

2 years ago

1.49.1

2 years ago

1.49.0

2 years ago

1.71.0

2 years ago

1.71.1

2 years ago

1.52.0

2 years ago

1.58.1

2 years ago

1.58.0

2 years ago

1.61.0

2 years ago

1.65.0

2 years ago

1.65.1

2 years ago

1.53.2

2 years ago

1.53.1

2 years ago

1.55.2

2 years ago

1.55.1

2 years ago

1.59.0

2 years ago

1.55.4

2 years ago

1.55.3

2 years ago

1.55.5

2 years ago

1.62.0

2 years ago

1.62.1

2 years ago

1.66.0

2 years ago

1.66.1

2 years ago

1.50.1

2 years ago

1.50.0

2 years ago

1.50.3

2 years ago

1.50.2

2 years ago

1.54.0

2 years ago

1.50.4

2 years ago

1.37.1

2 years ago

1.44.4

2 years ago

1.48.2

2 years ago

1.48.1

2 years ago

1.44.5

2 years ago

1.29.1

2 years ago

1.29.4

2 years ago

1.29.2

2 years ago

1.29.3

2 years ago

1.32.1

2 years ago

1.34.0

2 years ago

1.38.0

2 years ago

1.41.0

2 years ago

1.41.2

2 years ago

1.45.0

2 years ago

1.26.1

3 years ago

1.46.11

2 years ago

1.46.10

2 years ago

1.35.1

2 years ago

1.39.1

2 years ago

1.35.6

2 years ago

1.39.0

2 years ago

1.35.4

2 years ago

1.35.8

2 years ago

1.46.6

2 years ago

1.35.10

2 years ago

1.27.0

3 years ago

1.46.5

2 years ago

1.46.8

2 years ago

1.46.7

2 years ago

1.46.9

2 years ago

1.30.2

2 years ago

1.30.0

2 years ago

1.30.1

2 years ago

1.36.0

2 years ago

1.47.0

2 years ago

1.28.2

3 years ago

1.28.0

3 years ago

1.28.4

2 years ago

1.31.1

2 years ago

1.31.0

2 years ago

1.25.4

3 years ago

1.25.0

3 years ago

1.25.1

3 years ago

1.23.0

3 years ago

1.22.2

3 years ago

1.14.1

3 years ago

1.14.0

3 years ago

1.10.3

3 years ago

1.12.0

3 years ago

1.10.2

3 years ago

1.11.0

3 years ago

1.15.0

3 years ago

1.13.1

3 years ago

1.13.0

3 years ago

1.11.1

3 years ago

1.15.2

3 years ago

1.15.1

3 years ago

1.9.11

3 years ago

1.10.0

3 years ago

1.9.10

3 years ago

1.9.9

3 years ago

1.9.8

3 years ago

1.9.7

3 years ago

1.9.6

3 years ago

1.9.1

3 years ago

1.9.5

3 years ago

1.9.4

3 years ago

1.9.3

3 years ago

1.9.2

3 years ago

1.9.0

3 years ago

1.8.8

3 years ago

1.8.7

3 years ago

1.8.6

3 years ago

1.8.5

3 years ago

1.8.4

3 years ago

1.8.2

3 years ago

1.8.1

3 years ago

1.8.0

3 years ago

1.7.8

3 years ago

1.7.7

3 years ago

1.8.3

3 years ago

1.7.3

3 years ago

1.7.6

3 years ago

1.7.5

3 years ago

1.7.4

3 years ago

1.7.1

3 years ago

1.7.0

3 years ago

1.6.0

3 years ago

1.5.0

3 years ago

1.4.0

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.0

3 years ago