UNPKG

spa-navigator

Version:

A lightweight JavaScript library for managing **Single Page Application (SPA)** navigation without reloading the page and url management.The SPA-Navigator NPM package helps implement 'soft' page loads for Single Page Applications (SPAs). This allows for a

168 lines (127 loc) • 4.67 kB
# **SPA Navigator** šŸ„ā€ā™‚ļøšŸš€ ### **By Terence Onyeweke <Mayor>** A lightweight JavaScript library for managing **Single Page Application (SPA)** navigation without reloading the page and handling URL management. The **SPA-Navigator** NPM package helps implement "soft" page loads for Single Page Applications (SPAs), improving user experience without full page reloads. --- ## **šŸ“Œ Features** āœ”ļø **Smooth Page Navigation** – Switch between pages without reloading. āœ”ļø **History Management** – Supports browser back/forward navigation. āœ”ļø **Easy Integration** – Works with plain HTML, CSS, and JavaScript. āœ”ļø **Minimal Dependencies** – No external frameworks required. --- ## **šŸ“¦ Installation** ### **Option 1: Using npm** ```sh npm install spa-navigator ``` ### **Option 2: Using a CDN** Include the minified script in your HTML file: ```html <script src="https://cdn.jsdelivr.net/npm/spa-navigator@latest/dist/spa-navigator.min.js"></script> ``` --- ## **šŸš€ Getting Started** ### **1ļøāƒ£ Include the Library** If using a local file: ```html <script src="path/to/spa-navigator.min.js"></script> ``` Or if using npm: ```js import SPANavigator from "spa-navigator"; ``` ### **2ļøāƒ£ Initialize SPA Navigator** ```js window.onload = function () { window.spa = new SPANavigator({ pages: ["home", "about", "contact"], defaultPage: "home" }); }; ``` ### **3ļøāƒ£ Create Your HTML Structure** ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>SPA Navigator Demo</title> <script src="dist/spa-navigator.min.js"></script> </head> <body> <header> <h1>My Website</h1> </header> <nav> <a href="javascript:void(0)" onclick="spa.showPage('home')">Home</a> <a href="javascript:void(0)" onclick="spa.showPage('about')">About</a> <a href="javascript:void(0)" onclick="spa.showPage('contact')">Contact</a> </nav> <main> <section id="home"><h1>šŸ” Home Page</h1></section> <section id="about"><h1>šŸ“– About Page</h1></section> <section id="contact"><h1>šŸ“© Contact Page</h1></section> </main> <footer> <p>Ā© 2025 SPA Navigator. All rights reserved.</p> </footer> </body> </html> ``` --- ## **āš™ļø API Methods** ### **1ļøāƒ£ `showPage(pageId, addToHistory = true)`** Changes the visible page without reloading. šŸ”¹ **`pageId`** – The ID of the section to display. šŸ”¹ **`addToHistory`** *(optional)* – Whether to add the page to the browser history. ```js spa.showPage("about"); // Navigates to the About page ``` --- ## **🚨 Important: Redirecting Non-Existent Routes to `index.html`** ### **šŸ”„ Why Redirect All Routes to `index.html`?** For Single Page Applications (SPAs), the browser handles navigation **without reloading the page**. However, when a user refreshes the page or directly visits a deep link (`/about`, `/contact`), the server **tries to load an actual file** that does not exist. To prevent "404 Not Found" errors, configure the server to **redirect all unknown routes to `index.html`**. ### **šŸ“Œ How to Configure This?** #### **For Apache (`.htaccess` file):** ```apache <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.html [L] </IfModule> ``` #### **For Nginx (`nginx.conf`):** ```nginx location / { try_files $uri /index.html; } ``` #### **For Express.js (Node.js Backend):** ```js const express = require('express'); const app = express(); app.use(express.static('public')); app.get('*', (req, res) => { res.sendFile(__dirname + '/public/index.html'); }); app.listen(3000, () => console.log('Server running on port 3000')); ``` ### **šŸ” Reasons for Redirecting to `index.html`** āœ”ļø Prevents **404 errors** when refreshing or accessing deep links. āœ”ļø Ensures **seamless navigation** across the entire application. āœ”ļø Improves **SEO handling** when paired with proper meta tags. --- ## **šŸŽÆ How It Works** 1ļøāƒ£ **Hides all sections** except the selected page. 2ļøāƒ£ **Updates the browser history** for back/forward navigation. 3ļøāƒ£ **Redirects all unknown routes to `index.html`**, allowing the SPA to handle navigation. --- ## **šŸ“œ License** This project is licensed under the **MIT License**. --- ## **🌟 Contributing** Contributions are welcome! Feel free to submit issues and pull requests.