Giter Club home page Giter Club logo

simple-githubpages-spa-router's Introduction

simple-githubpages-spa-router

Simple web router for SPA hosted with Github Pages.

You can see the basic use of this library in this Demo Page or you can check out the source code of the page the demo-page branch.

Disclaimer

Im not by any means an experienced web developer. This repository started as a side project while developing my personal Devlog web page with Github Pages.

I'm using this side project for personal studying purposes only.

Topics I'm studying with this project:

  • Typescript (the basics)
  • Library development with typescript
  • Manipulation of the Window Location and History properties with Javascript
  • The basics of how SPA's could work under the hood
  • Client side rendering ideas

Any comments are completely welcome. I am passionate about always learning new things.

About

This is a simple web router written in Typescript that can help you if you are trying to build a simple SPA using Github Pages as your hosting service.

Requirements

  • Rollup or any other bundler or library that lets you resolve Node module imports on the client side when building your project

Usage

Installation

You can install the library as a npm package with the next command:

$ npm install simple-githubpages-spa-router

createRouter()

Parameters:

routes

Array of the routes defined for the project.

For more information on creating routes please use this example.

repoName

Name of the page repository, this parameter is optional and you can define it only if your github page does not count with a custom domain and it is using the defatult one, example: github.io/repoName/

customListener

An optional custom method, that will be attached to the listener for the event "popstate" from the window element. If not defined, the router will use the default listener method.

Creating routes

A route is represented as an object with the following structure:

// Render method example
function renderAbout() {
    body = document.getElementById("body");
   
    body!.innterHTML = `<h1> About </h1>`;
}

var route = { "/about", renderAbout }

Basic implementation

Import the module into your main javascript file and use the method createRouter() to initialize a new gpSpaRouter instance to use in your project.

// index.js
import renderMethod1 from "./first_module";
import renderMethod2 from "./second_module";

import { createRouter } from "simple-githubpages-spa-router";

let router = createRouter([
    {path: "/first_path", pageRenderer: renderMethod1},
    {path: "/second_path", pageRenderer: renderMethod2},
], '/repositoryName');

Creating anchor <a> tags for navigation

You can tell your web page when to render the content attached to a path by using the <a> tag and writing the path as a reference with the hash format:

<a href="#/myPage"> Render my content </>

You can still use normal hash paths to anchor segments in your page, just remember to remove the / from the path.

Normal hash path Router hash path
#about #/about

Redirecting to the main page

By default, the hashed path #/ is reserved for reloading the index.html in case you want to use it to take you to the main page.

Handling page reload

By default, github pages will redirect you to a 404 error page when trying to load a path that does not exist. The custom 404.html page created in your repository would prevent this by parsing the custom path in the URL as a query parameter and redirecting you to the main HTML document. By doing this, the router can catch the requested path and render out the content.

To do list:

  • Handling of nested paths
  • Support for a custom 404 error page renderer method
  • A history property for the router to show all past navigated routes

simple-githubpages-spa-router's People

Contributors

clouduser98 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.