navigo

repository·master·Indexed 25 days ago

https://github.com/krasimir/navigo

A simple, dependency-free, and minimalistic vanilla JavaScript router (v8.11.1). It is lightweight (~10KB minified) and supports both History API and hash-based routing. Key features include parameterized routes, route hooks (before, after, leave, already), named routes, and automatic link handling via the data-navigo attribute.

Tokens
6.5K
Snippets
15
Records
51
Agent score
81%

What's inside navigo

  1. Explore Navigo routing patterns

    master

    Navigo provides several routing modes and patterns. You can find source code examples for the following implementations in the examples/ directory:

    • Basic routing (with full page reload): Standard routing behavior.
    • Basic routing (SPA experience): Single Page Application style routing.
    • Using regular expressions: Routing using RegExp patterns.
    • Node: Usage within a Node.js environment.
    • Routing based on the hash in the URL: Using the URL hash for navigation.
    • Mixed routing: Combining absolute URL navigation with hash-based navigation.
    • Custom selector: Implementing custom logic for route selection.
  2. Run Navigo examples

    master

    To run the provided examples, navigate to the examples directory, install dependencies, and then start the local server for a specific example folder.

    1. Install dependencies in the examples directory: yarn install.
    2. Navigate to a specific example folder (e.g., basic, basic-spa, regexp).
    3. Start the local server: yarn start.

    The server will typically start at http://localhost:3000.

  3. Bind page links using data-navigo

    master
    To allow Navigo to handle standard HTML links, add the data-navigo attribute to your <a> tags. You must call router.updatePageLinks() whenever new links are added to the DOM to ensure they are bound.
  4. Augment HTML <a> tags with data-navigo

    master

    To integrate standard HTML links with Navigo and prevent full page reloads, add the data-navigo attribute to your <a> tags.

    • Basic usage: <a href="/path" data-navigo>Link</a>
    • Ignore link: Use data-navigo="false" to prevent Navigo from intercepting the click.
    • Passing options: You can pass NavigateOptions via the data-navigo-options attribute using a comma-separated string format.
    • Custom Selector: You can define a custom CSS selector for links during router initialization using the linksSelector option.

    Note: Links with target="_blank" are ignored even if they have data-navigo.

    Important: Since Navigo is not tied to a rendering engine, you must call router.updatePageLinks() whenever you dynamically change the DOM to ensure new links are intercepted.

  5. Quick start with Navigo

    master

    To use Navigo, import the library, initialize the router with your application's root path, define your routes, and call resolve() to start listening for changes.

    1. Initialize: Pass the root path of your app to the Navigo constructor.
    2. Define Routes: Use router.on(path, callback) to map a URL path to a function.
    3. Resolve: Call router.resolve() to trigger the initial routing logic.
    4. Navigate: Use router.navigate(path) to programmatically change the URL and trigger route resolution.
    import Navigo from 'navigo';
    
    const router = new Navigo('/');
    
    router.on('/about', function () {
      // do something
    });
    
    router.resolve();
    
    // To navigate programmatically:
    router.navigate('/about');
  6. Initialize Navigo router

    master

    To use Navigo, create a new instance of the Navigo class. The constructor requires the root path of your application as the first argument. You can optionally provide a second argument for RouterOptions.

    Key options:

    • strategy: Defines how many matches the router finds. Set to 'ALL' to find multiple matches, or 'ONE' (default) to stop after the first match.
    • noMatchWarning: If set to true, prevents the router from logging a warning when no matching route is found.
  7. Use data-navigo for automatic link handling

    master

    You can integrate Navigo with HTML links by adding the data-navigo attribute. This automatically transforms standard <a> tags into Navigo navigate callers, preventing full page reloads.

    <a href="/about/contacts" data-navigo>Contacts</a>
  8. Match current location with matchLocation()

    master
    The matchLocation(path, [location]) method provides bare matching logic against the current browser location. If a second argument is provided, it matches against that specific string instead of the current browser URL.