GithubHelp home page GithubHelp logo

pinecone-router / middleware-render Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 393 KB

WARNINGL not working currently will work on it when i have time. A Turbolinks-like functionality for Pinecone Router

License: MIT License

JavaScript 100.00%
alpinejs alpine router pinecone pinecone-router

middleware-render's Introduction

Display Server-Rendered Pages with Pinecone Router

GitHub tag (latest by date) npm bundle size Downloads from Jsdelivr Github Downloads from Jsdelivr NPM npm Changelog

A middleware for Pinecone Router that add Turbolinks-like functionality with extra features.

Warning: not working currently, will work on it asap, just don't have time

About

A middleware that adds Turbolinks-like functionality for Pinecone Router, while still allowing you to handle routes!

Development version, watch for v1.0 :)

Features:

  • No server co-operation needed! serve regular html files as normally done.
  • Allow route checking before displaying pages! can be used for client-side authorization etc.
  • Preload pages when hovering over links!

Installation

Current version not working! sorry

CDN

Include the following <script> tag in the <head> of your document, before Pinecone Router:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/index.umd.js"></script>

ES6 Module:

import 'https://cdn.jsdelivr.net/npm/[email protected]/dist/index.umd.js';

NPM

npm install pinecone-router-middleware-render
// load this middleware
import 'pinecone-router-middleware-render';
// then load pinecone router
import 'pinecone-router';

Important: This must be added before loading Pinecone Router.

Usage

  1. Enable render middleware in the router Settings:
function router() {
	return {
		settings: {
			middlewares: {
				render: {
					enable: true;
				}
			}
		}
	}
<div x-data="router()" x-router></div>

By default this will fetch the whole page and replaces the <body> content. To use another element instead, set its selector in settings.

Settings

render: {
	enable: true,
	basePath: '/',
	selector: '#app',
}

Handling routes while using render

While optional, you can also handle routes while all pages render normally!

<div x-data="router()" x-router>
	<template x-route="/hello/:name" x-handler="hello"></template>
</div>

Note: The routes will be handled before the page is rendered.

Authorization

If you'd like to make checks before actually displaying a page, using authentication/authorization etc, you can make your checks in the handler. Then within the handler, if you need to redirect the user to another page simply return context.redirect('/another/page') this way it'll prevent the page from rendering and go to the other page directly.

Example:

In this example the user will only be allowed to edit their own profile

<div x-data="router()" x-router>
...
<template
	x-route="/profile/:username/edit"
	x-handler="editProfile"
></template>
...

The handler: (auth is a placeholder name, replace it with your own auth provider methods)

editProfile(context) {
	if (context.params.username != auth.username) {
		return context.redirect('/unauthorized');
	}
}

Advanced

Show

Notfound and specifying routes

By default, 404 pages are left to the server to handle. However, if you'd like to specify the routes allowed, you can do it like this:

<div x-data="router()" x-router>
	<template x-route="/"></template>
	<template x-route="/hello/:name"></template>
	<template x-route="notfound" x-handler="notfound"></template>
</div>

As you see, the handler is optional on routes as the page will be rendered regardless, but you can add it if you need it.

Supported versions

Version Pinecone Router Versions
0.0.3 0.3.0

Contributing:

Please refer to CONTRIBUTING.md

Versioning

This projects follow the Semantic Versioning guidelines.

License

Copyright (c) 2021 Rafik El Hadi Houari

Licensed under the MIT license, see LICENSE.md for details.

middleware-render's People

Contributors

rehhouari avatar

Stargazers

 avatar  avatar

Watchers

 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.